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)
}
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)
}

View File

@ -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]

View File

@ -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{