From 1564a3a8f1c119823cd523b3cbfe8a88b5f12f15 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Tue, 22 Aug 2023 12:33:47 -0400 Subject: [PATCH 1/3] Completion: Fix two issues with local vars Closes https://github.com/grafana/jsonnet-language-server/issues/114 Closes https://github.com/grafana/jsonnet-language-server/issues/115 I still have #113 to fix but I'll do it in another PR --- pkg/ast/processing/find_bind.go | 3 +- pkg/ast/processing/find_field.go | 1 + pkg/server/completion_test.go | 83 +++++++++++++++++++++ pkg/server/testdata/local-at-root-2.jsonnet | 5 ++ pkg/server/testdata/local-at-root.jsonnet | 13 ++++ 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 pkg/server/testdata/local-at-root-2.jsonnet create mode 100644 pkg/server/testdata/local-at-root.jsonnet diff --git a/pkg/ast/processing/find_bind.go b/pkg/ast/processing/find_bind.go index 3cb8016..be1150a 100644 --- a/pkg/ast/processing/find_bind.go +++ b/pkg/ast/processing/find_bind.go @@ -6,7 +6,8 @@ import ( ) func FindBindByIDViaStack(stack *nodestack.NodeStack, id ast.Identifier) *ast.LocalBind { - for _, node := range stack.Stack { + nodes := append(stack.Stack, stack.From) + for _, node := range nodes { switch curr := node.(type) { case *ast.Local: for _, bind := range curr.Binds { diff --git a/pkg/ast/processing/find_field.go b/pkg/ast/processing/find_field.go index a5765aa..c674044 100644 --- a/pkg/ast/processing/find_field.go +++ b/pkg/ast/processing/find_field.go @@ -88,6 +88,7 @@ func extractObjectRangesFromDesugaredObjs(stack *nodestack.NodeStack, vm *jsonne 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) desugaredObjs = nil if len(foundFields) == 0 { diff --git a/pkg/server/completion_test.go b/pkg/server/completion_test.go index f991da7..146802b 100644 --- a/pkg/server/completion_test.go +++ b/pkg/server/completion_test.go @@ -425,6 +425,89 @@ func TestCompletion(t *testing.T) { }, }, }, + { + name: "autocomplete local at root", + filename: "testdata/local-at-root.jsonnet", + replaceString: "hello.hello", + replaceByString: "hello.hel", + expected: protocol.CompletionList{ + IsIncomplete: false, + Items: []protocol.CompletionItem{ + { + Label: "hel", + Kind: protocol.FieldCompletion, + Detail: "hello.hel", + InsertText: "hel", + LabelDetails: protocol.CompletionItemLabelDetails{ + Description: "object", + }, + }, + { + Label: "hello", + Kind: protocol.FieldCompletion, + Detail: "hello.hello", + InsertText: "hello", + LabelDetails: protocol.CompletionItemLabelDetails{ + Description: "object", + }, + }, + }, + }, + }, + // TODO: This one doesn't work yet + // Issue: https://github.com/grafana/jsonnet-language-server/issues/113 + // { + // name: "autocomplete local at root 2", + // filename: "testdata/local-at-root-2.jsonnet", + // replaceString: "hello.to", + // replaceByString: "hello.", + // expected: protocol.CompletionList{ + // IsIncomplete: false, + // Items: []protocol.CompletionItem{ + // { + // Label: "to", + // Kind: protocol.FieldCompletion, + // Detail: "hello.to", + // InsertText: "to", + // LabelDetails: protocol.CompletionItemLabelDetails{ + // Description: "object", + // }, + // }, + // }, + // }, + // }, + { + // This checks that we don't match on `hello.hello.*` if we autocomplete on `hello.hel.` + name: "autocomplete local at root, no partial match if full match exists", + filename: "testdata/local-at-root.jsonnet", + replaceString: "hello.hello", + replaceByString: "hello.hel.", + expected: protocol.CompletionList{ + IsIncomplete: false, + Items: []protocol.CompletionItem{ + { + Label: "wel", + Kind: protocol.FieldCompletion, + Detail: "hello.hel.wel", + InsertText: "wel", + LabelDetails: protocol.CompletionItemLabelDetails{ + Description: "string", + }, + }, + }, + }, + }, + { + // This checks that we don't match anything on `hello.hell.*` + name: "autocomplete local at root, no match on unknown field", + filename: "testdata/local-at-root.jsonnet", + replaceString: "hello.hello", + replaceByString: "hello.hell.", + expected: protocol.CompletionList{ + IsIncomplete: false, + Items: nil, + }, + }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { diff --git a/pkg/server/testdata/local-at-root-2.jsonnet b/pkg/server/testdata/local-at-root-2.jsonnet new file mode 100644 index 0000000..7e25388 --- /dev/null +++ b/pkg/server/testdata/local-at-root-2.jsonnet @@ -0,0 +1,5 @@ +local hello = import 'local-at-root.jsonnet'; + +{ + a: hello.to, +} diff --git a/pkg/server/testdata/local-at-root.jsonnet b/pkg/server/testdata/local-at-root.jsonnet new file mode 100644 index 0000000..c7fbcda --- /dev/null +++ b/pkg/server/testdata/local-at-root.jsonnet @@ -0,0 +1,13 @@ +// hello.jsonnet +local hello = { + hel: { + wel: 'test', + }, + hello: { + to: { + the: 'world', + }, + }, +}; + +hello.hello From 52f3ed368f118583aaf02841000c35ccf84b2845 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Tue, 22 Aug 2023 12:38:58 -0400 Subject: [PATCH 2/3] Fix linting --- pkg/ast/processing/find_bind.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/ast/processing/find_bind.go b/pkg/ast/processing/find_bind.go index be1150a..59da31b 100644 --- a/pkg/ast/processing/find_bind.go +++ b/pkg/ast/processing/find_bind.go @@ -6,7 +6,8 @@ import ( ) func FindBindByIDViaStack(stack *nodestack.NodeStack, id ast.Identifier) *ast.LocalBind { - nodes := append(stack.Stack, stack.From) + nodes := append([]ast.Node{}, stack.From) + nodes = append(nodes, stack.Stack...) for _, node := range nodes { switch curr := node.(type) { case *ast.Local: From c910c2509baf3ea7e2e6a266195fb582c79e1579 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Tue, 22 Aug 2023 13:09:37 -0400 Subject: [PATCH 3/3] Fix linting --- pkg/server/completion_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/server/completion_test.go b/pkg/server/completion_test.go index 146802b..5ba5177 100644 --- a/pkg/server/completion_test.go +++ b/pkg/server/completion_test.go @@ -549,7 +549,7 @@ func TestCompletion(t *testing.T) { }, }) require.NoError(t, err) - assert.Equal(t, &tc.expected, result) + assert.Equal(t, tc.expected, *result) }) } }