fix: Improve completions

This commit is contained in:
Myzel394 2024-07-29 23:52:56 +02:00
parent 56b661db72
commit 267a0ff604
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
4 changed files with 20 additions and 32 deletions

View File

@ -111,8 +111,6 @@ func (v ArrayValue) GetTypeDescription() []string {
func (v ArrayValue) CheckIsValid(value string) error { func (v ArrayValue) CheckIsValid(value string) error {
values := strings.Split(value, v.Separator) values := strings.Split(value, v.Separator)
println(fmt.Sprintf("values: %v", values))
if v.DuplicatesExtractor != nil { if v.DuplicatesExtractor != nil {
valuesOccurrences := SliceToMap( valuesOccurrences := SliceToMap(
Map(values, *v.DuplicatesExtractor), Map(values, *v.DuplicatesExtractor),
@ -311,6 +309,10 @@ func (v KeyValueAssignmentValue) GetTypeDescription() []string {
func (v KeyValueAssignmentValue) CheckIsValid(value string) error { func (v KeyValueAssignmentValue) CheckIsValid(value string) error {
parts := strings.Split(value, v.Separator) parts := strings.Split(value, v.Separator)
if len(parts) == 1 && parts[0] == "" {
return nil
}
if len(parts) != 2 { if len(parts) != 2 {
return KeyValueAssignmentError{} return KeyValueAssignmentError{}
} }

View File

@ -14,13 +14,6 @@ type SimpleConfigLine struct {
Position SimpleConfigPosition Position SimpleConfigPosition
} }
func (l SimpleConfigLine) IsCursorAtRootOption(cursor int) bool {
if cursor <= len(l.Value) {
return true
}
return false
}
type SimpleConfigOptions struct { type SimpleConfigOptions struct {
Separator string Separator string

View File

@ -101,18 +101,10 @@ func IsPathFile(path string) bool {
return err == nil && !info.IsDir() return err == nil && !info.IsDir()
} }
func OffsetLineAtLeft(offset uint32, line string, cursor uint32) (string, uint32) { func FindPreviousCharacter(line string, character string, startIndex int) (int, bool) {
if offset >= uint32(len(line)) { for index := startIndex; index >= 0; index-- {
return line, cursor
}
return line[offset:], cursor - offset
}
func FindPreviousCharacter(line string, character string) (uint32, bool) {
for index := len(line) - 1; index >= 0; index-- {
if string(line[index]) == character { if string(line[index]) == character {
return uint32(index), true return index, true
} }
} }

View File

@ -3,7 +3,6 @@ package openssh
import ( import (
"config-lsp/common" "config-lsp/common"
"errors" "errors"
"unicode/utf8"
"github.com/tliron/glsp" "github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16" protocol "github.com/tliron/glsp/protocol_3_16"
@ -16,16 +15,12 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
optionName, line, err := Parser.FindByLineNumber(uint32(params.Position.Line)) optionName, line, err := Parser.FindByLineNumber(uint32(params.Position.Line))
if err == nil { if err == nil {
if line.IsCursorAtRootOption(int(params.Position.Character)) { if params.Position.Character < uint32(len(optionName)) {
return getRootCompletions(), nil return getRootCompletions(), nil
} else { } else {
rawLine, cursor := common.OffsetLineAtLeft( cursor := params.Position.Character - uint32(len(optionName + Parser.Options.Separator))
uint32(utf8.RuneCountInString(optionName + " ")),
line.Value,
params.Position.Character,
)
return getOptionCompletions(optionName, rawLine, cursor), nil return getOptionCompletions(optionName, line.Value, cursor), nil
} }
} else if errors.Is(err, common.LineNotFoundError{}) { } else if errors.Is(err, common.LineNotFoundError{}) {
return getRootCompletions(), nil return getRootCompletions(), nil
@ -84,10 +79,11 @@ func getCompletionsFromValue(requiredValue common.Value, line string, cursor uin
return getCompletionsFromValue(val, line, cursor) return getCompletionsFromValue(val, line, cursor)
case common.ArrayValue: case common.ArrayValue:
arrayValue := requiredValue.(common.ArrayValue) arrayValue := requiredValue.(common.ArrayValue)
relativePosition, found := common.FindPreviousCharacter(line, arrayValue.Separator) relativePosition, found := common.FindPreviousCharacter(line, arrayValue.Separator, int(cursor - 1))
if found { if found {
line, cursor = common.OffsetLineAtLeft(relativePosition, line, cursor) line = line[uint32(relativePosition):]
cursor -= uint32(relativePosition)
} }
return getCompletionsFromValue(arrayValue.SubValue, line, cursor) return getCompletionsFromValue(arrayValue.SubValue, line, cursor)
@ -108,13 +104,18 @@ func getCompletionsFromValue(requiredValue common.Value, line string, cursor uin
case common.KeyValueAssignmentValue: case common.KeyValueAssignmentValue:
keyValueAssignmentValue := requiredValue.(common.KeyValueAssignmentValue) keyValueAssignmentValue := requiredValue.(common.KeyValueAssignmentValue)
relativePosition, found := common.FindPreviousCharacter(line, keyValueAssignmentValue.Separator) println("keyLine", line, "cursor", cursor)
relativePosition, found := common.FindPreviousCharacter(line, keyValueAssignmentValue.Separator, int(cursor - 1))
println("relativePosition", relativePosition)
if found { if found {
line, cursor = common.OffsetLineAtLeft(relativePosition, line, cursor) line = line[uint32(relativePosition):]
cursor -= uint32(relativePosition)
return getCompletionsFromValue(keyValueAssignmentValue.Value, line, cursor) return getCompletionsFromValue(keyValueAssignmentValue.Value, line, cursor)
} else { } else {
println("giving key")
return getCompletionsFromValue(keyValueAssignmentValue.Key, line, cursor) return getCompletionsFromValue(keyValueAssignmentValue.Key, line, cursor)
} }
} }