feat(wireguard): Add completions for root interfaces

This commit is contained in:
Myzel394 2024-08-14 20:03:53 +02:00
parent eab2d8ad29
commit c5c3ad802e
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
3 changed files with 93 additions and 1 deletions

View File

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

View File

@ -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()

View File

@ -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()