From 795ce32e8989e19dfc05556f91f1dad268cb1c67 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 21 Sep 2024 14:20:46 +0200 Subject: [PATCH] refactor(hosts): Migrating cursor to index position --- handlers/hosts/analyzer/handler_test.go | 4 ++-- handlers/hosts/handlers/hover.go | 11 ++++++----- handlers/hosts/lsp/text-document-hover.go | 20 ++++++++++++++------ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/handlers/hosts/analyzer/handler_test.go b/handlers/hosts/analyzer/handler_test.go index 4705c95..b7182a2 100644 --- a/handlers/hosts/analyzer/handler_test.go +++ b/handlers/hosts/analyzer/handler_test.go @@ -51,7 +51,7 @@ func TestValidSimpleExampleWorks( t.Errorf("Expected start to be 0, but got %v", entry.Location.Start) } - if !(entry.Location.End.Character == 16) { + if !(entry.Location.End.Character == 17) { t.Errorf("Expected end to be 17, but got %v", entry.Location.End.Character) } @@ -63,7 +63,7 @@ func TestValidSimpleExampleWorks( t.Errorf("Expected IP address start to be 0, but got %v", entry.IPAddress.Location.Start.Character) } - if !(entry.IPAddress.Location.End.Character == 6) { + if !(entry.IPAddress.Location.End.Character == 7) { t.Errorf("Expected IP address end to be 7, but got %v", entry.IPAddress.Location.End.Character) } diff --git a/handlers/hosts/handlers/hover.go b/handlers/hosts/handlers/hover.go index 50cc9cd..52b330b 100644 --- a/handlers/hosts/handlers/hover.go +++ b/handlers/hosts/handlers/hover.go @@ -1,6 +1,7 @@ package handlers import ( + "config-lsp/common" "config-lsp/handlers/hosts" "config-lsp/handlers/hosts/ast" "fmt" @@ -15,21 +16,21 @@ const ( ) func GetHoverTargetInEntry( + index common.IndexPosition, e ast.HostsEntry, - cursor uint32, ) *HoverTarget { - if e.IPAddress != nil && e.IPAddress.Location.ContainsCursorByCharacter(cursor) { + if e.IPAddress != nil && e.IPAddress.Location.ContainsPosition(index) { target := HoverTargetIPAddress return &target } - if e.Hostname != nil && e.Hostname.Location.ContainsCursorByCharacter(cursor) { + if e.Hostname != nil && e.Hostname.Location.ContainsPosition(index) { target := HoverTargetHostname return &target } for _, alias := range e.Aliases { - if alias.Location.ContainsCursorByCharacter(cursor) { + if alias.Location.ContainsPosition(index) { target := HoverTargetAlias return &target } @@ -39,9 +40,9 @@ func GetHoverTargetInEntry( } func GetHoverInfoForHostname( + index common.IndexPosition, d hosts.HostsDocument, hostname ast.HostsHostname, - cursor uint32, ) []string { ipAddress := d.Indexes.Resolver.Entries[hostname.Value] diff --git a/handlers/hosts/lsp/text-document-hover.go b/handlers/hosts/lsp/text-document-hover.go index 53ceb61..ca2530b 100644 --- a/handlers/hosts/lsp/text-document-hover.go +++ b/handlers/hosts/lsp/text-document-hover.go @@ -1,6 +1,7 @@ package lsp import ( + "config-lsp/common" "config-lsp/handlers/hosts" "config-lsp/handlers/hosts/ast" "config-lsp/handlers/hosts/fields" @@ -18,7 +19,7 @@ func TextDocumentHover( document := hosts.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 @@ -33,14 +34,15 @@ func TextDocumentHover( } entry := rawEntry.(*ast.HostsEntry) - target := handlers.GetHoverTargetInEntry(*entry, character) + target := handlers.GetHoverTargetInEntry(index, *entry) var hostname *ast.HostsHostname switch *target { case handlers.HoverTargetIPAddress: - relativeCursor := character - entry.IPAddress.Location.Start.Character - hover := fields.IPAddressField.FetchHoverInfo(entry.IPAddress.Value.String(), relativeCursor) + line := entry.IPAddress.Value.String() + relativeCursor := uint32(entry.IPAddress.Location.Start.GetRelativeIndexPosition(index)) + hover := fields.IPAddressField.FetchHoverInfo(line, relativeCursor) return &protocol.Hover{ Contents: hover, @@ -49,7 +51,7 @@ func TextDocumentHover( hostname = entry.Hostname case handlers.HoverTargetAlias: for _, alias := range entry.Aliases { - if character >= alias.Location.Start.Character && character <= alias.Location.End.Character { + if alias.Location.ContainsPosition(index) { hostname = alias break } @@ -76,7 +78,13 @@ func TextDocumentHover( ) contents = append( contents, - handlers.GetHoverInfoForHostname(*document, *hostname, character)..., + []string{ + "", + }..., + ) + contents = append( + contents, + handlers.GetHoverInfoForHostname(index, *document, *hostname)..., ) return &protocol.Hover{