From ff08c3c5b1d9f8586b606d6d7e38b534a9d8b030 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 21 Sep 2024 13:30:56 +0200 Subject: [PATCH] refactor(aliases): Migrating cursor to index position --- handlers/aliases/ast/parser_test.go | 4 ++-- handlers/aliases/handlers/completions.go | 18 +++++------------- handlers/aliases/handlers/get-value.go | 19 ++++++++++--------- handlers/aliases/handlers/signature_help.go | 8 +++++--- .../aliases/lsp/text-document-completion.go | 9 +++++---- .../aliases/lsp/text-document-definition.go | 7 ++++--- handlers/aliases/lsp/text-document-hover.go | 9 +++++---- .../lsp/text-document-prepare-rename.go | 9 +++++---- handlers/aliases/lsp/text-document-rename.go | 9 +++++---- .../lsp/text-document-signature-help.go | 10 +++++----- 10 files changed, 51 insertions(+), 51 deletions(-) diff --git a/handlers/aliases/ast/parser_test.go b/handlers/aliases/ast/parser_test.go index 17351ea..9ad9165 100644 --- a/handlers/aliases/ast/parser_test.go +++ b/handlers/aliases/ast/parser_test.go @@ -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) } } diff --git a/handlers/aliases/handlers/completions.go b/handlers/aliases/handlers/completions.go index 246c219..b7d43a7 100644 --- a/handlers/aliases/handlers/completions.go +++ b/handlers/aliases/handlers/completions.go @@ -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) diff --git a/handlers/aliases/handlers/get-value.go b/handlers/aliases/handlers/get-value.go index d9c0d12..ce6219b 100644 --- a/handlers/aliases/handlers/get-value.go +++ b/handlers/aliases/handlers/get-value.go @@ -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 diff --git a/handlers/aliases/handlers/signature_help.go b/handlers/aliases/handlers/signature_help.go index 7da7f06..b85bf73 100644 --- a/handlers/aliases/handlers/signature_help.go +++ b/handlers/aliases/handlers/signature_help.go @@ -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 diff --git a/handlers/aliases/lsp/text-document-completion.go b/handlers/aliases/lsp/text-document-completion.go index 0240c8f..1d5d429 100644 --- a/handlers/aliases/lsp/text-document-completion.go +++ b/handlers/aliases/lsp/text-document-completion.go @@ -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, diff --git a/handlers/aliases/lsp/text-document-definition.go b/handlers/aliases/lsp/text-document-definition.go index 4798e16..5976f5d 100644 --- a/handlers/aliases/lsp/text-document-definition.go +++ b/handlers/aliases/lsp/text-document-definition.go @@ -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 diff --git a/handlers/aliases/lsp/text-document-hover.go b/handlers/aliases/lsp/text-document-hover.go index 51acd2c..a1c97fd 100644 --- a/handlers/aliases/lsp/text-document-hover.go +++ b/handlers/aliases/lsp/text-document-hover.go @@ -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 diff --git a/handlers/aliases/lsp/text-document-prepare-rename.go b/handlers/aliases/lsp/text-document-prepare-rename.go index 2b2d03e..56e2dd1 100644 --- a/handlers/aliases/lsp/text-document-prepare-rename.go +++ b/handlers/aliases/lsp/text-document-prepare-rename.go @@ -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 diff --git a/handlers/aliases/lsp/text-document-rename.go b/handlers/aliases/lsp/text-document-rename.go index da53577..d51f092 100644 --- a/handlers/aliases/lsp/text-document-rename.go +++ b/handlers/aliases/lsp/text-document-rename.go @@ -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 diff --git a/handlers/aliases/lsp/text-document-signature-help.go b/handlers/aliases/lsp/text-document-signature-help.go index 6408f50..913fe62 100644 --- a/handlers/aliases/lsp/text-document-signature-help.go +++ b/handlers/aliases/lsp/text-document-signature-help.go @@ -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