From c47ed48d195c79dcb6e49502a651fbf39ce42ce9 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 1 Sep 2024 23:08:42 +0200 Subject: [PATCH] fix(aliases): Improve analyzer --- handlers/aliases/analyzer/value_valid.go | 32 ++++++++++++----- handlers/aliases/handlers/completions.go | 36 ++++++++++++------- .../aliases/lsp/text-document-completion.go | 26 +++++++++++--- 3 files changed, 69 insertions(+), 25 deletions(-) diff --git a/handlers/aliases/analyzer/value_valid.go b/handlers/aliases/analyzer/value_valid.go index 59e4f45..bc19351 100644 --- a/handlers/aliases/analyzer/value_valid.go +++ b/handlers/aliases/analyzer/value_valid.go @@ -6,8 +6,9 @@ import ( "config-lsp/handlers/aliases/ast" "config-lsp/handlers/aliases/fetchers" "config-lsp/handlers/aliases/indexes" - "config-lsp/utils" "fmt" + "net/mail" + "path" ers "errors" ) @@ -51,13 +52,6 @@ func analyzeValuesAreValid( for _, value := range entry.Values.Values { newErrors := checkValue(d.Indexes, value) - newErrors = utils.Map( - newErrors, - func(e common.LSPError) common.LSPError { - startPosition := value.GetAliasValue().Location.Start.Character - return e.ShiftCharacter(-startPosition) - }, - ) errors = append(errors, newErrors...) } @@ -82,6 +76,28 @@ func checkValue( Err: ers.New(fmt.Sprintf("User '%s' not found", aliasValue.Value)), }} } + case ast.AliasValueEmail: + emailValue := value.(ast.AliasValueEmail) + + if _, error := mail.ParseAddress(emailValue.Value); error != nil { + return []common.LSPError{{ + Range: emailValue.Location, + Err: ers.New(fmt.Sprintf("This does not seem to be a valid email: %s", error.Error())), + }} + } + case ast.AliasValueFile: + fileValue := value.(ast.AliasValueFile) + + // I'm not sure if the path really needs to be absolute + // The docs say: + // "Append messages to file, specified by its absolute pathname." + // + if !path.IsAbs(fileValue.Value) { + return []common.LSPError{{ + Range: fileValue.Location, + Err: ers.New("This path must be absolute"), + }} + } } return nil } diff --git a/handlers/aliases/handlers/completions.go b/handlers/aliases/handlers/completions.go index 4c2d107..2cb9e07 100644 --- a/handlers/aliases/handlers/completions.go +++ b/handlers/aliases/handlers/completions.go @@ -5,7 +5,6 @@ import ( "config-lsp/handlers/aliases/ast" "config-lsp/handlers/aliases/fetchers" "config-lsp/handlers/aliases/indexes" - "config-lsp/utils" "fmt" protocol "github.com/tliron/glsp/protocol_3_16" @@ -56,7 +55,12 @@ func GetCompletionsForEntry( completions = append(completions, getCommandCompletion()) completions = append(completions, getIncludeCompletion()) - completions = append(completions, getUserCompletions(i, "", 0)...) + completions = append(completions, getUserCompletions( + i, + indexes.NormalizeKey(entry.Key.Value), + "", + 0, + )...) println("la completions etaient", completions) return completions, nil @@ -68,6 +72,7 @@ func GetCompletionsForEntry( return getUserCompletions( i, + indexes.NormalizeKey(entry.Key.Value), userValue.Value, relativeCursor, ), nil @@ -106,20 +111,27 @@ func getIncludeCompletion() protocol.CompletionItem { func getUserCompletions( i *indexes.AliasesIndexes, + excludeKey string, line string, cursor uint32, ) []protocol.CompletionItem { users := fetchers.GetAvailableUserValues(i) kind := protocol.CompletionItemKindValue - return utils.MapMapToSlice( - users, - func(name string, user fetchers.User) protocol.CompletionItem { - return protocol.CompletionItem{ - Label: name, - Kind: &kind, - Documentation: user.Documentation(), - } - }, - ) + + completions := make([]protocol.CompletionItem, 0) + + for name, user := range users { + if name == excludeKey { + continue + } + + completions = append(completions, protocol.CompletionItem{ + Label: name, + Kind: &kind, + Documentation: user.Documentation(), + }) + } + + return completions } diff --git a/handlers/aliases/lsp/text-document-completion.go b/handlers/aliases/lsp/text-document-completion.go index 5795dad..0240c8f 100644 --- a/handlers/aliases/lsp/text-document-completion.go +++ b/handlers/aliases/lsp/text-document-completion.go @@ -27,9 +27,25 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa entry := rawEntry.(*ast.AliasEntry) - return handlers.GetCompletionsForEntry( - cursor, - entry, - d.Indexes, - ) + if entry.Key == nil { + return handlers.GetAliasesCompletions(d.Indexes), nil + } + + if cursor >= entry.Key.Location.Start.Character && cursor <= entry.Key.Location.End.Character { + return handlers.GetAliasesCompletions(d.Indexes), nil + } + + if entry.Separator == nil && cursor > entry.Key.Location.End.Character { + return nil, nil + } + + if cursor > entry.Separator.End.Character { + return handlers.GetCompletionsForEntry( + cursor, + entry, + d.Indexes, + ) + } + + return nil, nil }