Skip to content

Commit

Permalink
Fix double completion bug (#124)
Browse files Browse the repository at this point in the history
* Fix double completion bug
woops
Closes #122

* One more layer in test
  • Loading branch information
julienduchesne authored Aug 31, 2023
1 parent a23b945 commit aebe161
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/ast/processing/find_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/server/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/server/testdata/doubled-index-bug-1.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
hello: {
to: {
the: 'world',
},
},
}
1 change: 1 addition & 0 deletions pkg/server/testdata/doubled-index-bug-2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ hello: (import 'doubled-index-bug-1.jsonnet').hello }
1 change: 1 addition & 0 deletions pkg/server/testdata/doubled-index-bug-3.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'doubled-index-bug-2.jsonnet'
5 changes: 5 additions & 0 deletions pkg/server/testdata/doubled-index-bug-4.jsonnet
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit aebe161

Please sign in to comment.