-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
lsif.ts
160 lines (145 loc) · 4.91 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
import { convertHover, convertLocations } from '@sourcegraph/lsp-client/dist/lsp-conversion'
import * as sourcegraph from 'sourcegraph'
import * as LSP from 'vscode-languageserver-types'
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 {
const url = new URL(doc.uri)
url.hash = path
return url.href
}
async function send({
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': 'Sourcegraph LSIF extension',
}),
body: JSON.stringify({
method,
path,
position,
}),
})
if (!response.ok) {
throw new Error(`LSIF /request returned ${response.statusText}`)
}
return await response.json()
}
const lsifDocs = new Map<string, Promise<boolean>>()
async function hasLSIF(doc: sourcegraph.TextDocument): Promise<boolean> {
if (lsifDocs.has(doc.uri)) {
return await lsifDocs.get(doc.uri)!
}
const url = new URL('.api/lsif/exists', sourcegraph.internal.sourcegraphURL)
url.searchParams.set('repository', repositoryFromDoc(doc))
url.searchParams.set('commit', commitFromDoc(doc))
url.searchParams.set('file', pathFromDoc(doc))
const hasLSIFPromise = (async () => {
const response = await fetch(url.href, {
method: 'POST',
headers: new Headers({ 'x-requested-with': 'Sourcegraph LSIF extension' }),
})
if (!response.ok) {
throw new Error(`LSIF /exists returned ${response.statusText}`)
}
return await response.json()
})()
lsifDocs.set(doc.uri, hasLSIFPromise)
return await hasLSIFPromise
}
export function activate(ctx: sourcegraph.ExtensionContext): void {
ctx.subscriptions.add(
sourcegraph.languages.registerHoverProvider(['*'], {
provideHover: async (doc, position) => {
if (!(await hasLSIF(doc))) {
return null
}
const hover: LSP.Hover | null = await send({
doc,
method: 'hover',
path: pathFromDoc(doc),
position,
})
if (!hover) {
return null
}
return convertHover(sourcegraph, hover)
},
})
)
ctx.subscriptions.add(
sourcegraph.languages.registerDefinitionProvider(['*'], {
provideDefinition: async (doc, position) => {
if (!(await hasLSIF(doc))) {
return null
}
const body: LSP.Location | LSP.Location[] | null = await send({
doc,
method: 'definitions',
path: pathFromDoc(doc),
position,
})
if (!body) {
return null
}
const locations = Array.isArray(body) ? body : [body]
return convertLocations(
sourcegraph,
locations.map((definition: LSP.Location) => ({
...definition,
uri: setPath(doc, definition.uri),
}))
)
},
})
)
ctx.subscriptions.add(
sourcegraph.languages.registerReferenceProvider(['*'], {
provideReferences: async (doc, position) => {
if (!(await hasLSIF(doc))) {
return null
}
const locations: LSP.Location[] | null = await send({
doc,
method: 'references',
path: pathFromDoc(doc),
position,
})
if (!locations) {
return null
}
return convertLocations(
sourcegraph,
locations.map((reference: LSP.Location) => ({
...reference,
uri: setPath(doc, reference.uri),
}))
)
},
})
)
}