Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completion: Fix two issues with local vars #117

Merged
merged 3 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/ast/processing/find_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
)

func FindBindByIDViaStack(stack *nodestack.NodeStack, id ast.Identifier) *ast.LocalBind {
for _, node := range stack.Stack {
nodes := append([]ast.Node{}, stack.From)
nodes = append(nodes, stack.Stack...)
for _, node := range nodes {
switch curr := node.(type) {
case *ast.Local:
for _, bind := range curr.Binds {
Expand Down
1 change: 1 addition & 0 deletions pkg/ast/processing/find_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
85 changes: 84 additions & 1 deletion pkg/server/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -466,7 +549,7 @@ func TestCompletion(t *testing.T) {
},
})
require.NoError(t, err)
assert.Equal(t, &tc.expected, result)
assert.Equal(t, tc.expected, *result)
})
}
}
5 changes: 5 additions & 0 deletions pkg/server/testdata/local-at-root-2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local hello = import 'local-at-root.jsonnet';

{
a: hello.to,
}
13 changes: 13 additions & 0 deletions pkg/server/testdata/local-at-root.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// hello.jsonnet
local hello = {
hel: {
wel: 'test',
},
hello: {
to: {
the: 'world',
},
},
};

hello.hello