@@ -9,34 +9,49 @@ import (
9
9
"github.com/pkg/errors"
10
10
)
11
11
12
- func defRefImplQuery (n int ) string {
12
+ func defRefImplQuery (args DefRefImplArgs ) string {
13
13
var aliased []string
14
14
var params []string
15
- for i := 0 ; i < n ; i ++ {
16
- params = append (params , fmt .Sprintf ("$line%v: Int!, $character%v: Int!" , i , i ))
17
- aliased = append (aliased , fmt .Sprintf (`
18
- references%v: references(
19
- line: $line%v
20
- character: $character%v
15
+
16
+ for fileIndex , file := range args .Files {
17
+ params = append (params , fmt .Sprintf ("$file%vpath: String!" , fileIndex ))
18
+ var aliasedFile []string
19
+ for i := range file .Positions {
20
+ params = append (params , fmt .Sprintf ("$file%vline%v: Int!" , fileIndex , i ))
21
+ params = append (params , fmt .Sprintf ("$file%vcharacter%v: Int!" , fileIndex , i ))
22
+ aliasedFile = append (aliasedFile , strings .ReplaceAll (strings .ReplaceAll (`
23
+ references#P: references(
24
+ line: $file#Fline#P
25
+ character: $file#Fcharacter#P
21
26
first: $firstReferences
22
27
after: $afterReferences
23
28
filter: $filter
24
29
) {
25
30
...LocationConnectionFields
26
31
}
27
- implementations%v: implementations(
28
- line: $line%v
29
- character: $character%v
30
- first: $firstImplementations
31
- after: $afterImplementations
32
- filter: $filter
33
- ) {
32
+ # TODO: query implementations once that API does not take down prod:
33
+ # https://github.com/sourcegraph/sourcegraph/issues/36882
34
+ #implementations#P: implementations(
35
+ # line: $file#Fline#P
36
+ # character: $file#Fcharacter#P
37
+ # first: $firstImplementations
38
+ # after: $afterImplementations
39
+ # filter: $filter
40
+ #) {
41
+ # ...LocationConnectionFields
42
+ #}
43
+ definitions#P: definitions(line: $file#Fline#P, character: $file#Fcharacter#P, filter: $filter) {
34
44
...LocationConnectionFields
35
45
}
36
- definitions%v: definitions(line: $line%v, character: $character%v, filter: $filter) {
37
- ...LocationConnectionFields
38
- }
39
- ` , i , i , i , i , i , i , i , i , i ))
46
+ ` , "#F" , fmt .Sprint (fileIndex )), "#P" , fmt .Sprint (i )))
47
+ }
48
+ aliased = append (aliased , fmt .Sprintf (`
49
+ blob%v: blob(path: $file%vpath) {
50
+ lsif {
51
+ %s
52
+ }
53
+ }
54
+ ` , fileIndex , fileIndex , strings .Join (aliasedFile , "\n " )))
40
55
}
41
56
42
57
return fmt .Sprintf (`
@@ -69,7 +84,16 @@ func defRefImplQuery(n int) string {
69
84
}
70
85
}
71
86
72
- query UsePreciseCodeIntelForPosition($repositoryCloneUrl: String!, $commit: String!, $path: String!, $afterReferences: String, $firstReferences: Int, $afterImplementations: String, $firstImplementations: Int, $filter: String, %s) {
87
+ query UsePreciseCodeIntelForPosition(
88
+ $repositoryCloneUrl: String!,
89
+ $commit: String!,
90
+ $afterReferences: String,
91
+ $firstReferences: Int,
92
+ # TODO: query implementations once that API does not take down prod:
93
+ # https://github.com/sourcegraph/sourcegraph/issues/36882
94
+ #$afterImplementations: String,
95
+ #$firstImplementations: Int,
96
+ $filter: String, %s) {
73
97
repository(cloneURL: $repositoryCloneUrl) {
74
98
id
75
99
name
@@ -78,11 +102,7 @@ func defRefImplQuery(n int) string {
78
102
isArchived
79
103
commit(rev: $commit) {
80
104
id
81
- blob(path: $path) {
82
- lsif {
83
- %s
84
- }
85
- }
105
+ %s
86
106
}
87
107
}
88
108
}
@@ -94,12 +114,16 @@ type Position struct {
94
114
Character uint `json:"character"`
95
115
}
96
116
117
+ type File struct {
118
+ Path string `json:"path"`
119
+ Positions []Position
120
+ }
121
+
97
122
type DefRefImplArgs struct {
98
123
AfterImplementations * string `json:"afterImplementations"`
99
124
AfterReferences * string `json:"afterReferences"`
100
125
RepositoryCloneURL string `json:"repositoryCloneUrl"`
101
- Path string `json:"path"`
102
- Positions []Position
126
+ Files []File
103
127
Commit string `json:"commit"`
104
128
Filter * string `json:"filter"`
105
129
FirstImplementations uint `json:"firstImplementations"`
@@ -111,18 +135,20 @@ func (c *graphQLClient) DefRefImpl(ctx context.Context, args DefRefImplArgs) (*R
111
135
"afterImplementations" : args .AfterImplementations ,
112
136
"afterReferences" : args .AfterReferences ,
113
137
"repositoryCloneUrl" : args .RepositoryCloneURL ,
114
- "path" : args .Path ,
115
138
"commit" : args .Commit ,
116
139
"filter" : args .Filter ,
117
140
"firstImplementations" : args .FirstImplementations ,
118
141
"firstReferences" : args .FirstReferences ,
119
142
}
120
- for i , pos := range args .Positions {
121
- vars [fmt .Sprintf ("line%v" , i )] = pos .Line
122
- vars [fmt .Sprintf ("character%v" , i )] = pos .Character
143
+ for fileIndex , file := range args .Files {
144
+ vars [fmt .Sprintf ("file%vpath" , fileIndex )] = file .Path
145
+ for i , pos := range file .Positions {
146
+ vars [fmt .Sprintf ("file%vline%v" , fileIndex , i )] = pos .Line
147
+ vars [fmt .Sprintf ("file%vcharacter%v" , fileIndex , i )] = pos .Character
148
+ }
123
149
}
124
150
125
- resp , err := c .requestGraphQL (ctx , "DefRefImpl" , defRefImplQuery (len ( args . Positions ) ), vars )
151
+ resp , err := c .requestGraphQL (ctx , "DefRefImpl" , defRefImplQuery (args ), vars )
126
152
if err != nil {
127
153
return nil , errors .Wrap (err , "graphql" )
128
154
}
@@ -135,71 +161,74 @@ func (c *graphQLClient) DefRefImpl(ctx context.Context, args DefRefImplArgs) (*R
135
161
return nil , errors .Wrap (err , "Unmarshal" )
136
162
}
137
163
var (
138
- r = raw .Data .Repository
139
- references []Location
140
- implementations []Location
141
- definitions []Location
164
+ r = raw .Data .Repository
165
+ blobs []Blob
142
166
)
143
- decodeLocation := func ( name string , dst * [] Location ) error {
144
- raw , ok := r .Commit . Blob . LSIF [ name ]
167
+ for fileIndex , file := range args . Files {
168
+ rawBlob , ok := r .Commit [ fmt . Sprintf ( "blob%v" , fileIndex ) ]
145
169
if ! ok {
146
- return nil
147
- }
148
- var result * Location
149
- if err := json .Unmarshal (raw , & result ); err != nil {
150
- return errors .Wrap (err , "Unmarshal" )
170
+ continue
151
171
}
152
- if result != nil {
153
- * dst = append ( * dst , * result )
172
+ var info struct {
173
+ LSIF map [ string ]json. RawMessage
154
174
}
155
- return nil
156
- }
157
- for i := range args .Positions {
158
- if err := decodeLocation (fmt .Sprintf ("references%v" , i ), & references ); err != nil {
159
- return nil , errors .Wrap (err , "decodeLocation(references)" )
175
+ if err := json .Unmarshal (rawBlob , & info ); err != nil {
176
+ return nil , errors .Wrap (err , "Unmarshal" )
160
177
}
161
- if err := decodeLocation (fmt .Sprintf ("implementations%v" , i ), & implementations ); err != nil {
162
- return nil , errors .Wrap (err , "decodeLocation(implementations)" )
178
+
179
+ decodeLocation := func (name string , dst * []Location ) error {
180
+ raw , ok := info .LSIF [name ]
181
+ if ! ok {
182
+ return nil
183
+ }
184
+ var result * Location
185
+ if err := json .Unmarshal (raw , & result ); err != nil {
186
+ return errors .Wrap (err , "Unmarshal" )
187
+ }
188
+ if result != nil {
189
+ * dst = append (* dst , * result )
190
+ }
191
+ return nil
163
192
}
164
- if err := decodeLocation (fmt .Sprintf ("definitions%v" , i ), & definitions ); err != nil {
165
- return nil , errors .Wrap (err , "decodeLocation(definitions)" )
193
+ blob := Blob {LSIF : & LSIFBlob {}}
194
+ for i := range file .Positions {
195
+ if err := decodeLocation (fmt .Sprintf ("references%v" , i ), & blob .LSIF .References ); err != nil {
196
+ return nil , errors .Wrap (err , "decodeLocation(references)" )
197
+ }
198
+ // TODO: query implementations once that API does not take down prod:
199
+ // https://github.com/sourcegraph/sourcegraph/issues/36882
200
+ // if err := decodeLocation(fmt.Sprintf("implementations%v", i), &blob.LSIF.Implementations); err != nil {
201
+ // return nil, errors.Wrap(err, "decodeLocation(implementations)")
202
+ // }
203
+ if err := decodeLocation (fmt .Sprintf ("definitions%v" , i ), & blob .LSIF .Definitions ); err != nil {
204
+ return nil , errors .Wrap (err , "decodeLocation(definitions)" )
205
+ }
166
206
}
207
+ blobs = append (blobs , blob )
167
208
}
209
+ var commitID string
210
+ _ = json .Unmarshal (r .Commit ["id" ], & commitID )
211
+ var commitOID string
212
+ _ = json .Unmarshal (r .Commit ["oid" ], & commitOID )
168
213
return & Repository {
169
214
ID : r .ID ,
170
215
Name : r .Name ,
171
216
Stars : r .Stars ,
172
217
IsFork : r .IsFork ,
173
218
IsArchived : r .IsArchived ,
174
219
Commit : & Commit {
175
- ID : r .Commit .ID ,
176
- OID : r .Commit .OID ,
177
- Blob : & Blob {
178
- LSIF : & LSIFBlob {
179
- References : references ,
180
- Implementations : implementations ,
181
- Definitions : definitions ,
182
- },
183
- },
220
+ ID : commitID ,
221
+ OID : commitOID ,
222
+ Blobs : blobs ,
184
223
},
185
224
}, nil
186
225
}
187
226
188
- type DefRefImplBlob struct {
189
- LSIF map [string ]json.RawMessage
190
- }
191
-
192
- type DefRefImplCommit struct {
193
- ID string
194
- OID string
195
- Blob * DefRefImplBlob
196
- }
197
-
198
227
type DefRefImplRepository struct {
199
228
ID string
200
229
Name string
201
230
Stars uint64
202
231
IsFork bool
203
232
IsArchived bool
204
- Commit * DefRefImplCommit ` json:"commit"`
233
+ Commit map [ string ] json. RawMessage
205
234
}
0 commit comments