diff --git a/server/handlers/wireguard/handlers/hover.go b/server/handlers/wireguard/handlers/hover.go index 8d652d1..c0765f4 100644 --- a/server/handlers/wireguard/handlers/hover.go +++ b/server/handlers/wireguard/handlers/hover.go @@ -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] - - if !found { - return []string{} - } - - option, found := options[p.Key.Name] - - if !found { - return []string{} - } - - return strings.Split(option.Documentation, "\n") - } - - 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 } - if option, found := options[p.Key.Name]; found { - return option.GetTypeDescription() + option, found := availableOptions[fields.CreateNormalizedName(property.Key.Name)] + + if !found { + return nil, nil } - return []string{} + if property.Key.ContainsPosition(index) { + return &protocol.Hover{ + Contents: protocol.MarkupContent{ + Kind: protocol.MarkupKindMarkdown, + Value: option.Documentation, + }, + }, nil + } + + if property.Value != nil && property.Value.ContainsPosition(index) { + return &protocol.Hover{ + Contents: protocol.MarkupContent{ + Kind: protocol.MarkupKindMarkdown, + Value: strings.Join(option.GetTypeDescription(), "\n"), + }, + }, nil + } + + return nil, nil } -func getSectionInfo(s ast.WGSection) []string { - if s.Header == nil { - return []string{} - } +func GetSectionHoverInfo( + d *wireguard.WGDocument, + section ast.WGSection, +) (*protocol.Hover, error) { + var docValue *docvalues.EnumString = nil - 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 -} -*/ diff --git a/server/handlers/wireguard/lsp/text-document-hover.go b/server/handlers/wireguard/lsp/text-document-hover.go index 1f2945f..a1b4d9b 100644 --- a/server/handlers/wireguard/lsp/text-document-hover.go +++ b/server/handlers/wireguard/lsp/text-document-hover.go @@ -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) - hover := protocol.Hover{ - Contents: protocol.MarkupContent{ - Kind: protocol.MarkupKindMarkdown, - Value: strings.Join(documentation, "\n"), - }, - } - return &hover, nil - } - */ + index := common.LSPCharacterAsIndexPosition(params.Position.Character) + + if property != nil && section != nil { + return handlers.GetPropertyHoverInfo( + d, + *section, + *property, + index, + ) + } + + if section != nil && section.Start.Line == line { + return handlers.GetSectionHoverInfo( + d, + *section, + ) + } return nil, nil }