Skip to content

Commit d57743f

Browse files
committed
Improve completion item type safety
1 parent ebc78db commit d57743f

File tree

5 files changed

+46
-28
lines changed

5 files changed

+46
-28
lines changed

server/src/__tests__/__snapshots__/analyzer.test.ts.snap

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,127 +131,127 @@ Array [
131131
Object {
132132
"data": Object {
133133
"name": "ret",
134-
"type": "function",
134+
"type": 3,
135135
},
136136
"kind": 6,
137137
"label": "ret",
138138
},
139139
Object {
140140
"data": Object {
141141
"name": "configures",
142-
"type": "function",
142+
"type": 3,
143143
},
144144
"kind": 6,
145145
"label": "configures",
146146
},
147147
Object {
148148
"data": Object {
149149
"name": "npm_config_loglevel",
150-
"type": "function",
150+
"type": 3,
151151
},
152152
"kind": 6,
153153
"label": "npm_config_loglevel",
154154
},
155155
Object {
156156
"data": Object {
157157
"name": "node",
158-
"type": "function",
158+
"type": 3,
159159
},
160160
"kind": 6,
161161
"label": "node",
162162
},
163163
Object {
164164
"data": Object {
165165
"name": "TMP",
166-
"type": "function",
166+
"type": 3,
167167
},
168168
"kind": 6,
169169
"label": "TMP",
170170
},
171171
Object {
172172
"data": Object {
173173
"name": "BACK",
174-
"type": "function",
174+
"type": 3,
175175
},
176176
"kind": 6,
177177
"label": "BACK",
178178
},
179179
Object {
180180
"data": Object {
181181
"name": "tar",
182-
"type": "function",
182+
"type": 3,
183183
},
184184
"kind": 6,
185185
"label": "tar",
186186
},
187187
Object {
188188
"data": Object {
189189
"name": "MAKE",
190-
"type": "function",
190+
"type": 3,
191191
},
192192
"kind": 6,
193193
"label": "MAKE",
194194
},
195195
Object {
196196
"data": Object {
197197
"name": "make",
198-
"type": "function",
198+
"type": 3,
199199
},
200200
"kind": 6,
201201
"label": "make",
202202
},
203203
Object {
204204
"data": Object {
205205
"name": "clean",
206-
"type": "function",
206+
"type": 3,
207207
},
208208
"kind": 6,
209209
"label": "clean",
210210
},
211211
Object {
212212
"data": Object {
213213
"name": "node_version",
214-
"type": "function",
214+
"type": 3,
215215
},
216216
"kind": 6,
217217
"label": "node_version",
218218
},
219219
Object {
220220
"data": Object {
221221
"name": "t",
222-
"type": "function",
222+
"type": 3,
223223
},
224224
"kind": 6,
225225
"label": "t",
226226
},
227227
Object {
228228
"data": Object {
229229
"name": "url",
230-
"type": "function",
230+
"type": 3,
231231
},
232232
"kind": 6,
233233
"label": "url",
234234
},
235235
Object {
236236
"data": Object {
237237
"name": "ver",
238-
"type": "function",
238+
"type": 3,
239239
},
240240
"kind": 6,
241241
"label": "ver",
242242
},
243243
Object {
244244
"data": Object {
245245
"name": "isnpm10",
246-
"type": "function",
246+
"type": 3,
247247
},
248248
"kind": 6,
249249
"label": "isnpm10",
250250
},
251251
Object {
252252
"data": Object {
253253
"name": "NODE",
254-
"type": "function",
254+
"type": 3,
255255
},
256256
"kind": 6,
257257
"label": "NODE",

server/src/__tests__/server.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as lsp from 'vscode-languageserver'
22

33
import { FIXTURE_FOLDER, FIXTURE_URI } from '../../../testing/fixtures'
44
import LspServer from '../server'
5+
import { CompletionItemDataType } from '../types'
56

67
async function initializeServer() {
78
const diagnostics: Array<lsp.PublishDiagnosticsParams | undefined> = undefined
@@ -157,7 +158,7 @@ describe('server', () => {
157158
{
158159
data: {
159160
name: 'rm',
160-
type: 'executable',
161+
type: CompletionItemDataType.Executable,
161162
},
162163
kind: expect.any(Number),
163164
label: 'rm',

server/src/analyser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as LSP from 'vscode-languageserver'
55
import * as Parser from 'web-tree-sitter'
66

77
import { getGlobPattern } from './config'
8+
import { BashCompletionItem, CompletionItemDataType } from './types'
89
import { uniqueBasedOnHash } from './util/array'
910
import { flattenArray, flattenObjectValues } from './util/flatten'
1011
import { getFilePaths } from './util/fs'
@@ -223,7 +224,7 @@ export default class Analyzer {
223224
/**
224225
* Find unique symbol completions for the given file.
225226
*/
226-
public findSymbolCompletions(uri: string): LSP.CompletionItem[] {
227+
public findSymbolCompletions(uri: string): BashCompletionItem[] {
227228
const hashFunction = ({ name, kind }: LSP.SymbolInformation) => `${name}${kind}`
228229

229230
return uniqueBasedOnHash(this.findSymbols(uri), hashFunction).map(
@@ -232,7 +233,7 @@ export default class Analyzer {
232233
kind: this.symbolKindToCompletionKind(symbol.kind),
233234
data: {
234235
name: symbol.name,
235-
type: 'function',
236+
type: CompletionItemDataType.Symbol,
236237
},
237238
}),
238239
)

server/src/server.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as config from './config'
77
import Executables from './executables'
88
import { initializeParser } from './parser'
99
import * as ReservedWords from './reservedWords'
10+
import { BashCompletionItem, CompletionItemDataType } from './types'
1011

1112
/**
1213
* The BashServer glues together the separate components to implement
@@ -204,7 +205,7 @@ export default class BashServer {
204205
return this.analyzer.findReferences(word)
205206
}
206207

207-
private onCompletion(params: LSP.TextDocumentPositionParams): LSP.CompletionItem[] {
208+
private onCompletion(params: LSP.TextDocumentPositionParams): BashCompletionItem[] {
208209
const word = this.getWordAtPoint(params)
209210
this.logRequest({ request: 'onCompletion', params, word })
210211

@@ -217,7 +218,7 @@ export default class BashServer {
217218
kind: LSP.SymbolKind.Interface, // ??
218219
data: {
219220
name: reservedWord,
220-
type: 'reservedWord',
221+
type: CompletionItemDataType.ReservedWord,
221222
},
222223
}))
223224

@@ -227,17 +228,17 @@ export default class BashServer {
227228
kind: LSP.SymbolKind.Function,
228229
data: {
229230
name: s,
230-
type: 'executable',
231+
type: CompletionItemDataType.Executable,
231232
},
232233
}
233234
})
234235

235236
const builtinsCompletions = Builtins.LIST.map(builtin => ({
236237
label: builtin,
237-
kind: LSP.SymbolKind.Method, // ??
238+
kind: LSP.SymbolKind.Interface, // ??
238239
data: {
239240
name: builtin,
240-
type: 'builtin',
241+
type: CompletionItemDataType.Builtin,
241242
},
242243
}))
243244

@@ -263,7 +264,7 @@ export default class BashServer {
263264
}
264265

265266
private async onCompletionResolve(
266-
item: LSP.CompletionItem,
267+
item: BashCompletionItem,
267268
): Promise<LSP.CompletionItem> {
268269
const {
269270
data: { name, type },
@@ -282,13 +283,13 @@ export default class BashServer {
282283
})
283284

284285
try {
285-
if (type === 'executable') {
286+
if (type === CompletionItemDataType.Executable) {
286287
const doc = await this.executables.documentation(name)
287288
return getMarkdownCompletionItem(doc)
288-
} else if (type === 'builtin') {
289+
} else if (type === CompletionItemDataType.Builtin) {
289290
const doc = await Builtins.documentation(name)
290291
return getMarkdownCompletionItem(doc)
291-
} else if (type === 'reservedWord') {
292+
} else if (type === CompletionItemDataType.ReservedWord) {
292293
const doc = await ReservedWords.documentation(name)
293294
return getMarkdownCompletionItem(doc)
294295
} else {

server/src/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as LSP from 'vscode-languageserver'
2+
3+
export enum CompletionItemDataType {
4+
Builtin,
5+
Executable,
6+
ReservedWord,
7+
Symbol,
8+
}
9+
10+
export interface BashCompletionItem extends LSP.CompletionItem {
11+
data: {
12+
type: CompletionItemDataType
13+
name: string
14+
}
15+
}

0 commit comments

Comments
 (0)