-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCompletionSupportProvider.ts
More file actions
391 lines (332 loc) · 14.6 KB
/
CompletionSupportProvider.ts
File metadata and controls
391 lines (332 loc) · 14.6 KB
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
// Copyright 2022 - 2025 The MathWorks, Inc.
import { CompletionItem, CompletionItemKind, CompletionList, CompletionParams, ParameterInformation, Position, SignatureHelp, SignatureHelpParams, SignatureInformation, TextDocuments, InsertTextFormat } from 'vscode-languageserver'
import { TextDocument } from 'vscode-languageserver-textdocument'
import MatlabLifecycleManager from '../../lifecycle/MatlabLifecycleManager'
import ConfigurationManager, { Argument } from '../../lifecycle/ConfigurationManager'
import MVM from '../../mvm/impl/MVM'
import Logger from '../../logging/Logger'
import parse from '../../mvm/MdaParser'
import * as FileNameUtils from '../../utils/FileNameUtils'
interface MCompletionData {
widgetData?: MWidgetData
widgetType?: string
signatures?: MSignatureData | MSignatureData[] // If there is only one signature, it is not given as an array
}
interface MWidgetData {
choices?: MCompletionChoice | MCompletionChoice[] // If there is only one choice, it is not given as an array
truncated?: boolean
}
interface MCompletionChoice {
completion: string
matchType: string
purpose: string
displayString?: string
}
interface MSignatureData {
functionName: string
inputArguments?: MArgumentData | MArgumentData[] // If there is only one argument, it is not given as an array
outputArguments?: MArgumentData | MArgumentData[] // If there is only one argument, it is not given as an array
}
interface MArgumentData {
name: string
widgetType: string
widgetData?: MWidgetData
status?: string
purpose?: string
valueSummary?: string
}
// Maps the completion type, as returned by MATLAB®, to the corresponding CompletionItemKind
const MatlabCompletionToKind: { [index: string]: CompletionItemKind } = {
literal: CompletionItemKind.Text,
unknown: CompletionItemKind.Function,
pathItem: CompletionItemKind.File,
mFile: CompletionItemKind.Function,
pFile: CompletionItemKind.Function,
mlxFile: CompletionItemKind.Function,
mlappFile: CompletionItemKind.Function,
mex: CompletionItemKind.Function,
mdlFile: CompletionItemKind.Function,
slxFile: CompletionItemKind.Function,
slxpFile: CompletionItemKind.Function,
sscFile: CompletionItemKind.Function,
sscpFile: CompletionItemKind.Function,
sfxFile: CompletionItemKind.Class,
folder: CompletionItemKind.Folder,
logical: CompletionItemKind.Value,
function: CompletionItemKind.Function,
filename: CompletionItemKind.File,
localFunction: CompletionItemKind.Function,
fieldname: CompletionItemKind.Field,
username: CompletionItemKind.Text,
variable: CompletionItemKind.Variable,
feature: CompletionItemKind.Text,
cellString: CompletionItemKind.Value,
class: CompletionItemKind.Class,
package: CompletionItemKind.Module,
property: CompletionItemKind.Property,
method: CompletionItemKind.Method,
enumeration: CompletionItemKind.EnumMember,
messageId: CompletionItemKind.Text,
keyword: CompletionItemKind.Keyword,
attribute: CompletionItemKind.Keyword,
codeSnippet: CompletionItemKind.Snippet
}
/**
* Handles requests for completion-related features.
* Currently, this handles auto-completion as well as function signature help.
*/
class CompletionSupportProvider {
constructor (private readonly matlabLifecycleManager: MatlabLifecycleManager, private readonly mvm: MVM) {}
/**
* Handles a request for auto-completion choices.
*
* @param params Parameters from the onCompletion request
* @param documentManager The text document manager
* @returns An array of possible completions
*/
async handleCompletionRequest (params: CompletionParams, documentManager: TextDocuments<TextDocument>): Promise<CompletionList> {
const doc = documentManager.get(params.textDocument.uri)
if (doc == null) {
return CompletionList.create()
}
const completionData = await this.retrieveCompletionDataForDocument(doc, params.position)
return this.parseCompletionItems(completionData)
}
/**
* Returns completions for a give string
* @returns An array of possible completions
*/
async getCompletions (code: string, cursorOffset: number): Promise<CompletionList> {
const completionData = await this.retrieveCompletionData(code, '', cursorOffset);
return this.parseCompletionItems(completionData);
}
/**
* Handles a request for function signature help.
*
* @param params Parameters from the onSignatureHelp request
* @param documentManager The text document manager
* @returns The signature help, or null if no signature help is available
*/
async handleSignatureHelpRequest (params: SignatureHelpParams, documentManager: TextDocuments<TextDocument>): Promise<SignatureHelp | null> {
const doc = documentManager.get(params.textDocument.uri)
if (doc == null) {
return null
}
const completionData = await this.retrieveCompletionDataForDocument(doc, params.position)
return this.parseSignatureHelp(completionData)
}
/**
* Retrieves raw completion data from MATLAB.
*
* @param doc The text document
* @param position The cursor position in the document
* @returns The raw completion data
*/
private async retrieveCompletionDataForDocument (doc: TextDocument, position: Position): Promise<MCompletionData> {
const docUri = doc.uri
const code = doc.getText()
const fileName = FileNameUtils.getFilePathFromUri(docUri, true)
const cursorPosition = doc.offsetAt(position)
return await this.retrieveCompletionData(code, fileName, cursorPosition);
}
/**
* Retrieves raw completion data from MATLAB.
*
* @param code The code to be completed
* @param fileName The name of the file with the completion, or empty string if there is no file
* @param cursorPosition The cursor position in the code
* @returns The raw completion data
*/
private async retrieveCompletionData (code: string, fileName: string, cursorPosition: number): Promise<MCompletionData> {
if (!this.mvm.isReady()) {
// MVM not yet ready
return {}
}
try {
const response = await this.mvm.feval(
'matlabls.handlers.completions.getCompletions',
1,
[code, fileName, cursorPosition]
)
if ('error' in response) {
// Handle MVMError
Logger.error('Error received while retrieving completion data:')
Logger.error(response.error.msg)
return {}
}
return parse(response.result[0]) as MCompletionData
} catch (err) {
Logger.error('Error caught while retrieving completion data:')
Logger.error(err as string)
return {}
}
}
/**
* Parses the raw completion data to extract possible auto-completions.
*
* @param completionData The raw completion data
* @returns A list of completion items
*/
private parseCompletionItems (completionData: MCompletionData): CompletionList {
const completionItems: CompletionItem[] = []
const completionsMap = new Map<string, { kind: CompletionItemKind, doc: string, insertText: string }>()
// Gather completions from top-level object. This should find function completions.
this.gatherCompletions(completionData, completionsMap)
// Gather completions from each signature. This should find function argument completions.
let signatures = completionData.signatures
if (signatures != null) {
signatures = Array.isArray(signatures) ? signatures : [signatures]
signatures.forEach(signature => {
let inputArguments = signature.inputArguments
if (inputArguments == null) {
return
}
inputArguments = Array.isArray(inputArguments) ? inputArguments : [inputArguments]
inputArguments.forEach(inputArgument => {
this.gatherCompletions(inputArgument, completionsMap)
})
})
}
let index = 0
completionsMap.forEach((completionData, completionName) => {
// Preserve the sorting from MATLAB
const sortText = String(index).padStart(10, '0')
const completionItem = CompletionItem.create(completionName)
completionItem.kind = completionData.kind
completionItem.detail = completionData.doc
completionItem.data = index++
completionItem.sortText = sortText
if (completionData.kind === CompletionItemKind.Snippet) {
completionItem.insertText = completionData.insertText
completionItem.insertTextFormat = InsertTextFormat.Snippet
}
completionItems.push(completionItem)
})
return CompletionList.create(completionItems, completionData.widgetData?.truncated ?? false)
}
/**
* Parses raw completion and argument data and stores info about possible completions in the provided map.
*
* @param completionDataObj Raw completion or argument data
* @param completionMap A map in which to store info about possible completions
*/
private gatherCompletions (completionDataObj: MCompletionData | MArgumentData, completionMap: Map<string, { kind: CompletionItemKind, doc: string, insertText: string }>): void {
let choices = completionDataObj.widgetData?.choices
if (choices == null) {
return
}
choices = Array.isArray(choices) ? choices : [choices]
choices = this.filterSnippetChoices(choices);
choices.forEach(choice => {
let completion: string = choice.completion
let isPath = false
switch (choice.matchType) {
case 'folder':
case 'filename':
// For files and folders, the completion is the full path whereas the displayString is the path to be added
completion = choice.displayString ?? ''
isPath = true
break
case 'messageId':
// Remove quotes from completion
completion = (choice.displayString ?? '').replace(/['"]/g, '')
break
case 'codeSnippet':
completion = choice.displayString ?? ''
break
}
if (choice.matchType !== 'codeSnippet') {
const dotIdx = choice.completion.lastIndexOf('.')
if (dotIdx > 0 && !isPath) {
completion = completion.slice(dotIdx + 1)
}
}
completionMap.set(completion, {
kind: MatlabCompletionToKind[choice.matchType] ?? CompletionItemKind.Function,
doc: choice.purpose,
insertText: choice.completion ?? ''
})
})
}
/**
* Parses the raw completion data to extract function signature help.
*
* @param completionData The raw completion data
* @returns The signature help, or null if no signature help is available
*/
private parseSignatureHelp (completionData: MCompletionData): SignatureHelp | null {
let signatureData = completionData.signatures
if (signatureData == null) {
return null
}
signatureData = Array.isArray(signatureData) ? signatureData : [signatureData]
const signatureHelp: SignatureHelp = {
activeParameter: 0,
activeSignature: 0,
signatures: []
}
// Parse each signature
signatureData.forEach(sigData => {
const params: ParameterInformation[] = []
// Handle function inputs
const argNames: string[] = []
let inputArguments = sigData.inputArguments
if (inputArguments == null) {
return
}
inputArguments = Array.isArray(inputArguments) ? inputArguments : [inputArguments]
inputArguments.forEach((inputArg, index) => {
let paramDoc = ''
if (inputArg.purpose != null) {
paramDoc += inputArg.purpose
}
if (inputArg.valueSummary != null) {
paramDoc += (paramDoc.length > 0 ? '\n' : '') + inputArg.valueSummary
}
const paramDocArgs = paramDoc.length > 0 ? [paramDoc] : []
params.push(ParameterInformation.create(inputArg.name, ...paramDocArgs))
argNames.push(inputArg.name)
if (inputArg.status === 'presenting') {
signatureHelp.activeParameter = index
}
})
let argStr = ''
if (argNames.length === 1) {
argStr = argNames[0]
} else if (argNames.length > 1) {
argStr = argNames.join(', ')
}
// Handle function outputs
let outStr = ''
let outputArguments = sigData.outputArguments
if (outputArguments != null) {
outputArguments = Array.isArray(outputArguments) ? outputArguments : [outputArguments]
outStr = outputArguments.length === 1
? outputArguments[0].name
: `[${outputArguments.map(output => output.name).join(', ')}]`
outStr += ' = '
}
const id = `${outStr}${sigData.functionName}(${argStr})`
signatureHelp.signatures.push(SignatureInformation.create(
id,
undefined,
...params
))
})
return signatureHelp
}
/**
* Filters out the snippet choices from the list of choices based on the configured snippet ignore list.
*
* @param choices The list of completion choices to filter
* @returns The list of completion choices with snippet choices filtered out based on the configured snippet ignore list
*/
private filterSnippetChoices (choices: MCompletionChoice[]): MCompletionChoice[] {
// Get the snippet ignore list from the configuration manager
const snippetIgnoreList = ConfigurationManager.getArgument(Argument.SnippetIgnoreList).split(';');
return choices.filter(choice => {
return choice.matchType !== 'codeSnippet' || (choice.displayString !== undefined && !snippetIgnoreList.includes(choice.displayString));
});
}
}
export default CompletionSupportProvider