fix(ssh_config): Fix fetching options

This commit is contained in:
Myzel394 2024-09-22 22:48:02 +02:00
parent a9e58a7f50
commit 73a850c11a
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
4 changed files with 86 additions and 16 deletions

View File

@ -2,6 +2,7 @@ package ast
import (
"config-lsp/common"
"config-lsp/utils"
"github.com/emirpasic/gods/maps/treemap"
)
@ -149,6 +150,24 @@ type AllOptionInfo struct {
Option *SSHOption
}
func (c SSHConfig) GetAllOptionsForBlock(block SSHBlock) []AllOptionInfo {
if block == nil {
return c.GetAllOptions()
}
return utils.Map(
block.GetOptions().Values(),
func(rawOption interface{}) AllOptionInfo {
option := rawOption.(*SSHOption)
return AllOptionInfo{
Block: block,
Option: option,
}
},
)
}
func (c SSHConfig) GetAllOptions() []AllOptionInfo {
options := make([]AllOptionInfo, 0, 50)
@ -171,7 +190,7 @@ func (c SSHConfig) GetAllOptions() []AllOptionInfo {
for _, rawOption := range block.GetOptions().Values() {
option := rawOption.(*SSHOption)
options = append(options, AllOptionInfo{
Block: nil,
Block: block,
Option: option,
})
}
@ -186,7 +205,7 @@ func (c SSHConfig) GetAllOptions() []AllOptionInfo {
for _, rawOption := range block.GetOptions().Values() {
option := rawOption.(*SSHOption)
options = append(options, AllOptionInfo{
Block: nil,
Block: block,
Option: option,
})
}
@ -196,4 +215,3 @@ func (c SSHConfig) GetAllOptions() []AllOptionInfo {
return options
}

View File

@ -0,0 +1,59 @@
package sshconfig
import (
"config-lsp/handlers/ssh_config/ast"
"config-lsp/handlers/ssh_config/indexes"
"config-lsp/utils"
"testing"
)
func TestComplexExample(
t *testing.T,
) {
input := utils.Dedent(`
ProxyCommand hello
Host laptop
HostName laptop.lan
ProxyCommand test
Match originalhost laptop exec "[[ $(/usr/bin/dig +short laptop.lan) == '' ]]"
HostName laptop.sdn
`)
c := ast.NewSSHConfig()
errors := c.Parse(input)
if len(errors) != 0 {
t.Fatalf("Expected no errors, got %v", errors)
}
i, errors := indexes.CreateIndexes(*c)
if len(errors) != 0 {
t.Fatalf("Expected no errors, got %v", errors)
}
d := &SSHDocument{
Config: c,
Indexes: i,
}
options := d.FindOptionsByName("ProxyCommand")
if !(len(options) == 2 && options[0].Option.Start.Line == 0 && options[1].Option.Start.Line == 4) {
t.Errorf("Expected 2 options, got %v", options)
}
options = d.FindOptionsByName("HostName")
if !(len(options) == 2 && options[0].Option.Start.Line == 3 && options[1].Option.Start.Line == 7) {
t.Errorf("Expected 2 options, got %v", options)
}
block := d.Config.FindBlock(4)
if !(d.FindOptionByNameAndBlock("ProxyCommand", block).Option.Start.Line == 4) {
t.Errorf("Expected 4, got %v", d.FindOptionByNameAndBlock("PorxyCommand", block).Option.Start.Line)
}
if !(d.FindOptionByNameAndBlock("ProxyCommand", nil).Option.Start.Line == 0) {
t.Errorf("Expected 0, got %v", d.FindOptionByNameAndBlock("ProxyCommand", nil).Option.Start.Line)
}
}

View File

@ -11,6 +11,8 @@ var MAX_PORT = 65535
var AllowedDuplicateOptions = map[string]struct{}{
"CertificateFile": {},
"Match": {},
"Host": {},
}
var Options = map[string]docvalues.DocumentationValue{

View File

@ -28,15 +28,6 @@ func GetRootCompletions(
}
}
// Remove all fields that are already present and are not allowed to be duplicated
for _, info := range d.Config.GetAllOptions() {
if _, found := fields.AllowedDuplicateOptions[info.Option.Key.Key]; found {
continue
}
delete(availableOptions, info.Option.Key.Key)
}
return utils.MapMapToSlice(
availableOptions,
func(name string, doc docvalues.DocumentationValue) protocol.CompletionItem {