fix(wireguard): Improve root completions

This commit is contained in:
Myzel394 2024-08-18 14:56:57 +02:00
parent 9eedc063a2
commit 32ffbb8dbf
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
4 changed files with 51 additions and 6 deletions

View File

@ -20,13 +20,13 @@ func (p wireguardParser) checkForDuplicateProperties() []protocol.Diagnostic {
diagnostics := []protocol.Diagnostic{} diagnostics := []protocol.Diagnostic{}
for _, section := range p.Sections { for _, section := range p.Sections {
diagnostics = append(diagnostics, section.analyDuplicateProperties()...) diagnostics = append(diagnostics, section.analyzeDuplicateProperties()...)
} }
return diagnostics return diagnostics
} }
func (p wireguardSection) analyDuplicateProperties() []protocol.Diagnostic { func (p wireguardSection) analyzeDuplicateProperties() []protocol.Diagnostic {
diagnostics := []protocol.Diagnostic{} diagnostics := []protocol.Diagnostic{}
existingProperties := make(map[string]uint32) existingProperties := make(map[string]uint32)

View File

@ -146,3 +146,24 @@ DNS
t.Fatalf("getCompletionsForPropertyLine: Expected completion to be '= ', but got '%v'", completions[0].Label) t.Fatalf("getCompletionsForPropertyLine: Expected completion to be '= ', but got '%v'", completions[0].Label)
} }
} }
func TestHeaderButNoProperty(
t *testing.T,
) {
sample := dedent(`
[Interface]
`)
parser := createWireguardParser()
parser.parseFromString(sample)
completions, err := parser.Sections[0].getCompletionsForEmptyLine()
if err != nil {
t.Fatalf("getCompletionsForEmptyLine failed with error: %v", err)
}
if len(completions) != len(interfaceOptions) {
t.Fatalf("getCompletionsForEmptyLine: Expected %v completions, but got %v", len(interfaceOptions), len(completions))
}
}

View File

@ -19,7 +19,8 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
case LineTypeHeader: case LineTypeHeader:
return parser.getRootCompletionsForEmptyLine(), nil return parser.getRootCompletionsForEmptyLine(), nil
case LineTypeEmpty: case LineTypeEmpty:
if section == nil { if section.Name == nil {
// Root completions
return parser.getRootCompletionsForEmptyLine(), nil return parser.getRootCompletionsForEmptyLine(), nil
} }

View File

@ -49,14 +49,29 @@ func (p wireguardParser) getInterfaceSection() (*wireguardSection, bool) {
return nil, false return nil, false
} }
func getHeaderCompletion(name string, documentation string) protocol.CompletionItem {
textFormat := protocol.InsertTextFormatPlainText
kind := protocol.CompletionItemKindEnum
insertText := "[" + name + "]\n"
return protocol.CompletionItem{
Label: "[" + name + "]",
InsertTextFormat: &textFormat,
InsertText: &insertText,
Kind: &kind,
Documentation: &documentation,
}
}
func (p wireguardParser) getRootCompletionsForEmptyLine() []protocol.CompletionItem { func (p wireguardParser) getRootCompletionsForEmptyLine() []protocol.CompletionItem {
completions := []protocol.CompletionItem{} completions := []protocol.CompletionItem{}
if _, found := p.getInterfaceSection(); !found { if _, found := p.getInterfaceSection(); !found {
completions = append(completions, headerInterfaceEnum.ToCompletionItem()) completions = append(completions, getHeaderCompletion("Interface", headerInterfaceEnum.Documentation))
} }
completions = append(completions, headerPeerEnum.ToCompletionItem()) completions = append(completions, getHeaderCompletion("Peer", headerPeerEnum.Documentation))
return completions return completions
} }
@ -175,9 +190,17 @@ func (p *wireguardParser) parseFromString(input string) []common.ParseError {
var emptySection *wireguardSection var emptySection *wireguardSection
if len(collectedProperties) > 0 { if len(collectedProperties) > 0 {
var endLine uint32
if len(p.Sections) == 0 {
endLine = uint32(len(lines))
} else {
endLine = p.Sections[len(p.Sections)-1].StartLine
}
emptySection = &wireguardSection{ emptySection = &wireguardSection{
StartLine: 0, StartLine: 0,
EndLine: p.Sections[len(p.Sections)-1].StartLine - 1, EndLine: endLine,
Properties: collectedProperties, Properties: collectedProperties,
} }