fix(server): Improve wireguard hover lsp

Signed-off-by: Myzel394 <github.7a2op@simplelogin.co>
This commit is contained in:
Myzel394 2025-03-09 20:10:50 +01:00 committed by Myzel394
parent e14866bcdc
commit b94d987565
No known key found for this signature in database
GPG Key ID: B603E877F73D4ABB
2 changed files with 79 additions and 104 deletions

View File

@ -1,107 +1,81 @@
package handlers
/*
import (
"config-lsp/common"
docvalues "config-lsp/doc-values"
"config-lsp/handlers/wireguard"
"config-lsp/handlers/wireguard/ast"
"config-lsp/handlers/wireguard/fields"
"fmt"
"strings"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func getPropertyInfo(
p ast.WGProperty,
cursor uint32,
func GetPropertyHoverInfo(
d *wireguard.WGDocument,
section ast.WGSection,
) []string {
if cursor <= p.Key.Location.End {
options, found := fields.OptionsHeaderMap[*section.Header]
property ast.WGProperty,
index common.IndexPosition,
) (*protocol.Hover, error) {
availableOptions, found := fields.OptionsHeaderMap[fields.CreateNormalizedName(section.Header.Name)]
if !found {
return []string{}
return nil, nil
}
option, found := options[p.Key.Name]
option, found := availableOptions[fields.CreateNormalizedName(property.Key.Name)]
if !found {
return []string{}
return nil, nil
}
return strings.Split(option.Documentation, "\n")
if property.Key.ContainsPosition(index) {
return &protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: option.Documentation,
},
}, nil
}
options, found := fields.OptionsHeaderMap[*section.Header]
if !found {
return []string{}
if property.Value != nil && property.Value.ContainsPosition(index) {
return &protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: strings.Join(option.GetTypeDescription(), "\n"),
},
}, nil
}
if option, found := options[p.Key.Name]; found {
return option.GetTypeDescription()
return nil, nil
}
return []string{}
}
func GetSectionHoverInfo(
d *wireguard.WGDocument,
section ast.WGSection,
) (*protocol.Hover, error) {
var docValue *docvalues.EnumString = nil
func getSectionInfo(s ast.WGSection) []string {
if s.Header == nil {
return []string{}
}
contents := []string{
fmt.Sprintf("## [%s]", *s.Header),
"",
}
var option *docvalues.EnumString = nil
switch *s.Header {
switch section.Header.Name {
case "Interface":
option = &fields.HeaderInterfaceEnum
docValue = &fields.HeaderInterfaceEnum
case "Peer":
option = &fields.HeaderPeerEnum
docValue = &fields.HeaderPeerEnum
}
if option == nil {
return contents
if docValue == nil {
return nil, nil
}
contents = append(contents, strings.Split(option.Documentation, "\n")...)
return contents
return &protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: fmt.Sprintf(
"## [%s]\n\n%s",
section.Header.Name,
docValue.Documentation,
),
},
}, nil
}
func GetHoverContent(
p ast.WGConfig,
line uint32,
cursor uint32,
) []string {
section := p.GetSectionByLine(line)
if section == nil {
return []string{}
}
sectionInfo := getSectionInfo(*section)
property, _ := section.GetPropertyByLine(line)
if property == nil {
return sectionInfo
}
propertyInfo := getPropertyInfo(*property, cursor, *section)
if len(propertyInfo) == 0 {
return sectionInfo
}
contents := append(sectionInfo, []string{
"",
fmt.Sprintf("### %s", property.Key.Name),
}...)
contents = append(contents, propertyInfo...)
return contents
}
*/

View File

@ -1,6 +1,10 @@
package lsp
import (
"config-lsp/common"
"config-lsp/handlers/wireguard"
"config-lsp/handlers/wireguard/handlers"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
@ -9,32 +13,29 @@ func TextDocumentHover(
context *glsp.Context,
params *protocol.HoverParams,
) (*protocol.Hover, error) {
/*
p := documentParserMap[params.TextDocument.URI]
d := wireguard.DocumentParserMap[params.TextDocument.URI]
line := params.Position.Line
switch p.GetTypeByLine(params.Position.Line) {
case parser.LineTypeComment:
return nil, nil
case parser.LineTypeEmpty:
return nil, nil
case parser.LineTypeHeader:
fallthrough
case parser.LineTypeProperty:
documentation := handlers.GetHoverContent(
*p,
params.Position.Line,
params.Position.Character,
section := d.Config.FindSectionByLine(line)
property := d.Config.FindPropertyByLine(line)
index := common.LSPCharacterAsIndexPosition(params.Position.Character)
if property != nil && section != nil {
return handlers.GetPropertyHoverInfo(
d,
*section,
*property,
index,
)
}
hover := protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: strings.Join(documentation, "\n"),
},
if section != nil && section.Start.Line == line {
return handlers.GetSectionHoverInfo(
d,
*section,
)
}
return &hover, nil
}
*/
return nil, nil
}