This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
lsif.ts
641 lines (579 loc) · 19 KB
/
lsif.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import * as sourcegraph from 'sourcegraph'
import * as LSP from 'vscode-languageserver-types'
import { convertLocations, convertHover } from './lsp-conversion'
import { queryGraphQL } from './api'
import { compareVersion } from './versions'
/** The date that the LSIF GraphQL API resolvers became available. */
const GRAPHQL_API_MINIMUM_DATE = '2019-12-12'
/** The version that the LSIF GraphQL API resolvers became available. */
const GRAPHQL_API_MINIMUM_VERSION = '3.11.0'
function repositoryFromDoc(doc: sourcegraph.TextDocument): string {
const url = new URL(doc.uri)
return url.hostname + url.pathname
}
function commitFromDoc(doc: sourcegraph.TextDocument): string {
const url = new URL(doc.uri)
return url.search.slice(1)
}
function pathFromDoc(doc: sourcegraph.TextDocument): string {
const url = new URL(doc.uri)
return url.hash.slice(1)
}
function setPath(doc: sourcegraph.TextDocument, path: string): string {
if (path.startsWith('git://')) {
return path
}
const url = new URL(doc.uri)
url.hash = path
return url.href
}
async function queryLSIF({
doc,
method,
path,
position,
}: {
doc: sourcegraph.TextDocument
method: string
path: string
position: LSP.Position
}): Promise<any> {
const url = new URL(
'.api/lsif/request',
sourcegraph.internal.sourcegraphURL
)
url.searchParams.set('repository', repositoryFromDoc(doc))
url.searchParams.set('commit', commitFromDoc(doc))
const response = await fetch(url.href, {
method: 'POST',
headers: new Headers({
'content-type': 'application/json',
'x-requested-with': 'Basic code intel',
}),
body: JSON.stringify({
method,
path,
position,
}),
})
if (!response.ok) {
throw new Error(`LSIF /request returned ${response.statusText}`)
}
return await response.json()
}
/**
* Creates an asynchronous predicate on a doc that checks for the existence of
* LSIF data for the given doc. It's a constructor because it creates an
* internal cache to reduce network traffic.
*/
export const mkIsLSIFAvailable = () => {
const lsifDocs = new Map<string, Promise<boolean>>()
return (doc: sourcegraph.TextDocument): Promise<boolean> => {
if (!sourcegraph.configuration.get().get('codeIntel.lsif')) {
console.log('LSIF is not enabled in global settings')
return Promise.resolve(false)
}
if (lsifDocs.has(doc.uri)) {
return lsifDocs.get(doc.uri)!
}
const repository = repositoryFromDoc(doc)
const commit = commitFromDoc(doc)
const file = pathFromDoc(doc)
const url = new URL(
'.api/lsif/exists',
sourcegraph.internal.sourcegraphURL
)
url.searchParams.set('repository', repository)
url.searchParams.set('commit', commit)
url.searchParams.set('file', file)
const hasLSIFPromise = (async () => {
try {
// Prevent leaking the name of a private repository to
// Sourcegraph.com by relying on the Sourcegraph extension host's
// private repository detection, which will throw an error when
// making a GraphQL request.
await queryGraphQL({
query: `query { currentUser { id } }`,
vars: {},
sourcegraph,
})
} catch (e) {
return false
}
const response = await fetch(url.href, {
method: 'POST',
headers: new Headers({
'x-requested-with': 'Basic code intel',
}),
})
if (!response.ok) {
return false
}
return response.json()
})()
lsifDocs.set(doc.uri, hasLSIFPromise)
return hasLSIFPromise
}
}
export async function hover(
doc: sourcegraph.TextDocument,
position: sourcegraph.Position
): Promise<Maybe<sourcegraph.Hover>> {
const hover: LSP.Hover | null = await queryLSIF({
doc,
method: 'hover',
path: pathFromDoc(doc),
position,
})
if (!hover) {
return undefined
}
return { value: convertHover(sourcegraph, hover) }
}
export async function definition(
doc: sourcegraph.TextDocument,
position: sourcegraph.Position
): Promise<Maybe<sourcegraph.Definition>> {
const body: LSP.Location | LSP.Location[] | null = await queryLSIF({
doc,
method: 'definitions',
path: pathFromDoc(doc),
position,
})
if (!body) {
return undefined
}
const locations = Array.isArray(body) ? body : [body]
if (locations.length === 0) {
return undefined
}
return {
value: convertLocations(
sourcegraph,
locations.map(d => ({ ...d, uri: setPath(doc, d.uri) }))
),
}
}
export async function references(
doc: sourcegraph.TextDocument,
position: sourcegraph.Position
): Promise<sourcegraph.Location[]> {
const body: LSP.Location[] | null = await queryLSIF({
doc,
method: 'references',
path: pathFromDoc(doc),
position,
})
if (!body) {
return []
}
const locations = Array.isArray(body) ? body : [body]
if (locations.length === 0) {
return []
}
return convertLocations(
sourcegraph,
locations.map(r => ({ ...r, uri: setPath(doc, r.uri) }))
)
}
/**
* An optional value of type T. It's either `{ value: T }` or `undefined`.
*/
export type Maybe<T> = { value: T } | undefined
/**
* Converts an async function that returns a type `ReturnType` to an async
* function that returns the type `Maybe<ReturnType>`.
*/
export const wrapMaybe = <Arguments extends any[], ReturnType>(
f: (...args: Arguments) => Promise<ReturnType>
) => async (...args: Arguments): Promise<Maybe<ReturnType>> => {
const returnValue = await f(...args)
return returnValue !== undefined ? { value: returnValue } : undefined
}
/**
* Only runs the given async function `f` when the given sync predicate on the arguments
* succeeds.
*/
export function when<Arguments extends any[], ReturnType>(
predicate: (...args: Arguments) => boolean
): (
f: (...args: Arguments) => Promise<ReturnType>
) => (...args: Arguments) => Promise<Maybe<ReturnType>> {
return f => async (...args) =>
predicate(...args) ? { value: await f(...args) } : undefined
}
/**
* Only runs the given async function `f` when the given async predicate on the arguments
* succeeds. Async version of `when`.
*/
export function asyncWhen<A extends any[], R>(
asyncPredicate: (...args: A) => Promise<boolean>
): (f: (...args: A) => Promise<R>) => (...args: A) => Promise<Maybe<R>> {
return f => async (...args) =>
(await asyncPredicate(...args))
? { value: await f(...args) }
: undefined
}
/**
* Only runs the given async function `f` when the given async predicate on the arguments
* succeeds. Async version of `when`.
*/
export function asyncWhenMaybe<A extends any[], R>(
asyncPredicate: (...args: A) => Promise<boolean>
): (f: (...args: A) => Promise<Maybe<R>>) => (...args: A) => Promise<Maybe<R>> {
return f => async (...args) =>
(await asyncPredicate(...args)) ? await f(...args) : undefined
}
/**
* Takes an array of async functions `fs` that return `Maybe<ReturnType>`, calls
* each `f` in series, bails when one returns `{ value: ... }`, and returns that
* value. Defaults to `defaultValue` when no `f` returns `{ value: ... }`.
*/
export const asyncFirst = <Arguments extends any[], ReturnType>(
fs: ((...args: Arguments) => Promise<Maybe<ReturnType>>)[],
defaultValue: ReturnType
) => async (...args: Arguments): Promise<ReturnType> => {
for (const f of fs) {
const maybeReturnValue = await f(...args)
if (maybeReturnValue !== undefined) {
return maybeReturnValue.value
}
}
return defaultValue
}
export interface MaybeProviders {
hover: (
doc: sourcegraph.TextDocument,
pos: sourcegraph.Position
) => Promise<Maybe<sourcegraph.Hover>>
definition: (
doc: sourcegraph.TextDocument,
pos: sourcegraph.Position
) => Promise<Maybe<sourcegraph.Definition>>
references: (
doc: sourcegraph.TextDocument,
pos: sourcegraph.Position
) => Promise<Maybe<sourcegraph.Location[]>>
}
export interface Providers {
hover: (
doc: sourcegraph.TextDocument,
pos: sourcegraph.Position
) => Promise<sourcegraph.Hover | null>
definition: (
doc: sourcegraph.TextDocument,
pos: sourcegraph.Position
) => Promise<sourcegraph.Definition | null>
references: (
doc: sourcegraph.TextDocument,
pos: sourcegraph.Position
) => Promise<sourcegraph.Location[] | null>
}
export const noopMaybeProviders = {
hover: () => Promise.resolve(undefined),
definition: () => Promise.resolve(undefined),
references: () => Promise.resolve(undefined),
}
export function initLSIF(): MaybeProviders {
const provider = (async () => {
if (await supportsGraphQL()) {
console.log('Sourcegraph instance supports LSIF GraphQL API')
return initGraphQL()
}
console.log(
'Sourcegraph instance does not support LSIF GraphQL API, falling back to HTTP API'
)
return initHTTP()
})()
return {
// If graphQL is supported, use the GraphQL implementation.
// Otherwise, use the legacy HTTP implementation.
definition: async (...args) => (await provider).definition(...args),
references: async (...args) => (await provider).references(...args),
hover: async (...args) => (await provider).hover(...args),
}
}
async function supportsGraphQL(): Promise<boolean> {
const query = `
query SiteVersion {
site {
productVersion
}
}
`
const respObj = await queryGraphQL({
query,
vars: {},
sourcegraph,
})
return compareVersion({
productVersion: respObj.data.site.productVersion,
minimumVersion: GRAPHQL_API_MINIMUM_VERSION,
minimumDate: GRAPHQL_API_MINIMUM_DATE,
})
}
function initHTTP(): MaybeProviders {
const isLSIFAvailable = mkIsLSIFAvailable()
return {
// You can read this as "only send a hover request when LSIF data is
// available for the given doc".
hover: asyncWhenMaybe<
[sourcegraph.TextDocument, sourcegraph.Position],
sourcegraph.Hover
>(isLSIFAvailable)(hover),
definition: asyncWhenMaybe<
[sourcegraph.TextDocument, sourcegraph.Position],
sourcegraph.Definition
>(isLSIFAvailable)(definition),
references: asyncWhen<
[sourcegraph.TextDocument, sourcegraph.Position],
sourcegraph.Location[]
>(isLSIFAvailable)(references),
}
}
function initGraphQL(): MaybeProviders {
const noLSIFData = new Set<string>()
const cacheUndefined = <R>(
f: (
doc: sourcegraph.TextDocument,
pos: sourcegraph.Position
) => Promise<Maybe<R>>
) => async (
doc: sourcegraph.TextDocument,
pos: sourcegraph.Position
): Promise<Maybe<R>> => {
if (!sourcegraph.configuration.get().get('codeIntel.lsif')) {
console.log('LSIF is not enabled in global settings')
return undefined
}
if (noLSIFData.has(doc.uri)) {
return undefined
}
const result = await f(doc, pos)
if (result === undefined) {
noLSIFData.add(doc.uri)
}
return result
}
return {
definition: cacheUndefined(definitionGraphQL),
references: cacheUndefined(referencesGraphQL),
hover: cacheUndefined(hoverGraphQL),
}
}
async function definitionGraphQL(
doc: sourcegraph.TextDocument,
position: sourcegraph.Position
): Promise<Maybe<sourcegraph.Definition | null>> {
const query = `
query Definitions($repository: String!, $commit: String!, $path: String!, $line: Int!, $character: Int!) {
repository(name: $repository) {
commit(rev: $commit) {
blob(path: $path) {
lsif {
definitions(line: $line, character: $character) {
nodes {
resource {
path
repository {
name
}
commit {
oid
}
}
range {
start {
line
character
}
end {
line
character
}
}
}
}
}
}
}
}
}
`
const lsifObj = await queryLSIFGraphQL<{
definitions: { nodes: LocationConnectionNode[] }
}>({ doc, query, position })
if (!lsifObj) {
return undefined
}
return { value: lsifObj.definitions.nodes.map(nodeToLocation) }
}
async function referencesGraphQL(
doc: sourcegraph.TextDocument,
position: sourcegraph.Position
): Promise<Maybe<sourcegraph.Location[]>> {
const query = `
query References($repository: String!, $commit: String!, $path: String!, $line: Int!, $character: Int!) {
repository(name: $repository) {
commit(rev: $commit) {
blob(path: $path) {
lsif {
references(line: $line, character: $character) {
nodes {
resource {
path
repository {
name
}
commit {
oid
}
}
range {
start {
line
character
}
end {
line
character
}
}
}
}
}
}
}
}
}
`
const lsifObj = await queryLSIFGraphQL<{
references: { nodes: LocationConnectionNode[] }
}>({ doc, query, position })
if (!lsifObj) {
return undefined
}
return { value: lsifObj.references.nodes.map(nodeToLocation) }
}
async function hoverGraphQL(
doc: sourcegraph.TextDocument,
position: sourcegraph.Position
): Promise<Maybe<sourcegraph.Hover>> {
const query = `
query Hover($repository: String!, $commit: String!, $path: String!, $line: Int!, $character: Int!) {
repository(name: $repository) {
commit(rev: $commit) {
blob(path: $path) {
lsif {
hover(line: $line, character: $character) {
markdown {
text
}
range {
start {
line
character
}
end {
line
character
}
}
}
}
}
}
}
}
`
const lsifObj = await queryLSIFGraphQL<{
hover: { markdown: { text: string }; range: sourcegraph.Range }
}>({
doc,
query,
position,
})
if (!lsifObj) {
return undefined
}
return {
value: {
contents: {
value: lsifObj.hover.markdown.text,
kind: sourcegraph.MarkupKind.Markdown,
},
range: lsifObj.hover.range,
},
}
}
async function queryLSIFGraphQL<T>({
doc,
query,
position,
}: {
doc: sourcegraph.TextDocument
query: string
position: LSP.Position
}): Promise<T | undefined> {
repositoryFromDoc(doc)
commitFromDoc(doc)
const vars = {
repository: repositoryFromDoc(doc),
commit: commitFromDoc(doc),
path: pathFromDoc(doc),
line: position.line,
character: position.character,
}
const respObj: {
data: {
repository: {
commit: {
blob: {
lsif: T
}
}
}
}
errors: Error[]
} = await queryGraphQL({
query,
vars,
sourcegraph,
})
if (respObj.errors) {
const asError = (err: { message: string }): Error =>
Object.assign(new Error(err.message), err)
if (respObj.errors.length === 1) {
throw asError(respObj.errors[0])
}
throw Object.assign(
new Error(respObj.errors.map(e => e.message).join('\n')),
{
name: 'AggregateError',
errors: respObj.errors.map(asError),
}
)
}
return respObj.data.repository.commit.blob.lsif
}
type LocationConnectionNode = {
resource: {
path: string
repository: { name: string }
commit: { oid: string }
}
range: sourcegraph.Range
}
function nodeToLocation(node: LocationConnectionNode): sourcegraph.Location {
return {
uri: new sourcegraph.URI(
`git://${node.resource.repository.name}?${node.resource.commit.oid}#${node.resource.path}`
),
range: new sourcegraph.Range(
node.range.start.line,
node.range.start.character,
node.range.end.line,
node.range.end.character
),
}
}