mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-19 07:25:27 +02:00
feat(wireguard): Add completions for root interfaces
This commit is contained in:
parent
eab2d8ad29
commit
c5c3ad802e
@ -45,3 +45,54 @@ PrivateKey = 1234567890
|
|||||||
t.Fatalf("getCompletionsForEmptyLine: Expected %v completions, but got %v", expected, len(completions))
|
t.Fatalf("getCompletionsForEmptyLine: Expected %v completions, but got %v", expected, len(completions))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEmptyRootCompletionsWork(
|
||||||
|
t *testing.T,
|
||||||
|
) {
|
||||||
|
sample := dedent(`
|
||||||
|
`)
|
||||||
|
|
||||||
|
parser := createWireguardParser()
|
||||||
|
parser.parseFromString(sample)
|
||||||
|
|
||||||
|
completions := parser.getRootCompletionsForEmptyLine()
|
||||||
|
|
||||||
|
if len(completions) != 2 {
|
||||||
|
t.Fatalf("getRootCompletionsForEmptyLine: Expected 2 completions, but got %v", len(completions))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInterfaceSectionRootCompletionsBeforeWork(
|
||||||
|
t *testing.T,
|
||||||
|
) {
|
||||||
|
sample := dedent(`
|
||||||
|
|
||||||
|
[Interface]
|
||||||
|
`)
|
||||||
|
parser := createWireguardParser()
|
||||||
|
parser.parseFromString(sample)
|
||||||
|
|
||||||
|
completions := parser.getRootCompletionsForEmptyLine()
|
||||||
|
|
||||||
|
if len(completions) != 1 {
|
||||||
|
t.Fatalf("getRootCompletionsForEmptyLine: Expected 1 completions, but got %v", len(completions))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInterfaceAndPeerSectionRootCompletionsWork(
|
||||||
|
t *testing.T,
|
||||||
|
) {
|
||||||
|
sample := dedent(`
|
||||||
|
[Interface]
|
||||||
|
|
||||||
|
[Peer]
|
||||||
|
`)
|
||||||
|
parser := createWireguardParser()
|
||||||
|
parser.parseFromString(sample)
|
||||||
|
|
||||||
|
completions := parser.getRootCompletionsForEmptyLine()
|
||||||
|
|
||||||
|
if len(completions) != 1 {
|
||||||
|
t.Fatalf("getRootCompletionsForEmptyLine: Expected 1 completions, but got %v", len(completions))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -20,7 +20,7 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
case LineTypeEmpty:
|
case LineTypeEmpty:
|
||||||
if section == nil {
|
if section == nil {
|
||||||
return nil, nil
|
return parser.getRootCompletionsForEmptyLine(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return section.getCompletionsForEmptyLine()
|
return section.getCompletionsForEmptyLine()
|
||||||
|
@ -5,6 +5,8 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
|
||||||
var commentPattern = regexp.MustCompile(`^\s*(;|#)`)
|
var commentPattern = regexp.MustCompile(`^\s*(;|#)`)
|
||||||
@ -27,6 +29,45 @@ func (p *wireguardParser) clear() {
|
|||||||
p.CommentLines = map[uint32]struct{}{}
|
p.CommentLines = map[uint32]struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p wireguardParser) hasInterfaceSection() bool {
|
||||||
|
for _, section := range p.Sections {
|
||||||
|
if section.Name != nil && *section.Name == "Interface" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p wireguardParser) getRootCompletionsForEmptyLine() []protocol.CompletionItem {
|
||||||
|
kind := protocol.CompletionItemKindInterface
|
||||||
|
completions := []protocol.CompletionItem{}
|
||||||
|
|
||||||
|
if !p.hasInterfaceSection() {
|
||||||
|
sortText := "Z"
|
||||||
|
insertText := "[Interface]\n"
|
||||||
|
completions = append(completions, protocol.CompletionItem{
|
||||||
|
Label: "[Interface]",
|
||||||
|
InsertText: &insertText,
|
||||||
|
Kind: &kind,
|
||||||
|
Documentation: "An interface section represents the local node's configuration",
|
||||||
|
SortText: &sortText,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
sortText := "A"
|
||||||
|
insertText := "[Peer]\n"
|
||||||
|
completions = append(completions, protocol.CompletionItem{
|
||||||
|
Label: "[Peer]",
|
||||||
|
InsertText: &insertText,
|
||||||
|
Kind: &kind,
|
||||||
|
Documentation: "A peer section represents a peer that this node communicates with",
|
||||||
|
SortText: &sortText,
|
||||||
|
})
|
||||||
|
|
||||||
|
return completions
|
||||||
|
}
|
||||||
|
|
||||||
func createWireguardParser() wireguardParser {
|
func createWireguardParser() wireguardParser {
|
||||||
parser := wireguardParser{}
|
parser := wireguardParser{}
|
||||||
parser.clear()
|
parser.clear()
|
Loading…
x
Reference in New Issue
Block a user