From eda3a0e771f1cc4bcd03556d9a7af8aa840d0572 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:56:42 +0200 Subject: [PATCH] feat(ssh_config): Add completions for tag --- handlers/ssh_config/handlers/completions.go | 7 +++ .../ssh_config/handlers/completions_tag.go | 50 +++++++++++++++++++ handlers/ssh_config/handlers/formatting.go | 1 + .../ssh_config/handlers/formatting_nodes.go | 16 +++--- 4 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 handlers/ssh_config/handlers/completions_tag.go diff --git a/handlers/ssh_config/handlers/completions.go b/handlers/ssh_config/handlers/completions.go index 8f74ca9..15a44f9 100644 --- a/handlers/ssh_config/handlers/completions.go +++ b/handlers/ssh_config/handlers/completions.go @@ -81,6 +81,13 @@ func GetOptionCompletions( matchBlock.MatchValue, ) } + if entry.Key.Key == tagOption { + return getTagCompletions( + d, + cursor, + entry, + ) + } if entry.OptionValue == nil { return option.DeprecatedFetchCompletions("", 0), nil diff --git a/handlers/ssh_config/handlers/completions_tag.go b/handlers/ssh_config/handlers/completions_tag.go new file mode 100644 index 0000000..724dd05 --- /dev/null +++ b/handlers/ssh_config/handlers/completions_tag.go @@ -0,0 +1,50 @@ +package handlers + +import ( + "config-lsp/common" + "config-lsp/common/formatting" + sshconfig "config-lsp/handlers/ssh_config" + "config-lsp/handlers/ssh_config/ast" + "config-lsp/utils" + "fmt" + + protocol "github.com/tliron/glsp/protocol_3_16" +) + +func getTagCompletions( + d *sshconfig.SSHDocument, + cursor common.CursorPosition, + entry *ast.SSHOption, +) ([]protocol.CompletionItem, error) { + return utils.MapMapToSlice( + d.Indexes.Tags, + func(name string, block *ast.SSHMatchBlock) protocol.CompletionItem { + kind := protocol.CompletionItemKindModule + text := renderMatchBlock(block) + return protocol.CompletionItem{ + Label: name, + Kind: &kind, + Documentation: protocol.MarkupContent{ + Kind: protocol.MarkupKindMarkdown, + Value: fmt.Sprintf("```sshconfig\n%s\n```", text), + }, + } + }, + ), nil +} + +func renderMatchBlock( + block *ast.SSHMatchBlock, +) string { + text := "" + + text += "Match " + formatMatchToString(block.MatchValue) + "\n" + + it := block.Options.Iterator() + for it.Next() { + option := it.Value().(*ast.SSHOption) + text += formatOptionToString(option, formatting.DefaultFormattingOptions, blockOptionTemplate) + "\n" + } + + return text +} diff --git a/handlers/ssh_config/handlers/formatting.go b/handlers/ssh_config/handlers/formatting.go index 9fcfbe4..169f325 100644 --- a/handlers/ssh_config/handlers/formatting.go +++ b/handlers/ssh_config/handlers/formatting.go @@ -9,6 +9,7 @@ import ( var hostOption = fields.CreateNormalizedName("Host") var matchOption = fields.CreateNormalizedName("Match") +var tagOption = fields.CreateNormalizedName("Tag") func FormatDocument( d *sshconfig.SSHDocument, diff --git a/handlers/ssh_config/handlers/formatting_nodes.go b/handlers/ssh_config/handlers/formatting_nodes.go index 2ed5ca1..d8a71e9 100644 --- a/handlers/ssh_config/handlers/formatting_nodes.go +++ b/handlers/ssh_config/handlers/formatting_nodes.go @@ -49,7 +49,10 @@ func formatOption( template = blockOptionTemplate } - edits = append(edits, formatSSHOption(option, options, template)...) + edits = append(edits, protocol.TextEdit{ + Range: option.ToLSPRange(), + NewText: formatOptionToString(option, options, template), + }) } return edits @@ -101,11 +104,11 @@ func formatMatchBlock( return edits } -func formatSSHOption( +func formatOptionToString( option *ast.SSHOption, options protocol.FormattingOptions, template formatting.FormatTemplate, -) []protocol.TextEdit { +) string { var key string if option.Key != nil { @@ -126,12 +129,7 @@ func formatSSHOption( value = "" } - return []protocol.TextEdit{ - { - Range: option.ToLSPRange(), - NewText: template.Format(options, key, value), - }, - } + return template.Format(options, key, value) } func formatMatchToString(