mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
fix(aliases): Improvements
This commit is contained in:
parent
742dc48656
commit
f4d20c8e25
@ -9,7 +9,7 @@ import (
|
||||
ers "errors"
|
||||
)
|
||||
|
||||
var requiredFields = []string{
|
||||
var RequiredAliases = []string{
|
||||
indexes.NormalizeKey("mailer-daemon"),
|
||||
indexes.NormalizeKey("hostmaster"),
|
||||
indexes.NormalizeKey("postmaster"),
|
||||
@ -20,7 +20,7 @@ func analyzeContainsRequiredKeys(
|
||||
) []common.LSPError {
|
||||
errors := make([]common.LSPError, 0)
|
||||
|
||||
for _, requiredField := range requiredFields {
|
||||
for _, requiredField := range RequiredAliases {
|
||||
if _, found := d.Indexes.Keys[requiredField]; !found {
|
||||
errors = append(errors, common.LSPError{
|
||||
Range: common.GlobalLocationRange,
|
||||
|
@ -35,6 +35,7 @@ type AliasEntry struct {
|
||||
}
|
||||
|
||||
type AliasesParser struct {
|
||||
// uint32 -> *AliasEntry
|
||||
Aliases *treemap.Map
|
||||
CommentLines map[uint32]struct{}
|
||||
}
|
||||
|
@ -8,10 +8,15 @@ import (
|
||||
)
|
||||
|
||||
type AliasValueInterface interface {
|
||||
GetAliasValue() AliasValue
|
||||
FetchCompletions(line string, cursor uint32) []protocol.CompletionItem
|
||||
CheckIsValid() []*docvalues.InvalidValue
|
||||
}
|
||||
|
||||
func (a AliasValue) GetAliasValue() AliasValue {
|
||||
return a
|
||||
}
|
||||
|
||||
func (a AliasValue) FetchCompletions(line string, cursor uint32) []protocol.CompletionItem {
|
||||
return nil
|
||||
}
|
||||
|
56
handlers/aliases/handlers/completions.go
Normal file
56
handlers/aliases/handlers/completions.go
Normal file
@ -0,0 +1,56 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/aliases/analyzer"
|
||||
"config-lsp/handlers/aliases/ast"
|
||||
"config-lsp/handlers/aliases/indexes"
|
||||
"fmt"
|
||||
|
||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||
)
|
||||
|
||||
func GetAliasesCompletions(
|
||||
i *indexes.AliasesIndexes,
|
||||
) []protocol.CompletionItem {
|
||||
completions := make([]protocol.CompletionItem, 0)
|
||||
aliases := analyzer.RequiredAliases
|
||||
|
||||
kind := protocol.CompletionItemKindValue
|
||||
|
||||
for _, alias := range aliases {
|
||||
if i != nil {
|
||||
if _, found := i.Keys[alias]; found {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
text := fmt.Sprintf("%s: ", alias)
|
||||
completions = append(completions, protocol.CompletionItem{
|
||||
Label: alias,
|
||||
Kind: &kind,
|
||||
InsertText: &text,
|
||||
Documentation: "This alias is required by the aliases file",
|
||||
})
|
||||
}
|
||||
|
||||
return completions
|
||||
}
|
||||
|
||||
func GetCompletionsForEntry(
|
||||
cursor uint32,
|
||||
entry *ast.AliasEntry,
|
||||
i *indexes.AliasesIndexes,
|
||||
) ([]protocol.CompletionItem, error) {
|
||||
completions := make([]protocol.CompletionItem, 0)
|
||||
|
||||
if entry.Key == nil {
|
||||
return completions, nil
|
||||
}
|
||||
|
||||
value := getValueAtCursor(cursor, entry)
|
||||
|
||||
println(fmt.Sprintf("Value: %v", value))
|
||||
|
||||
return completions, nil
|
||||
}
|
||||
|
43
handlers/aliases/handlers/get-value.go
Normal file
43
handlers/aliases/handlers/get-value.go
Normal file
@ -0,0 +1,43 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/aliases/ast"
|
||||
"fmt"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func getValueAtCursor(
|
||||
cursor uint32,
|
||||
entry *ast.AliasEntry,
|
||||
) *ast.AliasValueInterface {
|
||||
if entry.Values == nil || len(entry.Values.Values) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
println(fmt.Sprintf("Values: %v", entry.Values.Values))
|
||||
index, found := slices.BinarySearchFunc(
|
||||
entry.Values.Values,
|
||||
cursor,
|
||||
func(entry ast.AliasValueInterface, pos uint32) int {
|
||||
println(fmt.Sprintf("Entry: %v", entry))
|
||||
value := entry.(ast.AliasValue)
|
||||
|
||||
if value.Location.End.Character > pos {
|
||||
return 1
|
||||
}
|
||||
|
||||
if value.Location.Start.Character < pos {
|
||||
return -1
|
||||
}
|
||||
|
||||
return 0
|
||||
},
|
||||
)
|
||||
|
||||
if !found {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &entry.Values.Values[index]
|
||||
}
|
||||
|
@ -1,12 +1,34 @@
|
||||
package lsp
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/aliases"
|
||||
"config-lsp/handlers/aliases/ast"
|
||||
"config-lsp/handlers/aliases/handlers"
|
||||
|
||||
"github.com/tliron/glsp"
|
||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||
)
|
||||
|
||||
func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionParams) (any, error) {
|
||||
// p := documentParserMap[params.TextDocument.URI]
|
||||
d := aliases.DocumentParserMap[params.TextDocument.URI]
|
||||
cursor := params.Position.Character
|
||||
line := params.Position.Line
|
||||
|
||||
return nil, nil
|
||||
if _, found := d.Parser.CommentLines[line]; found {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
rawEntry, found := d.Parser.Aliases.Get(line)
|
||||
entry := rawEntry.(*ast.AliasEntry)
|
||||
|
||||
if !found {
|
||||
// For the key there are no completions available
|
||||
return handlers.GetAliasesCompletions(d.Indexes), nil
|
||||
}
|
||||
|
||||
return handlers.GetCompletionsForEntry(
|
||||
cursor,
|
||||
entry,
|
||||
d.Indexes,
|
||||
)
|
||||
}
|
||||
|
@ -13,3 +13,4 @@ type AliasesDocument struct {
|
||||
}
|
||||
|
||||
var DocumentParserMap = map[protocol.DocumentUri]*AliasesDocument{}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user