mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
refactor: Improve utils package structure
This commit is contained in:
parent
d7f8b905a4
commit
89d11a16dc
@ -2,7 +2,6 @@ package docvalues
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@ package docvalues
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
"golang.org/x/exp/slices"
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"config-lsp/common"
|
"config-lsp/common"
|
||||||
"config-lsp/handlers/aliases"
|
"config-lsp/handlers/aliases"
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"config-lsp/handlers/aliases"
|
"config-lsp/handlers/aliases"
|
||||||
"config-lsp/handlers/aliases/analyzer"
|
"config-lsp/handlers/aliases/analyzer"
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"config-lsp/handlers/aliases/analyzer"
|
"config-lsp/handlers/aliases/analyzer"
|
||||||
"config-lsp/handlers/aliases/ast"
|
"config-lsp/handlers/aliases/ast"
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
@ -3,7 +3,6 @@ package fstab
|
|||||||
import (
|
import (
|
||||||
"config-lsp/common"
|
"config-lsp/common"
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
@ -3,7 +3,6 @@ package fstab
|
|||||||
import (
|
import (
|
||||||
"config-lsp/common"
|
"config-lsp/common"
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"config-lsp/common"
|
"config-lsp/common"
|
||||||
"config-lsp/handlers/hosts"
|
"config-lsp/handlers/hosts"
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"config-lsp/handlers/hosts"
|
"config-lsp/handlers/hosts"
|
||||||
"config-lsp/handlers/hosts/analyzer"
|
"config-lsp/handlers/hosts/analyzer"
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"config-lsp/handlers/hosts/ast"
|
"config-lsp/handlers/hosts/ast"
|
||||||
"config-lsp/handlers/hosts/indexes"
|
"config-lsp/handlers/hosts/indexes"
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
@ -3,7 +3,6 @@ package openssh
|
|||||||
import (
|
import (
|
||||||
docvalues "config-lsp/doc-values"
|
docvalues "config-lsp/doc-values"
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"config-lsp/common"
|
"config-lsp/common"
|
||||||
"config-lsp/handlers/wireguard/handlers"
|
"config-lsp/handlers/wireguard/handlers"
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"config-lsp/common"
|
"config-lsp/common"
|
||||||
"config-lsp/handlers/wireguard/parser"
|
"config-lsp/handlers/wireguard/parser"
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
166
utils/common.go
166
utils/common.go
@ -2,142 +2,8 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetLine(path string, line int) (string, error) {
|
|
||||||
path = path[len("file://"):]
|
|
||||||
readBytes, err := os.ReadFile(path)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Split file into lines
|
|
||||||
lines := strings.Split(string(readBytes), "\n")
|
|
||||||
|
|
||||||
return lines[line], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Map[T any, O any](values []T, f func(T) O) []O {
|
|
||||||
result := make([]O, len(values))
|
|
||||||
|
|
||||||
for index, value := range values {
|
|
||||||
result[index] = f(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func MapMap[T comparable, O any, P any](values map[T]O, f func(T, O) P) map[T]P {
|
|
||||||
result := make(map[T]P)
|
|
||||||
|
|
||||||
for key, value := range values {
|
|
||||||
result[key] = f(key, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func MapMapToSlice[T comparable, O any, P any](values map[T]O, f func(T, O) P) []P {
|
|
||||||
result := make([]P, 0)
|
|
||||||
|
|
||||||
for key, value := range values {
|
|
||||||
result = append(result, f(key, value))
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func SliceToSet[T comparable](values []T) map[T]struct{} {
|
|
||||||
set := make(map[T]struct{})
|
|
||||||
|
|
||||||
for _, value := range values {
|
|
||||||
set[value] = struct{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
return set
|
|
||||||
}
|
|
||||||
|
|
||||||
func SliceToMap[T comparable, O any](values []T, defaultValue O) map[T]O {
|
|
||||||
set := make(map[T]O)
|
|
||||||
|
|
||||||
for _, value := range values {
|
|
||||||
set[value] = defaultValue
|
|
||||||
}
|
|
||||||
|
|
||||||
return set
|
|
||||||
}
|
|
||||||
|
|
||||||
func FilterWhere[T any](values []T, f func(T) bool) []T {
|
|
||||||
result := make([]T, 0)
|
|
||||||
|
|
||||||
for _, value := range values {
|
|
||||||
if f(value) {
|
|
||||||
result = append(result, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func FilterMap[T comparable, O any](
|
|
||||||
values map[T]O,
|
|
||||||
f func(T, O) bool,
|
|
||||||
) map[T]O {
|
|
||||||
result := make(map[T]O)
|
|
||||||
|
|
||||||
for key, value := range values {
|
|
||||||
if f(key, value) {
|
|
||||||
result[key] = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func FilterMapWhere[T comparable, O any](values map[T]O, f func(T, O) bool) map[T]O {
|
|
||||||
result := make(map[T]O)
|
|
||||||
|
|
||||||
for key, value := range values {
|
|
||||||
if f(key, value) {
|
|
||||||
result[key] = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func KeysAsSet[T comparable, O any](values map[T]O) map[T]struct{} {
|
|
||||||
set := make(map[T]struct{})
|
|
||||||
|
|
||||||
for key := range values {
|
|
||||||
set[key] = struct{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
return set
|
|
||||||
}
|
|
||||||
|
|
||||||
func KeysOfMap[T comparable, O any](values map[T]O) []T {
|
|
||||||
keys := make([]T, 0)
|
|
||||||
|
|
||||||
for key := range values {
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
return keys
|
|
||||||
}
|
|
||||||
|
|
||||||
func ValuesOfMap[T comparable, O any](values map[T]O) []O {
|
|
||||||
keys := make([]O, 0)
|
|
||||||
|
|
||||||
for _, value := range values {
|
|
||||||
keys = append(keys, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
return keys
|
|
||||||
}
|
|
||||||
|
|
||||||
func DoesPathExist(path string) bool {
|
func DoesPathExist(path string) bool {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
|
|
||||||
@ -155,35 +21,3 @@ func IsPathFile(path string) bool {
|
|||||||
|
|
||||||
return err == nil && !info.IsDir()
|
return err == nil && !info.IsDir()
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindPreviousCharacter(line string, character string, startIndex int) (int, bool) {
|
|
||||||
for index := startIndex; index >= 0; index-- {
|
|
||||||
if string(line[index]) == character {
|
|
||||||
return index, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func FindNextCharacter(line string, character string, startIndex int) (int, bool) {
|
|
||||||
for index := startIndex; index < len(line); index++ {
|
|
||||||
if string(line[index]) == character {
|
|
||||||
return index, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func MergeMaps[T comparable, O any](maps ...map[T]O) map[T]O {
|
|
||||||
result := make(map[T]O)
|
|
||||||
|
|
||||||
for _, m := range maps {
|
|
||||||
for key, value := range m {
|
|
||||||
result[key] = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
@ -74,7 +74,7 @@ func CreateIPv4HostSet() IPv4HostSet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a new ip to the host set
|
// AddIP Add a new ip to the host set
|
||||||
// `hostAmount`: Amount of host bits
|
// `hostAmount`: Amount of host bits
|
||||||
// Return: (<Whether the ip has been added>, <error>)
|
// Return: (<Whether the ip has been added>, <error>)
|
||||||
func (h *IPv4HostSet) AddIP(
|
func (h *IPv4HostSet) AddIP(
|
||||||
|
143
utils/slices.go
Normal file
143
utils/slices.go
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
func Group[T any, V comparable](values []T, f func(T) V) map[V][]T {
|
||||||
|
result := make(map[V][]T)
|
||||||
|
|
||||||
|
for _, value := range values {
|
||||||
|
key := f(value)
|
||||||
|
result[key] = append(result[key], value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func Map[T any, O any](values []T, f func(T) O) []O {
|
||||||
|
result := make([]O, len(values))
|
||||||
|
|
||||||
|
for index, value := range values {
|
||||||
|
result[index] = f(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func MapMap[T comparable, O any, P any](values map[T]O, f func(T, O) P) map[T]P {
|
||||||
|
result := make(map[T]P)
|
||||||
|
|
||||||
|
for key, value := range values {
|
||||||
|
result[key] = f(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func MapMapToSlice[T comparable, O any, P any](values map[T]O, f func(T, O) P) []P {
|
||||||
|
result := make([]P, 0)
|
||||||
|
|
||||||
|
for key, value := range values {
|
||||||
|
result = append(result, f(key, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func SliceToSet[T comparable](values []T) map[T]struct{} {
|
||||||
|
set := make(map[T]struct{})
|
||||||
|
|
||||||
|
for _, value := range values {
|
||||||
|
set[value] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
func SliceToMap[T comparable, O any](values []T, defaultValue O) map[T]O {
|
||||||
|
set := make(map[T]O)
|
||||||
|
|
||||||
|
for _, value := range values {
|
||||||
|
set[value] = defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
func FilterWhere[T any](values []T, f func(T) bool) []T {
|
||||||
|
result := make([]T, 0)
|
||||||
|
|
||||||
|
for _, value := range values {
|
||||||
|
if f(value) {
|
||||||
|
result = append(result, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func FilterMap[T comparable, O any](
|
||||||
|
values map[T]O,
|
||||||
|
f func(T, O) bool,
|
||||||
|
) map[T]O {
|
||||||
|
result := make(map[T]O)
|
||||||
|
|
||||||
|
for key, value := range values {
|
||||||
|
if f(key, value) {
|
||||||
|
result[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func FilterMapWhere[T comparable, O any](values map[T]O, f func(T, O) bool) map[T]O {
|
||||||
|
result := make(map[T]O)
|
||||||
|
|
||||||
|
for key, value := range values {
|
||||||
|
if f(key, value) {
|
||||||
|
result[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func KeysAsSet[T comparable, O any](values map[T]O) map[T]struct{} {
|
||||||
|
set := make(map[T]struct{})
|
||||||
|
|
||||||
|
for key := range values {
|
||||||
|
set[key] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
func KeysOfMap[T comparable, O any](values map[T]O) []T {
|
||||||
|
keys := make([]T, 0)
|
||||||
|
|
||||||
|
for key := range values {
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
func ValuesOfMap[T comparable, O any](values map[T]O) []O {
|
||||||
|
keys := make([]O, 0)
|
||||||
|
|
||||||
|
for _, value := range values {
|
||||||
|
keys = append(keys, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
func MergeMaps[T comparable, O any](maps ...map[T]O) map[T]O {
|
||||||
|
result := make(map[T]O)
|
||||||
|
|
||||||
|
for _, m := range maps {
|
||||||
|
for key, value := range m {
|
||||||
|
result[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
@ -19,3 +19,35 @@ func GetTrimIndex(s string) []int {
|
|||||||
func SplitIntoLines(s string) []string {
|
func SplitIntoLines(s string) []string {
|
||||||
return regexp.MustCompile("\r?\n").Split(s, -1)
|
return regexp.MustCompile("\r?\n").Split(s, -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FindPreviousCharacter(line string, character string, startIndex int) (int, bool) {
|
||||||
|
for index := startIndex; index >= 0; index-- {
|
||||||
|
if string(line[index]) == character {
|
||||||
|
return index, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindNextCharacter(line string, character string, startIndex int) (int, bool) {
|
||||||
|
for index := startIndex; index < len(line); index++ {
|
||||||
|
if string(line[index]) == character {
|
||||||
|
return index, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func CountCharacterOccurrences(line string, character rune) int {
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
for _, c := range line {
|
||||||
|
if c == character {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
func CountCharacterOccurrences(line string, character rune) int {
|
|
||||||
count := 0
|
|
||||||
|
|
||||||
for _, c := range line {
|
|
||||||
if c == character {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return count
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user