refactor(hosts): Migrating cursor to index position

This commit is contained in:
Myzel394 2024-09-21 14:20:46 +02:00
parent ff08c3c5b1
commit 795ce32e89
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
3 changed files with 22 additions and 13 deletions

View File

@ -51,7 +51,7 @@ func TestValidSimpleExampleWorks(
t.Errorf("Expected start to be 0, but got %v", entry.Location.Start) 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) 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) 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) t.Errorf("Expected IP address end to be 7, but got %v", entry.IPAddress.Location.End.Character)
} }

View File

@ -1,6 +1,7 @@
package handlers package handlers
import ( import (
"config-lsp/common"
"config-lsp/handlers/hosts" "config-lsp/handlers/hosts"
"config-lsp/handlers/hosts/ast" "config-lsp/handlers/hosts/ast"
"fmt" "fmt"
@ -15,21 +16,21 @@ const (
) )
func GetHoverTargetInEntry( func GetHoverTargetInEntry(
index common.IndexPosition,
e ast.HostsEntry, e ast.HostsEntry,
cursor uint32,
) *HoverTarget { ) *HoverTarget {
if e.IPAddress != nil && e.IPAddress.Location.ContainsCursorByCharacter(cursor) { if e.IPAddress != nil && e.IPAddress.Location.ContainsPosition(index) {
target := HoverTargetIPAddress target := HoverTargetIPAddress
return &target return &target
} }
if e.Hostname != nil && e.Hostname.Location.ContainsCursorByCharacter(cursor) { if e.Hostname != nil && e.Hostname.Location.ContainsPosition(index) {
target := HoverTargetHostname target := HoverTargetHostname
return &target return &target
} }
for _, alias := range e.Aliases { for _, alias := range e.Aliases {
if alias.Location.ContainsCursorByCharacter(cursor) { if alias.Location.ContainsPosition(index) {
target := HoverTargetAlias target := HoverTargetAlias
return &target return &target
} }
@ -39,9 +40,9 @@ func GetHoverTargetInEntry(
} }
func GetHoverInfoForHostname( func GetHoverInfoForHostname(
index common.IndexPosition,
d hosts.HostsDocument, d hosts.HostsDocument,
hostname ast.HostsHostname, hostname ast.HostsHostname,
cursor uint32,
) []string { ) []string {
ipAddress := d.Indexes.Resolver.Entries[hostname.Value] ipAddress := d.Indexes.Resolver.Entries[hostname.Value]

View File

@ -1,6 +1,7 @@
package lsp package lsp
import ( import (
"config-lsp/common"
"config-lsp/handlers/hosts" "config-lsp/handlers/hosts"
"config-lsp/handlers/hosts/ast" "config-lsp/handlers/hosts/ast"
"config-lsp/handlers/hosts/fields" "config-lsp/handlers/hosts/fields"
@ -18,7 +19,7 @@ func TextDocumentHover(
document := hosts.DocumentParserMap[params.TextDocument.URI] document := hosts.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
@ -33,14 +34,15 @@ func TextDocumentHover(
} }
entry := rawEntry.(*ast.HostsEntry) entry := rawEntry.(*ast.HostsEntry)
target := handlers.GetHoverTargetInEntry(*entry, character) target := handlers.GetHoverTargetInEntry(index, *entry)
var hostname *ast.HostsHostname var hostname *ast.HostsHostname
switch *target { switch *target {
case handlers.HoverTargetIPAddress: case handlers.HoverTargetIPAddress:
relativeCursor := character - entry.IPAddress.Location.Start.Character line := entry.IPAddress.Value.String()
hover := fields.IPAddressField.FetchHoverInfo(entry.IPAddress.Value.String(), relativeCursor) relativeCursor := uint32(entry.IPAddress.Location.Start.GetRelativeIndexPosition(index))
hover := fields.IPAddressField.FetchHoverInfo(line, relativeCursor)
return &protocol.Hover{ return &protocol.Hover{
Contents: hover, Contents: hover,
@ -49,7 +51,7 @@ func TextDocumentHover(
hostname = entry.Hostname hostname = entry.Hostname
case handlers.HoverTargetAlias: case handlers.HoverTargetAlias:
for _, alias := range entry.Aliases { for _, alias := range entry.Aliases {
if character >= alias.Location.Start.Character && character <= alias.Location.End.Character { if alias.Location.ContainsPosition(index) {
hostname = alias hostname = alias
break break
} }
@ -76,7 +78,13 @@ func TextDocumentHover(
) )
contents = append( contents = append(
contents, contents,
handlers.GetHoverInfoForHostname(*document, *hostname, character)..., []string{
"",
}...,
)
contents = append(
contents,
handlers.GetHoverInfoForHostname(index, *document, *hostname)...,
) )
return &protocol.Hover{ return &protocol.Hover{