From aebe16108ead9c44bc88fc09db3199d4ffc51534 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Thu, 31 Aug 2023 14:15:23 -0400 Subject: [PATCH] Fix double completion bug (#124) * Fix double completion bug woops Closes https://github.com/grafana/jsonnet-language-server/issues/122 * One more layer in test --- pkg/ast/processing/find_field.go | 8 ++++---- pkg/server/completion_test.go | 20 +++++++++++++++++++ .../testdata/doubled-index-bug-1.jsonnet | 7 +++++++ .../testdata/doubled-index-bug-2.jsonnet | 1 + .../testdata/doubled-index-bug-3.jsonnet | 1 + .../testdata/doubled-index-bug-4.jsonnet | 5 +++++ 6 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 pkg/server/testdata/doubled-index-bug-1.jsonnet create mode 100644 pkg/server/testdata/doubled-index-bug-2.jsonnet create mode 100644 pkg/server/testdata/doubled-index-bug-3.jsonnet create mode 100644 pkg/server/testdata/doubled-index-bug-4.jsonnet diff --git a/pkg/ast/processing/find_field.go b/pkg/ast/processing/find_field.go index 364be83..d54021d 100644 --- a/pkg/ast/processing/find_field.go +++ b/pkg/ast/processing/find_field.go @@ -87,8 +87,8 @@ func extractObjectRangesFromDesugaredObjs(vm *jsonnet.VM, desugaredObjs []*ast.D for len(indexList) > 0 { index := indexList[0] indexList = indexList[1:] - partialMatchFields := partialMatchFields && len(indexList) == 0 // Only partial match on the last index. Others are considered complete - foundFields := findObjectFieldsInObjects(desugaredObjs, index, partialMatchFields) + partialMatchCurrentField := partialMatchFields && len(indexList) == 0 // Only partial match on the last index. Others are considered complete + foundFields := findObjectFieldsInObjects(desugaredObjs, index, partialMatchCurrentField) desugaredObjs = nil if len(foundFields) == 0 { return nil, fmt.Errorf("field %s was not found in ast.DesugaredObject", index) @@ -98,8 +98,8 @@ func extractObjectRangesFromDesugaredObjs(vm *jsonnet.VM, desugaredObjs []*ast.D ranges = append(ranges, FieldToRange(*found)) // If the field is not PlusSuper (field+: value), we stop there. Other previous values are not relevant - // If partialMatchFields is true, we can continue to look for other fields - if !found.PlusSuper && !partialMatchFields { + // If partialMatchCurrentField is true, we can continue to look for other fields + if !found.PlusSuper && !partialMatchCurrentField { break } } diff --git a/pkg/server/completion_test.go b/pkg/server/completion_test.go index c0c48ad..92855d7 100644 --- a/pkg/server/completion_test.go +++ b/pkg/server/completion_test.go @@ -566,6 +566,26 @@ func TestCompletion(t *testing.T) { }, }, }, + { + name: "autocomplete fix doubled index bug", + filename: "testdata/doubled-index-bug-4.jsonnet", + replaceString: "a: g.hello", + replaceByString: "a: g.hello.", + expected: protocol.CompletionList{ + IsIncomplete: false, + Items: []protocol.CompletionItem{ + { + Label: "to", + Kind: protocol.FieldCompletion, + Detail: "g.hello.to", + InsertText: "to", + LabelDetails: protocol.CompletionItemLabelDetails{ + Description: "object", + }, + }, + }, + }, + }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { diff --git a/pkg/server/testdata/doubled-index-bug-1.jsonnet b/pkg/server/testdata/doubled-index-bug-1.jsonnet new file mode 100644 index 0000000..7bd7208 --- /dev/null +++ b/pkg/server/testdata/doubled-index-bug-1.jsonnet @@ -0,0 +1,7 @@ +{ + hello: { + to: { + the: 'world', + }, + }, +} diff --git a/pkg/server/testdata/doubled-index-bug-2.jsonnet b/pkg/server/testdata/doubled-index-bug-2.jsonnet new file mode 100644 index 0000000..b0ab51d --- /dev/null +++ b/pkg/server/testdata/doubled-index-bug-2.jsonnet @@ -0,0 +1 @@ +{ hello: (import 'doubled-index-bug-1.jsonnet').hello } diff --git a/pkg/server/testdata/doubled-index-bug-3.jsonnet b/pkg/server/testdata/doubled-index-bug-3.jsonnet new file mode 100644 index 0000000..ddd33ad --- /dev/null +++ b/pkg/server/testdata/doubled-index-bug-3.jsonnet @@ -0,0 +1 @@ +import 'doubled-index-bug-2.jsonnet' diff --git a/pkg/server/testdata/doubled-index-bug-4.jsonnet b/pkg/server/testdata/doubled-index-bug-4.jsonnet new file mode 100644 index 0000000..3a3bef1 --- /dev/null +++ b/pkg/server/testdata/doubled-index-bug-4.jsonnet @@ -0,0 +1,5 @@ +local g = import 'doubled-index-bug-3.jsonnet'; +{ + // completing fields of `g.hello` should get use `g.hello.to`, not `g.hello.hello` + a: g.hello, +}