From 4e183d543c6c388b291fb409dcb913e2698c650f Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Fri, 18 Oct 2024 19:10:24 +0200 Subject: [PATCH] feat(hosts): Add signature help; Improvements --- .../handlers/hosts/handlers/signature_help.go | 58 +++++++++++++++++++ .../hosts/lsp/text-document-signature.go | 31 ++++++++++ .../ssh_config/handlers/signature_help.go | 9 +-- .../text-document-signature-help.go | 3 +- 4 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 server/handlers/hosts/handlers/signature_help.go create mode 100644 server/handlers/hosts/lsp/text-document-signature.go diff --git a/server/handlers/hosts/handlers/signature_help.go b/server/handlers/hosts/handlers/signature_help.go new file mode 100644 index 0000000..1a77dec --- /dev/null +++ b/server/handlers/hosts/handlers/signature_help.go @@ -0,0 +1,58 @@ +package handlers + +import ( + "config-lsp/common" + "config-lsp/handlers/hosts/ast" + + protocol "github.com/tliron/glsp/protocol_3_16" +) + +func GetEntrySignatureHelp( + entry *ast.HostsEntry, + cursor common.CursorPosition, +) *protocol.SignatureHelp { + var index uint32 + + if entry == nil || entry.IPAddress == nil || entry.IPAddress.Location.ContainsPosition(cursor) { + index = 0 + } else if entry.Hostname == nil || entry.Hostname.Location.ContainsPosition(cursor) { + index = 1 + } else { + index = 2 + } + + signature := uint32(0) + + return &protocol.SignatureHelp{ + ActiveSignature: &signature, + Signatures: []protocol.SignatureInformation{ + { + Label: " [...]", + ActiveParameter: &index, + Parameters: []protocol.ParameterInformation{ + { + Label: []uint32{ + 0, + uint32(len("")), + }, + Documentation: "The ip address to forward to", + }, + { + Label: []uint32{ + uint32(len("")), + uint32(len(" ") + len("")), + }, + Documentation: "The hostname to forward to", + }, + { + Label: []uint32{ + uint32(len(" ") + len("")), + uint32(len(" ") + len(" ") + len("[...]")), + }, + Documentation: "An optional list of aliases that can also forward", + }, + }, + }, + }, + } +} diff --git a/server/handlers/hosts/lsp/text-document-signature.go b/server/handlers/hosts/lsp/text-document-signature.go new file mode 100644 index 0000000..e79efb3 --- /dev/null +++ b/server/handlers/hosts/lsp/text-document-signature.go @@ -0,0 +1,31 @@ +package lsp + +import ( + "config-lsp/common" + "config-lsp/handlers/hosts" + "config-lsp/handlers/hosts/ast" + "config-lsp/handlers/hosts/handlers" + + "github.com/tliron/glsp" + protocol "github.com/tliron/glsp/protocol_3_16" +) + +func TextDocumentSignatureHelp(context *glsp.Context, params *protocol.SignatureHelpParams) (*protocol.SignatureHelp, error) { + document := hosts.DocumentParserMap[params.TextDocument.URI] + + line := uint32(params.Position.Line) + cursor := common.LSPCharacterAsCursorPosition(params.Position.Character) + + if _, found := document.Parser.CommentLines[line]; found { + // Comment + return nil, nil + } + + entry, found := document.Parser.Tree.Entries.Get(line) + + if !found { + return handlers.GetEntrySignatureHelp(nil, cursor), nil + } else { + return handlers.GetEntrySignatureHelp(entry.(*ast.HostsEntry), cursor), nil + } +} diff --git a/server/handlers/ssh_config/handlers/signature_help.go b/server/handlers/ssh_config/handlers/signature_help.go index 5bdacbb..f93ca93 100644 --- a/server/handlers/ssh_config/handlers/signature_help.go +++ b/server/handlers/ssh_config/handlers/signature_help.go @@ -20,6 +20,7 @@ func GetOptionSignatureHelp( } signature := uint32(0) + return &protocol.SignatureHelp{ ActiveSignature: &signature, Signatures: []protocol.SignatureInformation{ @@ -37,7 +38,7 @@ func GetOptionSignatureHelp( { Label: []uint32{ uint32(len("