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/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
}

View File

@ -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{
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
}

View File

@ -27,9 +27,25 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
entry := rawEntry.(*ast.AliasEntry)
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
}