From 89d11a16dc27648e920e1fea0263d369cfa0e955 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 7 Sep 2024 14:41:54 +0200 Subject: [PATCH] refactor: Improve utils package structure --- doc-values/base-value.go | 1 - doc-values/utils.go | 1 - handlers/aliases/analyzer/analyzer.go | 1 - .../aliases/lsp/text-document-did-change.go | 1 - .../aliases/lsp/text-document-did-open.go | 1 - handlers/fstab/text-document-did-change.go | 1 - handlers/fstab/text-document-did-open.go | 1 - handlers/hosts/analyzer/analyzer.go | 1 - .../hosts/lsp/text-document-did-change.go | 1 - handlers/hosts/lsp/text-document-did-open.go | 1 - handlers/openssh/diagnostics.go | 1 - .../wireguard/lsp/text-document-did-change.go | 1 - .../wireguard/lsp/text-document-did-open.go | 1 - utils/common.go | 166 ------------------ utils/ip-host.go | 2 +- utils/slices.go | 143 +++++++++++++++ utils/strings.go | 32 ++++ utils/text.go | 13 -- 18 files changed, 176 insertions(+), 193 deletions(-) create mode 100644 utils/slices.go delete mode 100644 utils/text.go diff --git a/doc-values/base-value.go b/doc-values/base-value.go index b2c2ba4..2a2d3d6 100644 --- a/doc-values/base-value.go +++ b/doc-values/base-value.go @@ -2,7 +2,6 @@ package docvalues import ( "config-lsp/utils" - protocol "github.com/tliron/glsp/protocol_3_16" ) diff --git a/doc-values/utils.go b/doc-values/utils.go index 68e7326..8d5a403 100644 --- a/doc-values/utils.go +++ b/doc-values/utils.go @@ -2,7 +2,6 @@ package docvalues import ( "config-lsp/utils" - protocol "github.com/tliron/glsp/protocol_3_16" "golang.org/x/exp/slices" ) diff --git a/handlers/aliases/analyzer/analyzer.go b/handlers/aliases/analyzer/analyzer.go index 68b6382..dd1fc38 100644 --- a/handlers/aliases/analyzer/analyzer.go +++ b/handlers/aliases/analyzer/analyzer.go @@ -4,7 +4,6 @@ import ( "config-lsp/common" "config-lsp/handlers/aliases" "config-lsp/utils" - protocol "github.com/tliron/glsp/protocol_3_16" ) diff --git a/handlers/aliases/lsp/text-document-did-change.go b/handlers/aliases/lsp/text-document-did-change.go index 87673d4..b8dd484 100644 --- a/handlers/aliases/lsp/text-document-did-change.go +++ b/handlers/aliases/lsp/text-document-did-change.go @@ -5,7 +5,6 @@ import ( "config-lsp/handlers/aliases" "config-lsp/handlers/aliases/analyzer" "config-lsp/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) diff --git a/handlers/aliases/lsp/text-document-did-open.go b/handlers/aliases/lsp/text-document-did-open.go index 81f9c1e..bccf0aa 100644 --- a/handlers/aliases/lsp/text-document-did-open.go +++ b/handlers/aliases/lsp/text-document-did-open.go @@ -6,7 +6,6 @@ import ( "config-lsp/handlers/aliases/analyzer" "config-lsp/handlers/aliases/ast" "config-lsp/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) diff --git a/handlers/fstab/text-document-did-change.go b/handlers/fstab/text-document-did-change.go index b192dcf..8b465b3 100644 --- a/handlers/fstab/text-document-did-change.go +++ b/handlers/fstab/text-document-did-change.go @@ -3,7 +3,6 @@ package fstab import ( "config-lsp/common" "config-lsp/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) diff --git a/handlers/fstab/text-document-did-open.go b/handlers/fstab/text-document-did-open.go index 2380cb9..d64f3ea 100644 --- a/handlers/fstab/text-document-did-open.go +++ b/handlers/fstab/text-document-did-open.go @@ -3,7 +3,6 @@ package fstab import ( "config-lsp/common" "config-lsp/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) diff --git a/handlers/hosts/analyzer/analyzer.go b/handlers/hosts/analyzer/analyzer.go index 9a1c617..0063a64 100644 --- a/handlers/hosts/analyzer/analyzer.go +++ b/handlers/hosts/analyzer/analyzer.go @@ -4,7 +4,6 @@ import ( "config-lsp/common" "config-lsp/handlers/hosts" "config-lsp/utils" - protocol "github.com/tliron/glsp/protocol_3_16" ) diff --git a/handlers/hosts/lsp/text-document-did-change.go b/handlers/hosts/lsp/text-document-did-change.go index 0b4bb34..af39187 100644 --- a/handlers/hosts/lsp/text-document-did-change.go +++ b/handlers/hosts/lsp/text-document-did-change.go @@ -5,7 +5,6 @@ import ( "config-lsp/handlers/hosts" "config-lsp/handlers/hosts/analyzer" "config-lsp/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) diff --git a/handlers/hosts/lsp/text-document-did-open.go b/handlers/hosts/lsp/text-document-did-open.go index 6e17715..2e07550 100644 --- a/handlers/hosts/lsp/text-document-did-open.go +++ b/handlers/hosts/lsp/text-document-did-open.go @@ -7,7 +7,6 @@ import ( "config-lsp/handlers/hosts/ast" "config-lsp/handlers/hosts/indexes" "config-lsp/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) diff --git a/handlers/openssh/diagnostics.go b/handlers/openssh/diagnostics.go index f38bb13..2fe3c92 100644 --- a/handlers/openssh/diagnostics.go +++ b/handlers/openssh/diagnostics.go @@ -3,7 +3,6 @@ package openssh import ( docvalues "config-lsp/doc-values" "config-lsp/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) diff --git a/handlers/wireguard/lsp/text-document-did-change.go b/handlers/wireguard/lsp/text-document-did-change.go index 103755c..c5ec400 100644 --- a/handlers/wireguard/lsp/text-document-did-change.go +++ b/handlers/wireguard/lsp/text-document-did-change.go @@ -4,7 +4,6 @@ import ( "config-lsp/common" "config-lsp/handlers/wireguard/handlers" "config-lsp/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) diff --git a/handlers/wireguard/lsp/text-document-did-open.go b/handlers/wireguard/lsp/text-document-did-open.go index fe003d4..fc9e0b2 100644 --- a/handlers/wireguard/lsp/text-document-did-open.go +++ b/handlers/wireguard/lsp/text-document-did-open.go @@ -4,7 +4,6 @@ import ( "config-lsp/common" "config-lsp/handlers/wireguard/parser" "config-lsp/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) diff --git a/utils/common.go b/utils/common.go index d5f7642..ba45c9c 100644 --- a/utils/common.go +++ b/utils/common.go @@ -2,142 +2,8 @@ package utils import ( "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 { _, err := os.Stat(path) @@ -155,35 +21,3 @@ func IsPathFile(path string) bool { 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 -} diff --git a/utils/ip-host.go b/utils/ip-host.go index 4fab166..84d3a05 100644 --- a/utils/ip-host.go +++ b/utils/ip-host.go @@ -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 // Return: (, ) func (h *IPv4HostSet) AddIP( diff --git a/utils/slices.go b/utils/slices.go new file mode 100644 index 0000000..0b74dab --- /dev/null +++ b/utils/slices.go @@ -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 +} diff --git a/utils/strings.go b/utils/strings.go index 4e85934..1e4f429 100644 --- a/utils/strings.go +++ b/utils/strings.go @@ -19,3 +19,35 @@ func GetTrimIndex(s string) []int { func SplitIntoLines(s string) []string { 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 +} diff --git a/utils/text.go b/utils/text.go deleted file mode 100644 index 26183b3..0000000 --- a/utils/text.go +++ /dev/null @@ -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 -}