Skip to content

Commit aebe161

Browse files
Fix double completion bug (#124)
* Fix double completion bug woops Closes #122 * One more layer in test
1 parent a23b945 commit aebe161

6 files changed

+38
-4
lines changed

pkg/ast/processing/find_field.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ func extractObjectRangesFromDesugaredObjs(vm *jsonnet.VM, desugaredObjs []*ast.D
8787
for len(indexList) > 0 {
8888
index := indexList[0]
8989
indexList = indexList[1:]
90-
partialMatchFields := partialMatchFields && len(indexList) == 0 // Only partial match on the last index. Others are considered complete
91-
foundFields := findObjectFieldsInObjects(desugaredObjs, index, partialMatchFields)
90+
partialMatchCurrentField := partialMatchFields && len(indexList) == 0 // Only partial match on the last index. Others are considered complete
91+
foundFields := findObjectFieldsInObjects(desugaredObjs, index, partialMatchCurrentField)
9292
desugaredObjs = nil
9393
if len(foundFields) == 0 {
9494
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
9898
ranges = append(ranges, FieldToRange(*found))
9999

100100
// If the field is not PlusSuper (field+: value), we stop there. Other previous values are not relevant
101-
// If partialMatchFields is true, we can continue to look for other fields
102-
if !found.PlusSuper && !partialMatchFields {
101+
// If partialMatchCurrentField is true, we can continue to look for other fields
102+
if !found.PlusSuper && !partialMatchCurrentField {
103103
break
104104
}
105105
}

pkg/server/completion_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,26 @@ func TestCompletion(t *testing.T) {
566566
},
567567
},
568568
},
569+
{
570+
name: "autocomplete fix doubled index bug",
571+
filename: "testdata/doubled-index-bug-4.jsonnet",
572+
replaceString: "a: g.hello",
573+
replaceByString: "a: g.hello.",
574+
expected: protocol.CompletionList{
575+
IsIncomplete: false,
576+
Items: []protocol.CompletionItem{
577+
{
578+
Label: "to",
579+
Kind: protocol.FieldCompletion,
580+
Detail: "g.hello.to",
581+
InsertText: "to",
582+
LabelDetails: protocol.CompletionItemLabelDetails{
583+
Description: "object",
584+
},
585+
},
586+
},
587+
},
588+
},
569589
}
570590
for _, tc := range testCases {
571591
t.Run(tc.name, func(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
hello: {
3+
to: {
4+
the: 'world',
5+
},
6+
},
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ hello: (import 'doubled-index-bug-1.jsonnet').hello }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'doubled-index-bug-2.jsonnet'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local g = import 'doubled-index-bug-3.jsonnet';
2+
{
3+
// completing fields of `g.hello` should get use `g.hello.to`, not `g.hello.hello`
4+
a: g.hello,
5+
}

0 commit comments

Comments
 (0)