-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathserver.ts
453 lines (403 loc) · 13.3 KB
/
server.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
import * as path from 'path'
import * as TurndownService from 'turndown'
import * as LSP from 'vscode-languageserver'
import { TextDocument } from 'vscode-languageserver-textdocument'
import Analyzer from './analyser'
import * as Builtins from './builtins'
import * as config from './config'
import Executables from './executables'
import { initializeParser } from './parser'
import * as ReservedWords from './reservedWords'
import { BashCompletionItem, CompletionItemDataType } from './types'
import { uniqueBasedOnHash } from './util/array'
/**
* The BashServer glues together the separate components to implement
* the various parts of the Language Server Protocol.
*/
export default class BashServer {
/**
* Initialize the server based on a connection to the client and the protocols
* initialization parameters.
*/
public static async initialize(
connection: LSP.Connection,
{ rootPath }: LSP.InitializeParams,
): Promise<BashServer> {
const parser = await initializeParser()
const { PATH } = process.env
if (!PATH) {
throw new Error('Expected PATH environment variable to be set')
}
return Promise.all([
Executables.fromPath(PATH),
Analyzer.fromRoot({ connection, rootPath, parser }),
]).then(xs => {
const executables = xs[0]
const analyzer = xs[1]
return new BashServer(connection, executables, analyzer)
})
}
private executables: Executables
private analyzer: Analyzer
private documents: LSP.TextDocuments<TextDocument> = new LSP.TextDocuments(TextDocument)
private connection: LSP.Connection
private constructor(
connection: LSP.Connection,
executables: Executables,
analyzer: Analyzer,
) {
this.connection = connection
this.executables = executables
this.analyzer = analyzer
}
/**
* Register handlers for the events from the Language Server Protocol that we
* care about.
*/
public register(connection: LSP.Connection): void {
// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
this.documents.listen(this.connection)
this.documents.onDidChangeContent(change => {
const { uri } = change.document
const diagnostics = this.analyzer.analyze(uri, change.document)
if (config.getHighlightParsingError()) {
connection.sendDiagnostics({
uri: change.document.uri,
diagnostics,
})
}
})
// Register all the handlers for the LSP events.
connection.onHover(this.onHover.bind(this))
connection.onDefinition(this.onDefinition.bind(this))
connection.onDocumentSymbol(this.onDocumentSymbol.bind(this))
connection.onWorkspaceSymbol(this.onWorkspaceSymbol.bind(this))
connection.onDocumentHighlight(this.onDocumentHighlight.bind(this))
connection.onReferences(this.onReferences.bind(this))
connection.onCompletion(this.onCompletion.bind(this))
connection.onCompletionResolve(this.onCompletionResolve.bind(this))
}
/**
* The parts of the Language Server Protocol that we are currently supporting.
*/
public capabilities(): LSP.ServerCapabilities {
return {
// For now we're using full-sync even though tree-sitter has great support
// for partial updates.
textDocumentSync: LSP.TextDocumentSyncKind.Full,
completionProvider: {
resolveProvider: true,
},
hoverProvider: true,
documentHighlightProvider: true,
definitionProvider: true,
documentSymbolProvider: true,
workspaceSymbolProvider: true,
referencesProvider: true,
}
}
private getWordAtPoint(
params: LSP.ReferenceParams | LSP.TextDocumentPositionParams,
): string | null {
return this.analyzer.wordAtPoint(
params.textDocument.uri,
params.position.line,
params.position.character,
)
}
private logRequest({
request,
params,
word,
}: {
request: string
params: LSP.ReferenceParams | LSP.TextDocumentPositionParams
word?: string | null
}) {
const wordLog = word ? `"${word}"` : 'null'
this.connection.console.log(
`${request} ${params.position.line}:${params.position.character} word=${wordLog}`,
)
}
private async onHover(
params: LSP.TextDocumentPositionParams,
): Promise<LSP.Hover | null> {
const word = this.getWordAtPoint(params)
this.logRequest({ request: 'onHover', params, word })
if (!word) {
return null
}
const explainshellEndpoint = config.getExplainshellEndpoint()
if (explainshellEndpoint) {
this.connection.console.log(`Query ${explainshellEndpoint}`)
const response = await this.analyzer.getExplainshellDocumentation({
params,
endpoint: explainshellEndpoint,
})
if (response.status === 'error') {
this.connection.console.log(
`getExplainshellDocumentation returned: ${JSON.stringify(response, null, 4)}`,
)
} else {
return {
contents: {
kind: 'markdown',
value: new TurndownService().turndown(response.helpHTML),
},
}
}
}
const getMarkdownHoverItem = (doc: string) => ({
// LSP.MarkupContent
value: ['``` man', doc, '```'].join('\n'),
// Passed as markdown for syntax highlighting
kind: 'markdown' as const,
})
if (Builtins.isBuiltin(word)) {
return Builtins.documentation(word).then(doc => ({
contents: getMarkdownHoverItem(doc),
}))
}
if (ReservedWords.isReservedWord(word)) {
return ReservedWords.documentation(word).then(doc => ({
contents: getMarkdownHoverItem(doc),
}))
}
if (this.executables.isExecutableOnPATH(word)) {
return this.executables.documentation(word).then(doc => ({
contents: getMarkdownHoverItem(doc),
}))
}
// FIXME: could also be a symbol
return null
}
private onDefinition(params: LSP.TextDocumentPositionParams): LSP.Definition | null {
const word = this.getWordAtPoint(params)
this.logRequest({ request: 'onDefinition', params, word })
if (!word) {
return null
}
return this.analyzer.findDefinition(word)
}
private onDocumentSymbol(params: LSP.DocumentSymbolParams): LSP.SymbolInformation[] {
this.connection.console.log(`onDocumentSymbol`)
return this.analyzer.findSymbolsForFile({ uri: params.textDocument.uri })
}
private onWorkspaceSymbol(params: LSP.WorkspaceSymbolParams): LSP.SymbolInformation[] {
this.connection.console.log('onWorkspaceSymbol')
return this.analyzer.search(params.query)
}
private onDocumentHighlight(
params: LSP.TextDocumentPositionParams,
): LSP.DocumentHighlight[] | null {
const word = this.getWordAtPoint(params)
this.logRequest({ request: 'onDocumentHighlight', params, word })
if (!word) {
return null
}
return this.analyzer
.findOccurrences(params.textDocument.uri, word)
.map(n => ({ range: n.range }))
}
private onReferences(params: LSP.ReferenceParams): LSP.Location[] | null {
const word = this.getWordAtPoint(params)
this.logRequest({ request: 'onReferences', params, word })
if (!word) {
return null
}
return this.analyzer.findReferences(word)
}
private onCompletion(params: LSP.TextDocumentPositionParams): BashCompletionItem[] {
const word = this.getWordAtPoint(params)
this.logRequest({ request: 'onCompletion', params, word })
const currentUri = params.textDocument.uri
const symbolCompletions =
word === null
? []
: getCompletionItemsForSymbols({
symbols: this.analyzer.findSymbolsMatchingWord({
word,
}),
currentUri,
})
const reservedWordsCompletions = ReservedWords.LIST.map(reservedWord => ({
label: reservedWord,
kind: LSP.SymbolKind.Interface, // ??
data: {
name: reservedWord,
type: CompletionItemDataType.ReservedWord,
},
}))
const programCompletions = this.executables
.list()
.filter(executable => !Builtins.isBuiltin(executable))
.map(executable => {
return {
label: executable,
kind: LSP.SymbolKind.Function,
data: {
name: executable,
type: CompletionItemDataType.Executable,
},
}
})
const builtinsCompletions = Builtins.LIST.map(builtin => ({
label: builtin,
kind: LSP.SymbolKind.Interface, // ??
data: {
name: builtin,
type: CompletionItemDataType.Builtin,
},
}))
const allCompletions = [
...reservedWordsCompletions,
...symbolCompletions,
...programCompletions,
...builtinsCompletions,
]
if (word) {
if (word.startsWith('#')) {
// Inside a comment block
return []
}
// Filter to only return suffixes of the current word
return allCompletions.filter(item => item.label.startsWith(word))
}
return allCompletions
}
private async onCompletionResolve(
item: LSP.CompletionItem,
): Promise<LSP.CompletionItem> {
const {
data: { name, type },
} = item as BashCompletionItem
this.connection.console.log(`onCompletionResolve name=${name} type=${type}`)
const getMarkdownCompletionItem = (doc: string) => ({
...item,
// LSP.MarkupContent
documentation: {
value: ['``` man', doc, '```'].join('\n'),
// Passed as markdown for syntax highlighting
kind: 'markdown' as const,
},
})
try {
if (type === CompletionItemDataType.Executable) {
const doc = await this.executables.documentation(name)
return getMarkdownCompletionItem(doc)
} else if (type === CompletionItemDataType.Builtin) {
const doc = await Builtins.documentation(name)
return getMarkdownCompletionItem(doc)
} else if (type === CompletionItemDataType.ReservedWord) {
const doc = await ReservedWords.documentation(name)
return getMarkdownCompletionItem(doc)
} else {
return item
}
} catch (error) {
return item
}
}
}
function getCompletionItemsForSymbols({
symbols,
currentUri,
}: {
symbols: LSP.SymbolInformation[]
currentUri: string
}): BashCompletionItem[] {
const isCurrentFile = ({ location: { uri } }: LSP.SymbolInformation) =>
uri === currentUri
const getSymbolId = ({ name, kind }: LSP.SymbolInformation) => `${name}${kind}`
const symbolsCurrentFile = symbols.filter(s => isCurrentFile(s))
const symbolsOtherFiles = symbols
.filter(s => !isCurrentFile(s))
// Remove identical symbols
.filter(
symbolOtherFiles =>
!symbolsCurrentFile.some(
symbolCurrentFile =>
getSymbolId(symbolCurrentFile) === getSymbolId(symbolOtherFiles),
),
)
return uniqueBasedOnHash(
[...symbolsCurrentFile, ...symbolsOtherFiles],
getSymbolId,
).map((symbol: LSP.SymbolInformation) => ({
label: symbol.name,
kind: symbolKindToCompletionKind(symbol.kind),
data: {
name: symbol.name,
type: CompletionItemDataType.Symbol,
},
documentation:
symbol.location.uri !== currentUri
? `${symbolKindToDescription(symbol.kind)} defined in ${path.relative(
currentUri,
symbol.location.uri,
)}`
: undefined,
}))
}
function symbolKindToCompletionKind(s: LSP.SymbolKind): LSP.CompletionItemKind {
switch (s) {
case LSP.SymbolKind.File:
return LSP.CompletionItemKind.File
case LSP.SymbolKind.Module:
case LSP.SymbolKind.Namespace:
case LSP.SymbolKind.Package:
return LSP.CompletionItemKind.Module
case LSP.SymbolKind.Class:
return LSP.CompletionItemKind.Class
case LSP.SymbolKind.Method:
return LSP.CompletionItemKind.Method
case LSP.SymbolKind.Property:
return LSP.CompletionItemKind.Property
case LSP.SymbolKind.Field:
return LSP.CompletionItemKind.Field
case LSP.SymbolKind.Constructor:
return LSP.CompletionItemKind.Constructor
case LSP.SymbolKind.Enum:
return LSP.CompletionItemKind.Enum
case LSP.SymbolKind.Interface:
return LSP.CompletionItemKind.Interface
case LSP.SymbolKind.Function:
return LSP.CompletionItemKind.Function
case LSP.SymbolKind.Variable:
return LSP.CompletionItemKind.Variable
case LSP.SymbolKind.Constant:
return LSP.CompletionItemKind.Constant
case LSP.SymbolKind.String:
case LSP.SymbolKind.Number:
case LSP.SymbolKind.Boolean:
case LSP.SymbolKind.Array:
case LSP.SymbolKind.Key:
case LSP.SymbolKind.Null:
return LSP.CompletionItemKind.Text
case LSP.SymbolKind.Object:
return LSP.CompletionItemKind.Module
case LSP.SymbolKind.EnumMember:
return LSP.CompletionItemKind.EnumMember
case LSP.SymbolKind.Struct:
return LSP.CompletionItemKind.Struct
case LSP.SymbolKind.Event:
return LSP.CompletionItemKind.Event
case LSP.SymbolKind.Operator:
return LSP.CompletionItemKind.Operator
case LSP.SymbolKind.TypeParameter:
return LSP.CompletionItemKind.TypeParameter
default:
return LSP.CompletionItemKind.Text
}
}
function symbolKindToDescription(s: LSP.SymbolKind): string {
switch (s) {
case LSP.SymbolKind.Function:
return 'Function'
case LSP.SymbolKind.Variable:
return 'Variable'
default:
return 'Keyword'
}
}