From 413d919719a57304a0eee0e612052ece3c8fd2f0 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Thu, 3 Oct 2024 14:27:05 +0200 Subject: [PATCH] feat(ssh_config): Add hover support --- handlers/ssh_config/handlers/hover.go | 59 +++++++++++++++++++ .../ssh_config/lsp/text-document-hover.go | 22 ++++++- 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 handlers/ssh_config/handlers/hover.go diff --git a/handlers/ssh_config/handlers/hover.go b/handlers/ssh_config/handlers/hover.go new file mode 100644 index 0000000..42839ea --- /dev/null +++ b/handlers/ssh_config/handlers/hover.go @@ -0,0 +1,59 @@ +package handlers + +import ( + "config-lsp/common" + "config-lsp/handlers/ssh_config/ast" + "config-lsp/handlers/ssh_config/fields" + "strings" + + protocol "github.com/tliron/glsp/protocol_3_16" +) + +func GetHoverInfoForOption( + option *ast.SSHOption, + line uint32, + index common.IndexPosition, +) (*protocol.Hover, error) { + docValue, found := fields.Options[option.Key.Key] + + if !found { + return nil, nil + } + + if option.Key.ContainsPosition(index) { + optionName := fields.FieldsNameFormattedMap[option.Key.Key] + contents := []string{ + "## " + optionName, + "", + } + contents = append(contents, docValue.Documentation) + contents = append(contents, []string{ + "", + "---", + "", + }...) + contents = append(contents, []string{ + "### Type", + "", + }...) + contents = append(contents, docValue.GetTypeDescription()...) + + return &protocol.Hover{ + Contents: strings.Join(contents, "\n"), + }, nil + } + + if option.OptionValue != nil && option.OptionValue.ContainsPosition(index) { + line := option.OptionValue.Value.Raw + contents := docValue.DeprecatedFetchHoverInfo( + line, + uint32(option.OptionValue.Start.GetRelativeIndexPosition(index)), + ) + + return &protocol.Hover{ + Contents: strings.Join(contents, "\n"), + }, nil + } + + return nil, nil +} diff --git a/handlers/ssh_config/lsp/text-document-hover.go b/handlers/ssh_config/lsp/text-document-hover.go index 61d19cd..789dbbd 100644 --- a/handlers/ssh_config/lsp/text-document-hover.go +++ b/handlers/ssh_config/lsp/text-document-hover.go @@ -1,6 +1,10 @@ package lsp import ( + "config-lsp/common" + sshconfig "config-lsp/handlers/ssh_config" + "config-lsp/handlers/ssh_config/handlers" + "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) @@ -9,5 +13,21 @@ func TextDocumentHover( context *glsp.Context, params *protocol.HoverParams, ) (*protocol.Hover, error) { - return nil, nil + line := params.Position.Line + index := common.LSPCharacterAsIndexPosition(params.Position.Character) + + d := sshconfig.DocumentParserMap[params.TextDocument.URI] + + option, _ := d.Config.FindOption(line) + + if option == nil || option.Key == nil { + // Empty line + return nil, nil + } + + return handlers.GetHoverInfoForOption( + option, + line, + index, + ) }