check position for function snippet

This commit is contained in:
chemzqm 2018-11-24 20:56:49 +08:00
parent ecd2189c15
commit ce1aff4981

View file

@ -223,7 +223,10 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP
if (command) item.command = command
item.additionalTextEdits = additionalTextEdits
if (detail && item.insertTextFormat == InsertTextFormat.Snippet) {
this.createSnippetOfFunctionCall(item, detail)
const shouldCompleteFunction = await this.isValidFunctionCompletionContext(filepath, position, token)
if (shouldCompleteFunction) {
this.createSnippetOfFunctionCall(item, detail)
}
}
return item
@ -395,6 +398,35 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP
textEdit.newText = snippet
item.textEdit = textEdit
}
private async isValidFunctionCompletionContext(
filepath: string,
position: Position,
token: CancellationToken
): Promise<boolean> {
// Workaround for https://github.com/Microsoft/TypeScript/issues/12677
// Don't complete function calls inside of destructive assigments or imports
try {
const args: Proto.FileLocationRequestArgs = typeConverters.Position.toFileLocationRequestArgs(filepath, position)
const response = await this.client.execute('quickinfo', args, token)
if (response.type !== 'response') {
return true
}
const { body } = response
switch (body && body.kind) {
case 'var':
case 'let':
case 'const':
case 'alias':
return false
default:
return true
}
} catch (e) {
return true
}
}
}
function shouldExcludeCompletionEntry(