mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
fix: Improve completions
This commit is contained in:
parent
56b661db72
commit
267a0ff604
@ -111,8 +111,6 @@ func (v ArrayValue) GetTypeDescription() []string {
|
||||
func (v ArrayValue) CheckIsValid(value string) error {
|
||||
values := strings.Split(value, v.Separator)
|
||||
|
||||
println(fmt.Sprintf("values: %v", values))
|
||||
|
||||
if v.DuplicatesExtractor != nil {
|
||||
valuesOccurrences := SliceToMap(
|
||||
Map(values, *v.DuplicatesExtractor),
|
||||
@ -311,6 +309,10 @@ func (v KeyValueAssignmentValue) GetTypeDescription() []string {
|
||||
func (v KeyValueAssignmentValue) CheckIsValid(value string) error {
|
||||
parts := strings.Split(value, v.Separator)
|
||||
|
||||
if len(parts) == 1 && parts[0] == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(parts) != 2 {
|
||||
return KeyValueAssignmentError{}
|
||||
}
|
||||
|
@ -14,13 +14,6 @@ type SimpleConfigLine struct {
|
||||
Position SimpleConfigPosition
|
||||
}
|
||||
|
||||
func (l SimpleConfigLine) IsCursorAtRootOption(cursor int) bool {
|
||||
if cursor <= len(l.Value) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
type SimpleConfigOptions struct {
|
||||
Separator string
|
||||
|
@ -101,18 +101,10 @@ func IsPathFile(path string) bool {
|
||||
return err == nil && !info.IsDir()
|
||||
}
|
||||
|
||||
func OffsetLineAtLeft(offset uint32, line string, cursor uint32) (string, uint32) {
|
||||
if offset >= uint32(len(line)) {
|
||||
return line, cursor
|
||||
}
|
||||
|
||||
return line[offset:], cursor - offset
|
||||
}
|
||||
|
||||
func FindPreviousCharacter(line string, character string) (uint32, bool) {
|
||||
for index := len(line) - 1; index >= 0; index-- {
|
||||
func FindPreviousCharacter(line string, character string, startIndex int) (int, bool) {
|
||||
for index := startIndex; index >= 0; index-- {
|
||||
if string(line[index]) == character {
|
||||
return uint32(index), true
|
||||
return index, true
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@ package openssh
|
||||
import (
|
||||
"config-lsp/common"
|
||||
"errors"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/tliron/glsp"
|
||||
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))
|
||||
|
||||
if err == nil {
|
||||
if line.IsCursorAtRootOption(int(params.Position.Character)) {
|
||||
if params.Position.Character < uint32(len(optionName)) {
|
||||
return getRootCompletions(), nil
|
||||
} else {
|
||||
rawLine, cursor := common.OffsetLineAtLeft(
|
||||
uint32(utf8.RuneCountInString(optionName + " ")),
|
||||
line.Value,
|
||||
params.Position.Character,
|
||||
)
|
||||
cursor := params.Position.Character - uint32(len(optionName + Parser.Options.Separator))
|
||||
|
||||
return getOptionCompletions(optionName, rawLine, cursor), nil
|
||||
return getOptionCompletions(optionName, line.Value, cursor), nil
|
||||
}
|
||||
} else if errors.Is(err, common.LineNotFoundError{}) {
|
||||
return getRootCompletions(), nil
|
||||
@ -84,10 +79,11 @@ func getCompletionsFromValue(requiredValue common.Value, line string, cursor uin
|
||||
return getCompletionsFromValue(val, line, cursor)
|
||||
case 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 {
|
||||
line, cursor = common.OffsetLineAtLeft(relativePosition, line, cursor)
|
||||
line = line[uint32(relativePosition):]
|
||||
cursor -= uint32(relativePosition)
|
||||
}
|
||||
|
||||
return getCompletionsFromValue(arrayValue.SubValue, line, cursor)
|
||||
@ -108,13 +104,18 @@ func getCompletionsFromValue(requiredValue common.Value, line string, cursor uin
|
||||
case 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 {
|
||||
line, cursor = common.OffsetLineAtLeft(relativePosition, line, cursor)
|
||||
line = line[uint32(relativePosition):]
|
||||
cursor -= uint32(relativePosition)
|
||||
|
||||
return getCompletionsFromValue(keyValueAssignmentValue.Value, line, cursor)
|
||||
} else {
|
||||
println("giving key")
|
||||
return getCompletionsFromValue(keyValueAssignmentValue.Key, line, cursor)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user