@@ -5,13 +5,12 @@ import (
5
5
"reflect"
6
6
"strings"
7
7
8
- "github.com/google/go-jsonnet"
9
8
"github.com/google/go-jsonnet/ast"
10
9
"github.com/grafana/jsonnet-language-server/pkg/nodestack"
11
10
log "github.com/sirupsen/logrus"
12
11
)
13
12
14
- func FindRangesFromIndexList (stack * nodestack.NodeStack , indexList []string , vm * jsonnet. VM , partialMatchFields bool ) ([]ObjectRange , error ) {
13
+ func ( p * Processor ) FindRangesFromIndexList (stack * nodestack.NodeStack , indexList []string , partialMatchFields bool ) ([]ObjectRange , error ) {
15
14
var foundDesugaredObjects []* ast.DesugaredObject
16
15
// First element will be super, self, or var name
17
16
start , indexList := indexList [0 ], indexList [1 :]
@@ -31,13 +30,13 @@ func FindRangesFromIndexList(stack *nodestack.NodeStack, indexList []string, vm
31
30
if _ , ok := tmpStack .Peek ().(* ast.Binary ); ok {
32
31
tmpStack .Pop ()
33
32
}
34
- foundDesugaredObjects = filterSelfScope (FindTopLevelObjects (tmpStack , vm ))
33
+ foundDesugaredObjects = filterSelfScope (p . FindTopLevelObjects (tmpStack ))
35
34
case start == "std" :
36
35
return nil , fmt .Errorf ("cannot get definition of std lib" )
37
36
case start == "$" :
38
- foundDesugaredObjects = FindTopLevelObjects (nodestack .NewNodeStack (stack .From ), vm )
37
+ foundDesugaredObjects = p . FindTopLevelObjects (nodestack .NewNodeStack (stack .From ))
39
38
case strings .Contains (start , "." ):
40
- foundDesugaredObjects = FindTopLevelObjectsInFile (vm , start , "" )
39
+ foundDesugaredObjects = p . FindTopLevelObjectsInFile (start , "" )
41
40
42
41
default :
43
42
if strings .Count (start , "(" ) == 1 && strings .Count (start , ")" ) == 1 {
@@ -65,15 +64,15 @@ func FindRangesFromIndexList(stack *nodestack.NodeStack, indexList []string, vm
65
64
foundDesugaredObjects = append (foundDesugaredObjects , bodyNode )
66
65
case * ast.Self :
67
66
tmpStack := nodestack .NewNodeStack (stack .From )
68
- foundDesugaredObjects = FindTopLevelObjects (tmpStack , vm )
67
+ foundDesugaredObjects = p . FindTopLevelObjects (tmpStack )
69
68
case * ast.Import :
70
69
filename := bodyNode .File .Value
71
- foundDesugaredObjects = FindTopLevelObjectsInFile (vm , filename , "" )
70
+ foundDesugaredObjects = p . FindTopLevelObjectsInFile (filename , "" )
72
71
73
72
case * ast.Index , * ast.Apply :
74
73
tempStack := nodestack .NewNodeStack (bodyNode )
75
74
indexList = append (tempStack .BuildIndexList (), indexList ... )
76
- return FindRangesFromIndexList (stack , indexList , vm , partialMatchFields )
75
+ return p . FindRangesFromIndexList (stack , indexList , partialMatchFields )
77
76
case * ast.Function :
78
77
// If the function's body is an object, it means we can look for indexes within the function
79
78
if funcBody := findChildDesugaredObject (bodyNode .Body ); funcBody != nil {
@@ -84,10 +83,10 @@ func FindRangesFromIndexList(stack *nodestack.NodeStack, indexList []string, vm
84
83
}
85
84
}
86
85
87
- return extractObjectRangesFromDesugaredObjs (vm , foundDesugaredObjects , indexList , partialMatchFields )
86
+ return p . extractObjectRangesFromDesugaredObjs (foundDesugaredObjects , indexList , partialMatchFields )
88
87
}
89
88
90
- func extractObjectRangesFromDesugaredObjs ( vm * jsonnet. VM , desugaredObjs []* ast.DesugaredObject , indexList []string , partialMatchFields bool ) ([]ObjectRange , error ) {
89
+ func ( p * Processor ) extractObjectRangesFromDesugaredObjs ( desugaredObjs []* ast.DesugaredObject , indexList []string , partialMatchFields bool ) ([]ObjectRange , error ) {
91
90
var ranges []ObjectRange
92
91
for len (indexList ) > 0 {
93
92
index := indexList [0 ]
@@ -111,7 +110,7 @@ func extractObjectRangesFromDesugaredObjs(vm *jsonnet.VM, desugaredObjs []*ast.D
111
110
return ranges , nil
112
111
}
113
112
114
- fieldNodes , err := unpackFieldNodes (vm , foundFields )
113
+ fieldNodes , err := p . unpackFieldNodes (foundFields )
115
114
if err != nil {
116
115
return nil , err
117
116
}
@@ -125,7 +124,7 @@ func extractObjectRangesFromDesugaredObjs(vm *jsonnet.VM, desugaredObjs []*ast.D
125
124
// The target is a function and will be found by FindVarReference on the next loop
126
125
fieldNodes = append (fieldNodes , fieldNode .Target )
127
126
case * ast.Var :
128
- varReference , err := FindVarReference (fieldNode , vm )
127
+ varReference , err := p . FindVarReference (fieldNode )
129
128
if err != nil {
130
129
return nil , err
131
130
}
@@ -142,11 +141,11 @@ func extractObjectRangesFromDesugaredObjs(vm *jsonnet.VM, desugaredObjs []*ast.D
142
141
// if we're trying to find the a definition which is an index,
143
142
// we need to find it from itself, meaning that we need to create a stack
144
143
// from the index's target and search from there
145
- rootNode , _ , _ := vm .ImportAST ("" , fieldNode .LocRange .FileName )
144
+ rootNode , _ , _ := p . vm .ImportAST ("" , fieldNode .LocRange .FileName )
146
145
stack , _ := FindNodeByPosition (rootNode , fieldNode .Target .Loc ().Begin )
147
146
if stack != nil {
148
147
additionalIndexList := append (nodestack .NewNodeStack (fieldNode ).BuildIndexList (), indexList ... )
149
- result , _ := FindRangesFromIndexList (stack , additionalIndexList , vm , partialMatchFields )
148
+ result , _ := p . FindRangesFromIndexList (stack , additionalIndexList , partialMatchFields )
150
149
if len (result ) > 0 {
151
150
return result , err
152
151
}
@@ -157,7 +156,7 @@ func extractObjectRangesFromDesugaredObjs(vm *jsonnet.VM, desugaredObjs []*ast.D
157
156
desugaredObjs = append (desugaredObjs , findChildDesugaredObject (fieldNode .Body ))
158
157
case * ast.Import :
159
158
filename := fieldNode .File .Value
160
- newObjs := FindTopLevelObjectsInFile (vm , filename , string (fieldNode .Loc ().File .DiagnosticFileName ))
159
+ newObjs := p . FindTopLevelObjectsInFile (filename , string (fieldNode .Loc ().File .DiagnosticFileName ))
161
160
desugaredObjs = append (desugaredObjs , newObjs ... )
162
161
}
163
162
i ++
@@ -177,13 +176,13 @@ func flattenBinary(node ast.Node) []ast.Node {
177
176
// unpackFieldNodes extracts nodes from fields
178
177
// - Binary nodes. A field could be either in the left or right side of the binary
179
178
// - Self nodes. We want the object self refers to, not the self node itself
180
- func unpackFieldNodes ( vm * jsonnet. VM , fields []* ast.DesugaredObjectField ) ([]ast.Node , error ) {
179
+ func ( p * Processor ) unpackFieldNodes ( fields []* ast.DesugaredObjectField ) ([]ast.Node , error ) {
181
180
var fieldNodes []ast.Node
182
181
for _ , foundField := range fields {
183
182
switch fieldNode := foundField .Body .(type ) {
184
183
case * ast.Self :
185
184
filename := fieldNode .LocRange .FileName
186
- rootNode , _ , _ := vm .ImportAST ("" , filename )
185
+ rootNode , _ , _ := p . vm .ImportAST ("" , filename )
187
186
tmpStack , err := FindNodeByPosition (rootNode , fieldNode .LocRange .Begin )
188
187
if err != nil {
189
188
return nil , err
@@ -220,7 +219,6 @@ func findObjectFieldsInObject(objectNode *ast.DesugaredObject, index string, par
220
219
221
220
var matchingFields []* ast.DesugaredObjectField
222
221
for _ , field := range objectNode .Fields {
223
- field := field
224
222
literalString , isString := field .Name .(* ast.LiteralString )
225
223
if ! isString {
226
224
continue
@@ -253,8 +251,8 @@ func findChildDesugaredObject(node ast.Node) *ast.DesugaredObject {
253
251
254
252
// FindVarReference finds the object that the variable is referencing
255
253
// To do so, we get the stack where the var is used and search that stack for the var's definition
256
- func FindVarReference (varNode * ast.Var , vm * jsonnet. VM ) (ast.Node , error ) {
257
- varFileNode , _ , _ := vm .ImportAST ("" , varNode .LocRange .FileName )
254
+ func ( p * Processor ) FindVarReference (varNode * ast.Var ) (ast.Node , error ) {
255
+ varFileNode , _ , _ := p . vm .ImportAST ("" , varNode .LocRange .FileName )
258
256
varStack , err := FindNodeByPosition (varFileNode , varNode .Loc ().Begin )
259
257
if err != nil {
260
258
return nil , fmt .Errorf ("got the following error when finding the bind for %s: %w" , varNode .Id , err )
0 commit comments