From 3cf43e12b32128946c8008275c0480cc9c65a11d Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 14 Aug 2024 21:52:47 +0200 Subject: [PATCH] fix(wireguard): Improve completions for separator --- handlers/wireguard/parser_test.go | 46 +++++++++++++++++++ .../wireguard/text-document-completion.go | 14 +++++- handlers/wireguard/wg-property.go | 1 + handlers/wireguard/wg-section.go | 17 ++++--- 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/handlers/wireguard/parser_test.go b/handlers/wireguard/parser_test.go index 2f96bc3..7dacc51 100644 --- a/handlers/wireguard/parser_test.go +++ b/handlers/wireguard/parser_test.go @@ -302,3 +302,49 @@ DNS=1.1.1.1 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) + } +} diff --git a/handlers/wireguard/text-document-completion.go b/handlers/wireguard/text-document-completion.go index 04c0c75..ffb8102 100644 --- a/handlers/wireguard/text-document-completion.go +++ b/handlers/wireguard/text-document-completion.go @@ -29,7 +29,19 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa 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") diff --git a/handlers/wireguard/wg-property.go b/handlers/wireguard/wg-property.go index 2b0f629..59ed3eb 100644 --- a/handlers/wireguard/wg-property.go +++ b/handlers/wireguard/wg-property.go @@ -48,6 +48,7 @@ func createWireguardProperty(line string) (*wireguardProperty, error) { return &wireguardProperty{ Key: wireguardPropertyKey{ + Name: line[indexes[0]:indexes[1]], Location: characterLocation{ Start: uint32(indexes[0]), End: uint32(indexes[1]), diff --git a/handlers/wireguard/wg-section.go b/handlers/wireguard/wg-section.go index fd85e89..8bb7eb5 100644 --- a/handlers/wireguard/wg-section.go +++ b/handlers/wireguard/wg-section.go @@ -43,13 +43,13 @@ func (s wireguardSection) String() string { } func (s *wireguardSection) findProperty(lineNumber uint32) (*wireguardProperty, error) { - for _, property := range s.Properties { - if property.Key.Location.Start <= lineNumber && property.Key.Location.End >= lineNumber { - return &property, nil - } + property, found := s.Properties[lineNumber] + + if !found { + return nil, propertyNotFoundError{} } - return nil, propertyNotFoundError{} + return &property, nil } func (s wireguardSection) getCompletionsForEmptyLine() ([]protocol.CompletionItem, error) { @@ -112,15 +112,18 @@ func (p wireguardSection) getCompletionsForPropertyLine( var insertText string if character == property.Key.Location.End { - insertText = " = " + insertText = property.Key.Name + " = " } else { insertText = "= " } + kind := protocol.CompletionItemKindValue + return []protocol.CompletionItem{ { - Label: "=", + Label: insertText, InsertText: &insertText, + Kind: &kind, }, }, propertyNotFullyTypedError{} }