feat(wireguard): Add hover support

This commit is contained in:
Myzel394 2024-08-18 18:47:17 +02:00
parent 2a809a7d4c
commit 6bbf83c794
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
4 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,95 @@
package wireguard
import (
docvalues "config-lsp/doc-values"
"fmt"
"strings"
)
func (p wireguardProperty) getHoverInfo(cursor uint32, section *wireguardSection) []string {
if cursor <= p.Key.Location.End {
options, found := optionsHeaderMap[*section.Name]
if !found {
return []string{}
}
option, found := options[p.Key.Name]
if !found {
return []string{}
}
return strings.Split(option.Documentation, "\n")
}
options, found := optionsHeaderMap[*section.Name]
if !found {
return []string{}
}
if option, found := options[p.Key.Name]; found {
return option.GetTypeDescription()
}
return []string{}
}
func (p wireguardParser) getHeaderInfo(line uint32, cursor uint32) []string {
section := p.getSectionByLine(line)
if section == nil {
return []string{}
}
sectionInfo := section.getHeaderInfo()
property, _ := section.findProperty(line)
if property == nil {
return sectionInfo
}
propertyInfo := property.getHoverInfo(cursor, section)
if len(propertyInfo) == 0 {
return sectionInfo
}
contents := append(sectionInfo, []string{
"",
fmt.Sprintf("### %s", property.Key.Name),
}...)
contents = append(contents, propertyInfo...)
return contents
}
func (p wireguardSection) getHeaderInfo() []string {
if p.Name == nil {
return []string{}
}
contents := []string{
fmt.Sprintf("## [%s]", *p.Name),
"",
}
var option *docvalues.EnumString = nil
switch *p.Name {
case "Interface":
option = &headerInterfaceEnum
case "Peer":
option = &headerPeerEnum
}
if option == nil {
return contents
}
contents = append(contents, strings.Split(option.Documentation, "\n")...)
return contents
}

View File

@ -0,0 +1,38 @@
package wireguard
import (
"strings"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func TextDocumentHover(
context *glsp.Context,
params *protocol.HoverParams,
) (*protocol.Hover, error) {
// cursor := params.Position.Character
parser := documentParserMap[params.TextDocument.URI]
switch parser.getTypeByLine(params.Position.Line) {
case LineTypeComment:
return nil, nil
case LineTypeEmpty:
return nil, nil
case LineTypeHeader:
fallthrough
case LineTypeProperty:
documentation := parser.getHeaderInfo(params.Position.Line, params.Position.Character)
hover := protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: strings.Join(documentation, "\n"),
},
}
return &hover, nil
}
return nil, nil
}

View File

@ -33,6 +33,16 @@ type wireguardParser struct {
LineIndexes map[uint32]wireguardLineIndex
}
func (p *wireguardParser) getSectionByLine(line uint32) *wireguardSection {
for _, section := range p.Sections {
if section.StartLine <= line && section.EndLine >= line {
return section
}
}
return nil
}
// Search for a property by name
// Returns (line number, property)
func (p *wireguardParser) fetchPropertyByName(name string) (*uint32, *wireguardProperty) {

View File

@ -2,6 +2,7 @@ package roothandler
import (
"config-lsp/handlers/fstab"
"config-lsp/handlers/wireguard"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
@ -15,6 +16,8 @@ func TextDocumentHover(context *glsp.Context, params *protocol.HoverParams) (*pr
return fstab.TextDocumentHover(context, params)
case LanguageSSHDConfig:
return nil, nil
case LanguageWireguard:
return wireguard.TextDocumentHover(context, params)
}
panic("root-handler/TextDocumentHover: unexpected language" + language)