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 ( import (
"config-lsp/common" "config-lsp/common"
"config-lsp/utils"
"github.com/emirpasic/gods/maps/treemap" "github.com/emirpasic/gods/maps/treemap"
) )
@ -145,10 +146,28 @@ func (c SSHConfig) FindOption(line uint32) (*SSHOption, SSHBlock) {
} }
type AllOptionInfo struct { type AllOptionInfo struct {
Block SSHBlock Block SSHBlock
Option *SSHOption 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 { func (c SSHConfig) GetAllOptions() []AllOptionInfo {
options := make([]AllOptionInfo, 0, 50) options := make([]AllOptionInfo, 0, 50)
@ -157,21 +176,21 @@ func (c SSHConfig) GetAllOptions() []AllOptionInfo {
case *SSHOption: case *SSHOption:
option := rawEntry.(*SSHOption) option := rawEntry.(*SSHOption)
options = append(options, AllOptionInfo{ options = append(options, AllOptionInfo{
Block: nil, Block: nil,
Option: option, Option: option,
}) })
case *SSHMatchBlock: case *SSHMatchBlock:
block := rawEntry.(SSHBlock) block := rawEntry.(SSHBlock)
options = append(options, AllOptionInfo{ options = append(options, AllOptionInfo{
Block: block, Block: block,
Option: block.GetEntryOption(), Option: block.GetEntryOption(),
}) })
for _, rawOption := range block.GetOptions().Values() { for _, rawOption := range block.GetOptions().Values() {
option := rawOption.(*SSHOption) option := rawOption.(*SSHOption)
options = append(options, AllOptionInfo{ options = append(options, AllOptionInfo{
Block: nil, Block: block,
Option: option, Option: option,
}) })
} }
@ -179,14 +198,14 @@ func (c SSHConfig) GetAllOptions() []AllOptionInfo {
block := rawEntry.(SSHBlock) block := rawEntry.(SSHBlock)
options = append(options, AllOptionInfo{ options = append(options, AllOptionInfo{
Block: block, Block: block,
Option: block.GetEntryOption(), Option: block.GetEntryOption(),
}) })
for _, rawOption := range block.GetOptions().Values() { for _, rawOption := range block.GetOptions().Values() {
option := rawOption.(*SSHOption) option := rawOption.(*SSHOption)
options = append(options, AllOptionInfo{ options = append(options, AllOptionInfo{
Block: nil, Block: block,
Option: option, Option: option,
}) })
} }
@ -196,4 +215,3 @@ func (c SSHConfig) GetAllOptions() []AllOptionInfo {
return options 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{}{ var AllowedDuplicateOptions = map[string]struct{}{
"CertificateFile": {}, "CertificateFile": {},
"Match": {},
"Host": {},
} }
var Options = map[string]docvalues.DocumentationValue{ 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( return utils.MapMapToSlice(
availableOptions, availableOptions,
func(name string, doc docvalues.DocumentationValue) protocol.CompletionItem { func(name string, doc docvalues.DocumentationValue) protocol.CompletionItem {