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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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