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 package handlers
/*
import ( import (
"config-lsp/common"
docvalues "config-lsp/doc-values" docvalues "config-lsp/doc-values"
"config-lsp/handlers/wireguard"
"config-lsp/handlers/wireguard/ast" "config-lsp/handlers/wireguard/ast"
"config-lsp/handlers/wireguard/fields" "config-lsp/handlers/wireguard/fields"
"fmt" "fmt"
"strings" "strings"
protocol "github.com/tliron/glsp/protocol_3_16"
) )
func getPropertyInfo( func GetPropertyHoverInfo(
p ast.WGProperty, d *wireguard.WGDocument,
cursor uint32,
section ast.WGSection, section ast.WGSection,
) []string { property ast.WGProperty,
if cursor <= p.Key.Location.End { index common.IndexPosition,
options, found := fields.OptionsHeaderMap[*section.Header] ) (*protocol.Hover, error) {
availableOptions, found := fields.OptionsHeaderMap[fields.CreateNormalizedName(section.Header.Name)]
if !found { if !found {
return []string{} return nil, nil
} }
option, found := options[p.Key.Name] option, found := availableOptions[fields.CreateNormalizedName(property.Key.Name)]
if !found { 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 property.Value != nil && property.Value.ContainsPosition(index) {
return &protocol.Hover{
if !found { Contents: protocol.MarkupContent{
return []string{} Kind: protocol.MarkupKindMarkdown,
Value: strings.Join(option.GetTypeDescription(), "\n"),
},
}, nil
} }
if option, found := options[p.Key.Name]; found { return nil, nil
return option.GetTypeDescription()
} }
return []string{} func GetSectionHoverInfo(
} d *wireguard.WGDocument,
section ast.WGSection,
) (*protocol.Hover, error) {
var docValue *docvalues.EnumString = nil
func getSectionInfo(s ast.WGSection) []string { switch section.Header.Name {
if s.Header == nil {
return []string{}
}
contents := []string{
fmt.Sprintf("## [%s]", *s.Header),
"",
}
var option *docvalues.EnumString = nil
switch *s.Header {
case "Interface": case "Interface":
option = &fields.HeaderInterfaceEnum docValue = &fields.HeaderInterfaceEnum
case "Peer": case "Peer":
option = &fields.HeaderPeerEnum docValue = &fields.HeaderPeerEnum
} }
if option == nil { if docValue == nil {
return contents return nil, nil
} }
contents = append(contents, strings.Split(option.Documentation, "\n")...) return &protocol.Hover{
Contents: protocol.MarkupContent{
return contents 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 package lsp
import ( import (
"config-lsp/common"
"config-lsp/handlers/wireguard"
"config-lsp/handlers/wireguard/handlers"
"github.com/tliron/glsp" "github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16" protocol "github.com/tliron/glsp/protocol_3_16"
) )
@ -9,32 +13,29 @@ func TextDocumentHover(
context *glsp.Context, context *glsp.Context,
params *protocol.HoverParams, params *protocol.HoverParams,
) (*protocol.Hover, error) { ) (*protocol.Hover, error) {
/* d := wireguard.DocumentParserMap[params.TextDocument.URI]
p := documentParserMap[params.TextDocument.URI] line := params.Position.Line
switch p.GetTypeByLine(params.Position.Line) { section := d.Config.FindSectionByLine(line)
case parser.LineTypeComment: property := d.Config.FindPropertyByLine(line)
return nil, nil
case parser.LineTypeEmpty: index := common.LSPCharacterAsIndexPosition(params.Position.Character)
return nil, nil
case parser.LineTypeHeader: if property != nil && section != nil {
fallthrough return handlers.GetPropertyHoverInfo(
case parser.LineTypeProperty: d,
documentation := handlers.GetHoverContent( *section,
*p, *property,
params.Position.Line, index,
params.Position.Character,
) )
}
hover := protocol.Hover{ if section != nil && section.Start.Line == line {
Contents: protocol.MarkupContent{ return handlers.GetSectionHoverInfo(
Kind: protocol.MarkupKindMarkdown, d,
Value: strings.Join(documentation, "\n"), *section,
}, )
} }
return &hover, nil
}
*/
return nil, nil return nil, nil
} }