refactor(aliases): Migrating cursor to index position

This commit is contained in:
Myzel394 2024-09-21 13:30:56 +02:00
parent 3cff613620
commit ff08c3c5b1
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
10 changed files with 51 additions and 51 deletions

View File

@ -105,11 +105,11 @@ luke: :include:/etc/other_aliases
t.Fatalf("Expected path to be '/etc/other_aliases', got %v", includeValue.Path.Path)
}
if !(includeValue.Location.Start.Character == 6 && includeValue.Location.End.Character == 32) {
if !(includeValue.Location.Start.Character == 6 && includeValue.Location.End.Character == 33) {
t.Fatalf("Expected location to be 6-33, got %v-%v", includeValue.Location.Start.Character, includeValue.Location.End.Character)
}
if !(includeValue.Path.Location.Start.Character == 15 && includeValue.Path.Location.End.Character == 32) {
if !(includeValue.Path.Location.Start.Character == 15 && includeValue.Path.Location.End.Character == 33) {
t.Fatalf("Expected path location to be 15-33, got %v-%v", includeValue.Path.Location.Start.Character, includeValue.Path.Location.End.Character)
}
}

View File

@ -1,6 +1,7 @@
package handlers
import (
"config-lsp/common"
"config-lsp/handlers/aliases/analyzer"
"config-lsp/handlers/aliases/ast"
"config-lsp/handlers/aliases/fetchers"
@ -38,7 +39,7 @@ func GetAliasesCompletions(
}
func GetCompletionsForEntry(
cursor uint32,
cursor common.CursorPosition,
entry *ast.AliasEntry,
i *indexes.AliasesIndexes,
) ([]protocol.CompletionItem, error) {
@ -48,8 +49,7 @@ func GetCompletionsForEntry(
return completions, nil
}
value := GetValueAtCursor(cursor, entry)
relativeCursor := cursor - entry.Key.Location.Start.Character
value := GetValueAtPosition(cursor, entry)
excludedUsers := getUsersFromEntry(entry)
@ -61,8 +61,6 @@ func GetCompletionsForEntry(
completions = append(completions, getUserCompletions(
i,
excludedUsers,
"",
0,
)...)
return completions, nil
@ -70,21 +68,17 @@ func GetCompletionsForEntry(
switch (*value).(type) {
case ast.AliasValueUser:
userValue := (*value).(ast.AliasValueUser)
return getUserCompletions(
i,
excludedUsers,
userValue.Value,
relativeCursor,
), nil
case ast.AliasValueError:
errorValue := (*value).(ast.AliasValueError)
isAtErrorCode := errorValue.Code == nil &&
relativeCursor >= errorValue.Location.Start.Character &&
errorValue.Location.IsPositionAfterStart(cursor) &&
(errorValue.Message == nil ||
relativeCursor <= errorValue.Message.Location.Start.Character)
errorValue.Message.Location.IsPositionBeforeEnd(cursor))
if isAtErrorCode {
kind := protocol.CompletionItemKindValue
@ -160,8 +154,6 @@ func getErrorCompletion() protocol.CompletionItem {
func getUserCompletions(
i *indexes.AliasesIndexes,
excluded map[string]struct{},
line string,
cursor uint32,
) []protocol.CompletionItem {
users := fetchers.GetAvailableUserValues(i)

View File

@ -1,12 +1,13 @@
package handlers
import (
"config-lsp/common"
"config-lsp/handlers/aliases/ast"
"slices"
)
func GetValueAtCursor(
cursor uint32,
func GetValueAtPosition(
position common.Position,
entry *ast.AliasEntry,
) *ast.AliasValueInterface {
if entry.Values == nil || len(entry.Values.Values) == 0 {
@ -15,16 +16,16 @@ func GetValueAtCursor(
index, found := slices.BinarySearchFunc(
entry.Values.Values,
cursor,
func(current ast.AliasValueInterface, target uint32) int {
value := current.GetAliasValue()
position,
func(rawCurrent ast.AliasValueInterface, target common.Position) int {
current := rawCurrent.GetAliasValue()
if target < value.Location.Start.Character {
return 1
if current.Location.IsPositionAfterEnd(target) {
return -1
}
if target > value.Location.End.Character {
return -1
if current.Location.IsPositionBeforeStart(target) {
return 1
}
return 0

View File

@ -1,6 +1,7 @@
package handlers
import (
"config-lsp/common"
"config-lsp/handlers/aliases/ast"
"strings"
@ -125,8 +126,8 @@ func GetAllValuesSignatureHelp() *protocol.SignatureHelp {
}
func GetValueSignatureHelp(
cursor common.CursorPosition,
value ast.AliasValueInterface,
cursor uint32,
) *protocol.SignatureHelp {
switch value.(type) {
case ast.AliasValueUser:
@ -166,7 +167,8 @@ func GetValueSignatureHelp(
},
}
case ast.AliasValueEmail:
isBeforeAtSymbol := cursor <= uint32(strings.Index(value.GetAliasValue().Value, "@"))
indexPosition := common.LSPCharacterAsIndexPosition(uint32(strings.Index(value.GetAliasValue().Value, "@")))
isBeforeAtSymbol := cursor.IsBeforeIndexPosition(indexPosition)
var index uint32
@ -269,7 +271,7 @@ func GetValueSignatureHelp(
errorValue := value.(ast.AliasValueError)
var index uint32
if errorValue.Code == nil || cursor <= errorValue.Code.Location.End.Character {
if errorValue.Code == nil || errorValue.Code.Location.IsPositionBeforeEnd(cursor) {
index = 1
} else {
index = 2

View File

@ -1,6 +1,7 @@
package lsp
import (
"config-lsp/common"
"config-lsp/handlers/aliases"
"config-lsp/handlers/aliases/ast"
"config-lsp/handlers/aliases/handlers"
@ -11,7 +12,7 @@ import (
func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionParams) (any, error) {
d := aliases.DocumentParserMap[params.TextDocument.URI]
cursor := params.Position.Character
cursor := common.LSPCharacterAsCursorPosition(params.Position.Character)
line := params.Position.Line
if _, found := d.Parser.CommentLines[line]; found {
@ -31,15 +32,15 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
return handlers.GetAliasesCompletions(d.Indexes), nil
}
if cursor >= entry.Key.Location.Start.Character && cursor <= entry.Key.Location.End.Character {
if entry.Key.Location.ContainsPosition(cursor) {
return handlers.GetAliasesCompletions(d.Indexes), nil
}
if entry.Separator == nil && cursor > entry.Key.Location.End.Character {
if entry.Separator == nil && entry.Key.Location.IsPositionBeforeEnd(cursor) {
return nil, nil
}
if cursor > entry.Separator.End.Character {
if entry.Separator.IsPositionBeforeEnd(cursor) {
return handlers.GetCompletionsForEntry(
cursor,
entry,

View File

@ -1,6 +1,7 @@
package lsp
import (
"config-lsp/common"
"config-lsp/handlers/aliases"
"config-lsp/handlers/aliases/ast"
"config-lsp/handlers/aliases/handlers"
@ -11,7 +12,7 @@ import (
func TextDocumentDefinition(context *glsp.Context, params *protocol.DefinitionParams) ([]protocol.Location, error) {
d := aliases.DocumentParserMap[params.TextDocument.URI]
character := params.Position.Character
index := common.LSPCharacterAsIndexPosition(params.Position.Character)
line := params.Position.Line
rawEntry, found := d.Parser.Aliases.Get(line)
@ -22,8 +23,8 @@ func TextDocumentDefinition(context *glsp.Context, params *protocol.DefinitionPa
entry := rawEntry.(*ast.AliasEntry)
if entry.Values != nil && character >= entry.Values.Location.Start.Character && character <= entry.Values.Location.End.Character {
rawValue := handlers.GetValueAtCursor(character, entry)
if entry.Values != nil && entry.Values.Location.ContainsPosition(index) {
rawValue := handlers.GetValueAtPosition(index, entry)
if rawValue == nil {
return nil, nil

View File

@ -1,6 +1,7 @@
package lsp
import (
"config-lsp/common"
"config-lsp/handlers/aliases"
"config-lsp/handlers/aliases/ast"
"config-lsp/handlers/aliases/handlers"
@ -17,7 +18,7 @@ func TextDocumentHover(
document := aliases.DocumentParserMap[params.TextDocument.URI]
line := params.Position.Line
character := params.Position.Character
index := common.LSPCharacterAsIndexPosition(params.Position.Character)
if _, found := document.Parser.CommentLines[line]; found {
// Comment
@ -32,7 +33,7 @@ func TextDocumentHover(
entry := rawEntry.(*ast.AliasEntry)
if entry.Key != nil && character >= entry.Key.Location.Start.Character && character <= entry.Key.Location.End.Character {
if entry.Key != nil && entry.Key.Location.ContainsPosition(index) {
text := handlers.GetAliasHoverInfo(*document.Indexes, *entry)
return &protocol.Hover{
@ -40,8 +41,8 @@ func TextDocumentHover(
}, nil
}
if entry.Values != nil && character >= entry.Values.Location.Start.Character && character <= entry.Values.Location.End.Character {
value := handlers.GetValueAtCursor(character, entry)
if entry.Values != nil && entry.Values.Location.ContainsPosition(index) {
value := handlers.GetValueAtPosition(index, entry)
if value == nil {
return nil, nil

View File

@ -1,6 +1,7 @@
package lsp
import (
"config-lsp/common"
"config-lsp/handlers/aliases"
"config-lsp/handlers/aliases/ast"
"config-lsp/handlers/aliases/handlers"
@ -11,7 +12,7 @@ import (
func TextDocumentPrepareRename(context *glsp.Context, params *protocol.PrepareRenameParams) (any, error) {
d := aliases.DocumentParserMap[params.TextDocument.URI]
character := params.Position.Character
index := common.LSPCharacterAsIndexPosition(params.Position.Character)
line := params.Position.Line
rawEntry, found := d.Parser.Aliases.Get(line)
@ -22,12 +23,12 @@ func TextDocumentPrepareRename(context *glsp.Context, params *protocol.PrepareRe
entry := rawEntry.(*ast.AliasEntry)
if character >= entry.Key.Location.Start.Character && character <= entry.Key.Location.End.Character {
if entry.Key.Location.ContainsPosition(index) {
return entry.Key.Location.ToLSPRange(), nil
}
if entry.Values != nil && character >= entry.Values.Location.Start.Character && character <= entry.Values.Location.End.Character {
rawValue := handlers.GetValueAtCursor(character, entry)
if entry.Values != nil && entry.Values.Location.ContainsPosition(index) {
rawValue := handlers.GetValueAtPosition(index, entry)
if rawValue == nil {
return nil, nil

View File

@ -1,6 +1,7 @@
package lsp
import (
"config-lsp/common"
"config-lsp/handlers/aliases"
"config-lsp/handlers/aliases/ast"
"config-lsp/handlers/aliases/handlers"
@ -11,7 +12,7 @@ import (
func TextDocumentRename(context *glsp.Context, params *protocol.RenameParams) (*protocol.WorkspaceEdit, error) {
d := aliases.DocumentParserMap[params.TextDocument.URI]
character := params.Position.Character
index := common.LSPCharacterAsIndexPosition(params.Position.Character)
line := params.Position.Line
rawEntry, found := d.Parser.Aliases.Get(line)
@ -22,7 +23,7 @@ func TextDocumentRename(context *glsp.Context, params *protocol.RenameParams) (*
entry := rawEntry.(*ast.AliasEntry)
if character >= entry.Key.Location.Start.Character && character <= entry.Key.Location.End.Character {
if entry.Key.Location.ContainsPosition(index) {
changes := handlers.RenameAlias(
*d.Indexes,
entry.Key.Value,
@ -36,8 +37,8 @@ func TextDocumentRename(context *glsp.Context, params *protocol.RenameParams) (*
}, nil
}
if entry.Values != nil && character >= entry.Values.Location.Start.Character && character <= entry.Values.Location.End.Character {
rawValue := handlers.GetValueAtCursor(character, entry)
if entry.Values != nil && entry.Values.Location.ContainsPosition(index) {
rawValue := handlers.GetValueAtPosition(index, entry)
if rawValue == nil {
return nil, nil

View File

@ -14,7 +14,7 @@ func TextDocumentSignatureHelp(context *glsp.Context, params *protocol.Signature
document := aliases.DocumentParserMap[params.TextDocument.URI]
line := params.Position.Line
character := common.CursorToCharacterIndex(params.Position.Character)
cursor := common.LSPCharacterAsCursorPosition(common.CursorToCharacterIndex(params.Position.Character))
if _, found := document.Parser.CommentLines[line]; found {
// Comment
@ -29,12 +29,12 @@ func TextDocumentSignatureHelp(context *glsp.Context, params *protocol.Signature
entry := rawEntry.(*ast.AliasEntry)
if entry.Key != nil && character >= entry.Key.Location.Start.Character && character <= entry.Key.Location.End.Character {
if entry.Key != nil && entry.Key.Location.ContainsPosition(cursor) {
return handlers.GetRootSignatureHelp(0), nil
}
if entry.Values != nil && character >= entry.Values.Location.Start.Character && character <= entry.Values.Location.End.Character {
value := handlers.GetValueAtCursor(character, entry)
if entry.Values != nil && entry.Values.Location.ContainsPosition(cursor) {
value := handlers.GetValueAtPosition(cursor, entry)
if value == nil {
// For some reason, this does not really work,
@ -46,7 +46,7 @@ func TextDocumentSignatureHelp(context *glsp.Context, params *protocol.Signature
return nil, nil
}
return handlers.GetValueSignatureHelp(*value, character), nil
return handlers.GetValueSignatureHelp(cursor, *value), nil
}
return nil, nil