feat(ssh_config): Add hover support

This commit is contained in:
Myzel394 2024-10-03 14:27:05 +02:00
parent 614463d429
commit 413d919719
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
2 changed files with 80 additions and 1 deletions

View File

@ -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
}

View File

@ -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) {
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,
)
}