@@ -17,20 +17,25 @@ import (
17
17
)
18
18
19
19
func (s * server ) Definition (ctx context.Context , params * protocol.DefinitionParams ) (protocol.Definition , error ) {
20
- definitionLink , err := s .DefinitionLink (ctx , params )
20
+ responseDefLinks , err := s .definitionLink (ctx , params , true )
21
21
if err != nil {
22
- return nil , nil
22
+ return nil , err
23
23
}
24
- definition := protocol.Definition {
25
- {
26
- URI : definitionLink .TargetURI ,
27
- Range : definitionLink .TargetRange ,
28
- },
24
+
25
+ // TODO: Support LocationLink instead of Location (this needs to be changed in the upstream protocol lib)
26
+ // When that's done, we can get rid of the intermediary `definitionLink` function which is used for testing
27
+ var response protocol.Definition
28
+ for _ , item := range responseDefLinks {
29
+ response = append (response , protocol.Location {
30
+ URI : item .TargetURI ,
31
+ Range : item .TargetRange ,
32
+ })
29
33
}
30
- return definition , nil
34
+
35
+ return response , nil
31
36
}
32
37
33
- func (s * server ) DefinitionLink (ctx context.Context , params * protocol.DefinitionParams ) (* protocol.DefinitionLink , error ) {
38
+ func (s * server ) definitionLink (ctx context.Context , params * protocol.DefinitionParams , warnIfNotFound bool ) ([] protocol.DefinitionLink , error ) {
34
39
doc , err := s .cache .get (params .TextDocument .URI )
35
40
if err != nil {
36
41
return nil , utils .LogErrorf ("Definition: %s: %w" , errorRetrievingDocument , err )
@@ -44,28 +49,24 @@ func (s *server) DefinitionLink(ctx context.Context, params *protocol.Definition
44
49
if err != nil {
45
50
return nil , utils .LogErrorf ("error creating the VM: %w" , err )
46
51
}
47
- definition , err := Definition (doc .ast , params , vm )
52
+ responseDefLinks , err := findDefinition (doc .ast , params , vm )
48
53
if err != nil {
49
- log .Warn (err .Error ())
54
+ if warnIfNotFound {
55
+ log .Warn (err .Error ())
56
+ return nil , nil
57
+ }
50
58
return nil , err
51
59
}
52
60
53
- return definition , nil
61
+ return responseDefLinks , nil
54
62
}
55
63
56
- func Definition (node ast.Node , params * protocol.DefinitionParams , vm * jsonnet.VM ) (* protocol.DefinitionLink , error ) {
57
- responseDefLink , err := findDefinition (node , params , vm )
58
- if err != nil {
59
- return nil , err
60
- }
61
- return responseDefLink , nil
62
- }
64
+ func findDefinition (root ast.Node , params * protocol.DefinitionParams , vm * jsonnet.VM ) ([]protocol.DefinitionLink , error ) {
65
+ var response []protocol.DefinitionLink
63
66
64
- func findDefinition (root ast.Node , params * protocol.DefinitionParams , vm * jsonnet.VM ) (* protocol.DefinitionLink , error ) {
65
67
searchStack , _ := processing .FindNodeByPosition (root , position .PositionProtocolToAST (params .Position ))
66
68
var deepestNode ast.Node
67
69
searchStack , deepestNode = searchStack .Pop ()
68
- var responseDefLink protocol.DefinitionLink
69
70
switch deepestNode := deepestNode .(type ) {
70
71
case * ast.Var :
71
72
log .Debugf ("Found Var node %s" , deepestNode .Id )
@@ -96,11 +97,11 @@ func findDefinition(root ast.Node, params *protocol.DefinitionParams, vm *jsonne
96
97
return nil , fmt .Errorf ("no matching bind found for %s" , deepestNode .Id )
97
98
}
98
99
99
- responseDefLink = protocol.DefinitionLink {
100
+ response = append ( response , protocol.DefinitionLink {
100
101
TargetURI : protocol .DocumentURI (filename ),
101
102
TargetRange : resultRange ,
102
103
TargetSelectionRange : resultSelectionRange ,
103
- }
104
+ })
104
105
case * ast.SuperIndex , * ast.Index :
105
106
indexSearchStack := nodestack .NewNodeStack (deepestNode )
106
107
indexList := indexSearchStack .BuildIndexList ()
@@ -109,32 +110,35 @@ func findDefinition(root ast.Node, params *protocol.DefinitionParams, vm *jsonne
109
110
if err != nil {
110
111
return nil , err
111
112
}
112
- objectRange := objectRanges [0 ] // TODO: Handle multiple positions
113
- responseDefLink = protocol.DefinitionLink {
114
- TargetURI : protocol .DocumentURI (objectRange .Filename ),
115
- TargetRange : position .RangeASTToProtocol (objectRange .FullRange ),
116
- TargetSelectionRange : position .RangeASTToProtocol (objectRange .SelectionRange ),
113
+ for _ , o := range objectRanges {
114
+ response = append (response , protocol.DefinitionLink {
115
+ TargetURI : protocol .DocumentURI (o .Filename ),
116
+ TargetRange : position .RangeASTToProtocol (o .FullRange ),
117
+ TargetSelectionRange : position .RangeASTToProtocol (o .SelectionRange ),
118
+ })
117
119
}
118
120
case * ast.Import :
119
121
filename := deepestNode .File .Value
120
122
importedFile , _ := vm .ResolveImport (string (params .TextDocument .URI ), filename )
121
- responseDefLink = protocol.DefinitionLink {
123
+ response = append ( response , protocol.DefinitionLink {
122
124
TargetURI : protocol .DocumentURI (importedFile ),
123
- }
125
+ })
124
126
default :
125
127
log .Debugf ("cannot find definition for node type %T" , deepestNode )
126
128
return nil , fmt .Errorf ("cannot find definition" )
127
129
128
130
}
129
131
130
- link := string (responseDefLink .TargetURI )
131
- if ! strings .HasPrefix (link , "file://" ) {
132
- targetFile , err := filepath .Abs (link )
133
- if err != nil {
134
- return nil , err
132
+ for i , item := range response {
133
+ link := string (item .TargetURI )
134
+ if ! strings .HasPrefix (link , "file://" ) {
135
+ targetFile , err := filepath .Abs (link )
136
+ if err != nil {
137
+ return nil , err
138
+ }
139
+ response [i ].TargetURI = protocol .URIFromPath (targetFile )
135
140
}
136
- responseDefLink .TargetURI = protocol .URIFromPath (targetFile )
137
141
}
138
142
139
- return & responseDefLink , nil
143
+ return response , nil
140
144
}
0 commit comments