feat(ssh_config): Add completions for tag

This commit is contained in:
Myzel394 2024-10-02 16:56:42 +02:00
parent 7359fac2e0
commit eda3a0e771
No known key found for this signature in database
GPG Key ID: ED20A1D1D423AF3F
4 changed files with 65 additions and 9 deletions

View File

@ -81,6 +81,13 @@ func GetOptionCompletions(
matchBlock.MatchValue, matchBlock.MatchValue,
) )
} }
if entry.Key.Key == tagOption {
return getTagCompletions(
d,
cursor,
entry,
)
}
if entry.OptionValue == nil { if entry.OptionValue == nil {
return option.DeprecatedFetchCompletions("", 0), nil return option.DeprecatedFetchCompletions("", 0), nil

View File

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

View File

@ -9,6 +9,7 @@ import (
var hostOption = fields.CreateNormalizedName("Host") var hostOption = fields.CreateNormalizedName("Host")
var matchOption = fields.CreateNormalizedName("Match") var matchOption = fields.CreateNormalizedName("Match")
var tagOption = fields.CreateNormalizedName("Tag")
func FormatDocument( func FormatDocument(
d *sshconfig.SSHDocument, d *sshconfig.SSHDocument,

View File

@ -49,7 +49,10 @@ func formatOption(
template = blockOptionTemplate template = blockOptionTemplate
} }
edits = append(edits, formatSSHOption(option, options, template)...) edits = append(edits, protocol.TextEdit{
Range: option.ToLSPRange(),
NewText: formatOptionToString(option, options, template),
})
} }
return edits return edits
@ -101,11 +104,11 @@ func formatMatchBlock(
return edits return edits
} }
func formatSSHOption( func formatOptionToString(
option *ast.SSHOption, option *ast.SSHOption,
options protocol.FormattingOptions, options protocol.FormattingOptions,
template formatting.FormatTemplate, template formatting.FormatTemplate,
) []protocol.TextEdit { ) string {
var key string var key string
if option.Key != nil { if option.Key != nil {
@ -126,12 +129,7 @@ func formatSSHOption(
value = "" value = ""
} }
return []protocol.TextEdit{ return template.Format(options, key, value)
{
Range: option.ToLSPRange(),
NewText: template.Format(options, key, value),
},
}
} }
func formatMatchToString( func formatMatchToString(