fix(aliases): Improve analyzer

This commit is contained in:
Myzel394 2024-09-01 23:08:42 +02:00
parent e6d396dba0
commit c47ed48d19
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
3 changed files with 69 additions and 25 deletions

View File

@ -6,8 +6,9 @@ import (
"config-lsp/handlers/aliases/ast" "config-lsp/handlers/aliases/ast"
"config-lsp/handlers/aliases/fetchers" "config-lsp/handlers/aliases/fetchers"
"config-lsp/handlers/aliases/indexes" "config-lsp/handlers/aliases/indexes"
"config-lsp/utils"
"fmt" "fmt"
"net/mail"
"path"
ers "errors" ers "errors"
) )
@ -51,13 +52,6 @@ func analyzeValuesAreValid(
for _, value := range entry.Values.Values { for _, value := range entry.Values.Values {
newErrors := checkValue(d.Indexes, value) 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...) errors = append(errors, newErrors...)
} }
@ -82,6 +76,28 @@ func checkValue(
Err: ers.New(fmt.Sprintf("User '%s' not found", aliasValue.Value)), 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 return nil
} }

View File

@ -5,7 +5,6 @@ import (
"config-lsp/handlers/aliases/ast" "config-lsp/handlers/aliases/ast"
"config-lsp/handlers/aliases/fetchers" "config-lsp/handlers/aliases/fetchers"
"config-lsp/handlers/aliases/indexes" "config-lsp/handlers/aliases/indexes"
"config-lsp/utils"
"fmt" "fmt"
protocol "github.com/tliron/glsp/protocol_3_16" protocol "github.com/tliron/glsp/protocol_3_16"
@ -56,7 +55,12 @@ func GetCompletionsForEntry(
completions = append(completions, getCommandCompletion()) completions = append(completions, getCommandCompletion())
completions = append(completions, getIncludeCompletion()) 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) println("la completions etaient", completions)
return completions, nil return completions, nil
@ -68,6 +72,7 @@ func GetCompletionsForEntry(
return getUserCompletions( return getUserCompletions(
i, i,
indexes.NormalizeKey(entry.Key.Value),
userValue.Value, userValue.Value,
relativeCursor, relativeCursor,
), nil ), nil
@ -106,20 +111,27 @@ func getIncludeCompletion() protocol.CompletionItem {
func getUserCompletions( func getUserCompletions(
i *indexes.AliasesIndexes, i *indexes.AliasesIndexes,
excludeKey string,
line string, line string,
cursor uint32, cursor uint32,
) []protocol.CompletionItem { ) []protocol.CompletionItem {
users := fetchers.GetAvailableUserValues(i) users := fetchers.GetAvailableUserValues(i)
kind := protocol.CompletionItemKindValue kind := protocol.CompletionItemKindValue
return utils.MapMapToSlice(
users, completions := make([]protocol.CompletionItem, 0)
func(name string, user fetchers.User) protocol.CompletionItem {
return protocol.CompletionItem{ for name, user := range users {
Label: name, if name == excludeKey {
Kind: &kind, continue
Documentation: user.Documentation(), }
}
}, completions = append(completions, protocol.CompletionItem{
) Label: name,
Kind: &kind,
Documentation: user.Documentation(),
})
}
return completions
} }

View File

@ -27,9 +27,25 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
entry := rawEntry.(*ast.AliasEntry) entry := rawEntry.(*ast.AliasEntry)
return handlers.GetCompletionsForEntry( if entry.Key == nil {
cursor, return handlers.GetAliasesCompletions(d.Indexes), nil
entry, }
d.Indexes,
) 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
} }