fix(wireguard): Improve completions for separator

This commit is contained in:
Myzel394 2024-08-14 21:52:47 +02:00
parent d9903805c5
commit 3cf43e12b3
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
4 changed files with 70 additions and 8 deletions

View File

@ -302,3 +302,49 @@ DNS=1.1.1.1
t.Fatalf("parseFromString: Invalid start and end lines %v", parser.Sections) t.Fatalf("parseFromString: Invalid start and end lines %v", parser.Sections)
} }
} }
func TestPartialKeyWorksCorrectly(
t *testing.T,
) {
sample := dedent(`
[Interface]
DNS
`)
parser := createWireguardParser()
errors := parser.parseFromString(sample)
if len(errors) > 0 {
t.Fatalf("parseFromString failed with error: %v", errors)
}
if !(parser.Sections[0].Properties[1].Key.Name == "DNS") {
t.Fatalf("parseFromString failed to collect properties of section 0: %v", parser.Sections[0].Properties)
}
if !(parser.Sections[0].Properties[1].Separator == nil) {
t.Fatalf("parseFromString failed to collect properties of section 0: %v", parser.Sections[0].Properties)
}
}
func TestPartialValueWithSeparatorWorksCorrectly(
t *testing.T,
) {
sample := dedent(`
[Interface]
DNS=
`)
parser := createWireguardParser()
errors := parser.parseFromString(sample)
if len(errors) > 0 {
t.Fatalf("parseFromString failed with error: %v", errors)
}
if !(parser.Sections[0].Properties[1].Value == nil) {
t.Fatalf("parseFromString failed to collect properties of section 0: %v", parser.Sections[0].Properties)
}
if !(parser.Sections[0].Properties[1].Separator != nil) {
t.Fatalf("parseFromString failed to collect properties of section 0: %v", parser.Sections[0].Properties)
}
}

View File

@ -29,7 +29,19 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
return nil, nil return nil, nil
} }
return section.getCompletionsForPropertyLine(lineNumber, params.Position.Character) completions, err := section.getCompletionsForPropertyLine(lineNumber, params.Position.Character)
if err != nil {
switch err.(type) {
// Ignore
case propertyNotFullyTypedError:
break
default:
return nil, err
}
}
return completions, nil
} }
panic("TextDocumentCompletion: unexpected line type") panic("TextDocumentCompletion: unexpected line type")

View File

@ -48,6 +48,7 @@ func createWireguardProperty(line string) (*wireguardProperty, error) {
return &wireguardProperty{ return &wireguardProperty{
Key: wireguardPropertyKey{ Key: wireguardPropertyKey{
Name: line[indexes[0]:indexes[1]],
Location: characterLocation{ Location: characterLocation{
Start: uint32(indexes[0]), Start: uint32(indexes[0]),
End: uint32(indexes[1]), End: uint32(indexes[1]),

View File

@ -43,13 +43,13 @@ func (s wireguardSection) String() string {
} }
func (s *wireguardSection) findProperty(lineNumber uint32) (*wireguardProperty, error) { func (s *wireguardSection) findProperty(lineNumber uint32) (*wireguardProperty, error) {
for _, property := range s.Properties { property, found := s.Properties[lineNumber]
if property.Key.Location.Start <= lineNumber && property.Key.Location.End >= lineNumber {
return &property, nil if !found {
} return nil, propertyNotFoundError{}
} }
return nil, propertyNotFoundError{} return &property, nil
} }
func (s wireguardSection) getCompletionsForEmptyLine() ([]protocol.CompletionItem, error) { func (s wireguardSection) getCompletionsForEmptyLine() ([]protocol.CompletionItem, error) {
@ -112,15 +112,18 @@ func (p wireguardSection) getCompletionsForPropertyLine(
var insertText string var insertText string
if character == property.Key.Location.End { if character == property.Key.Location.End {
insertText = " = " insertText = property.Key.Name + " = "
} else { } else {
insertText = "= " insertText = "= "
} }
kind := protocol.CompletionItemKindValue
return []protocol.CompletionItem{ return []protocol.CompletionItem{
{ {
Label: "=", Label: insertText,
InsertText: &insertText, InsertText: &insertText,
Kind: &kind,
}, },
}, propertyNotFullyTypedError{} }, propertyNotFullyTypedError{}
} }