Skip to content

Commit 446a98f

Browse files
authored
Include all in-scope locals in completion (#181)
* Enables auto-complete for variables scoped to current object node, instead of just globally defined variables
1 parent 2412c8b commit 446a98f

File tree

5 files changed

+64
-36
lines changed

5 files changed

+64
-36
lines changed

pkg/server/completion.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,19 @@ func (s *Server) completionFromStack(line string, stack *nodestack.NodeStack, vm
8282
items := []protocol.CompletionItem{}
8383
// firstIndex is a variable (local) completion
8484
for !stack.IsEmpty() {
85-
if curr, ok := stack.Pop().(*ast.Local); ok {
86-
for _, bind := range curr.Binds {
87-
label := string(bind.Variable)
88-
89-
if !strings.HasPrefix(label, indexes[0]) {
90-
continue
91-
}
92-
85+
curr := stack.Pop()
86+
var binds ast.LocalBinds
87+
switch typedCurr := curr.(type) {
88+
case *ast.DesugaredObject:
89+
binds = typedCurr.Locals
90+
case *ast.Local:
91+
binds = typedCurr.Binds
92+
default:
93+
continue
94+
}
95+
for _, bind := range binds {
96+
label := string(bind.Variable)
97+
if strings.HasPrefix(label, indexes[0]) && label != "$" {
9398
items = append(items, createCompletionItem(label, "", protocol.VariableCompletion, bind.Body, position))
9499
}
95100
}

pkg/server/completion_test.go

+38-16
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,26 @@ func TestCompletion(t *testing.T) {
221221
replaceByString: "bar: ",
222222
expected: protocol.CompletionList{
223223
IsIncomplete: false,
224-
Items: []protocol.CompletionItem{{
225-
Label: "somevar",
226-
Kind: protocol.VariableCompletion,
227-
Detail: "somevar",
228-
InsertText: "somevar",
229-
LabelDetails: protocol.CompletionItemLabelDetails{
230-
Description: "string",
224+
Items: []protocol.CompletionItem{
225+
{
226+
Label: "somevar2",
227+
Kind: protocol.VariableCompletion,
228+
Detail: "somevar2",
229+
InsertText: "somevar2",
230+
LabelDetails: protocol.CompletionItemLabelDetails{
231+
Description: "string",
232+
},
231233
},
232-
}},
234+
{
235+
Label: "somevar",
236+
Kind: protocol.VariableCompletion,
237+
Detail: "somevar",
238+
InsertText: "somevar",
239+
LabelDetails: protocol.CompletionItemLabelDetails{
240+
Description: "string",
241+
},
242+
},
243+
},
233244
},
234245
},
235246
{
@@ -239,15 +250,26 @@ func TestCompletion(t *testing.T) {
239250
replaceByString: "bar: some",
240251
expected: protocol.CompletionList{
241252
IsIncomplete: false,
242-
Items: []protocol.CompletionItem{{
243-
Label: "somevar",
244-
Kind: protocol.VariableCompletion,
245-
Detail: "somevar",
246-
InsertText: "somevar",
247-
LabelDetails: protocol.CompletionItemLabelDetails{
248-
Description: "string",
253+
Items: []protocol.CompletionItem{
254+
{
255+
Label: "somevar2",
256+
Kind: protocol.VariableCompletion,
257+
Detail: "somevar2",
258+
InsertText: "somevar2",
259+
LabelDetails: protocol.CompletionItemLabelDetails{
260+
Description: "string",
261+
},
249262
},
250-
}},
263+
{
264+
Label: "somevar",
265+
Kind: protocol.VariableCompletion,
266+
Detail: "somevar",
267+
InsertText: "somevar",
268+
LabelDetails: protocol.CompletionItemLabelDetails{
269+
Description: "string",
270+
},
271+
},
272+
},
251273
},
252274
},
253275
{

pkg/server/definition_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,12 @@ var definitionTestCases = []definitionTestCase{
232232
results: []definitionResult{{
233233
targetFilename: "testdata/basic-object.jsonnet",
234234
targetRange: protocol.Range{
235-
Start: protocol.Position{Line: 5, Character: 2},
236-
End: protocol.Position{Line: 5, Character: 12},
235+
Start: protocol.Position{Line: 6, Character: 2},
236+
End: protocol.Position{Line: 6, Character: 12},
237237
},
238238
targetSelectionRange: protocol.Range{
239-
Start: protocol.Position{Line: 5, Character: 2},
240-
End: protocol.Position{Line: 5, Character: 5},
239+
Start: protocol.Position{Line: 6, Character: 2},
240+
End: protocol.Position{Line: 6, Character: 5},
241241
},
242242
}},
243243
},
@@ -248,12 +248,12 @@ var definitionTestCases = []definitionTestCase{
248248
results: []definitionResult{{
249249
targetFilename: "testdata/basic-object.jsonnet",
250250
targetRange: protocol.Range{
251-
Start: protocol.Position{Line: 5, Character: 2},
252-
End: protocol.Position{Line: 5, Character: 12},
251+
Start: protocol.Position{Line: 6, Character: 2},
252+
End: protocol.Position{Line: 6, Character: 12},
253253
},
254254
targetSelectionRange: protocol.Range{
255-
Start: protocol.Position{Line: 5, Character: 2},
256-
End: protocol.Position{Line: 5, Character: 5},
255+
Start: protocol.Position{Line: 6, Character: 2},
256+
End: protocol.Position{Line: 6, Character: 5},
257257
},
258258
}},
259259
},

pkg/server/symbols_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,21 @@ func TestSymbols(t *testing.T) {
106106
Kind: protocol.Field,
107107
Range: protocol.Range{
108108
Start: protocol.Position{
109-
Line: 5,
109+
Line: 6,
110110
Character: 2,
111111
},
112112
End: protocol.Position{
113-
Line: 5,
113+
Line: 6,
114114
Character: 12,
115115
},
116116
},
117117
SelectionRange: protocol.Range{
118118
Start: protocol.Position{
119-
Line: 5,
119+
Line: 6,
120120
Character: 2,
121121
},
122122
End: protocol.Position{
123-
Line: 5,
123+
Line: 6,
124124
Character: 5,
125125
},
126126
},

pkg/server/testdata/basic-object.jsonnet

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ local somevar = 'hello';
33
{
44
foo: 'bar',
55
} + {
6+
local somevar2 = 'world',
67
bar: 'foo',
78
}

0 commit comments

Comments
 (0)