mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
fix(ssh_config): Fix fetching options
This commit is contained in:
parent
a9e58a7f50
commit
73a850c11a
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
59
handlers/ssh_config/document_fields_test.go
Normal file
59
handlers/ssh_config/document_fields_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@ -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{
|
||||||
|
@ -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 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user