Use snippet for jsdoc completion. Fixes #21105

This commit is contained in:
Matt Bierner 2017-02-23 14:07:59 -08:00
parent 8faa03f885
commit f9d76beb24

View file

@ -5,7 +5,7 @@
'use strict'; 'use strict';
import { Position, Selection, Range, CompletionItemProvider, CompletionItemKind, TextDocument, CancellationToken, CompletionItem, window, commands, Uri, ProviderResult, TextEditor, SnippetString } from 'vscode'; import { Position, Range, CompletionItemProvider, CompletionItemKind, TextDocument, CancellationToken, CompletionItem, window, commands, Uri, ProviderResult, TextEditor, SnippetString } from 'vscode';
import { ITypescriptServiceClient } from '../typescriptService'; import { ITypescriptServiceClient } from '../typescriptService';
import { FileLocationRequestArgs, DocCommandTemplateResponse } from '../protocol'; import { FileLocationRequestArgs, DocCommandTemplateResponse } from '../protocol';
@ -122,18 +122,28 @@ export default class JsDocCompletionHelper implements CompletionItemProvider {
if (!res || !res.body) { if (!res || !res.body) {
return false; return false;
} }
const commentText = res.body.newText; return editor.insertSnippet(
return editor.edit( this.templateToSnippet(res.body.newText),
edits => edits.insert(position, commentText), position,
{ undoStopBefore: false, undoStopAfter: true }); { undoStopBefore: false, undoStopAfter: true });
}, () => false) }, () => false);
.then((didInsertComment: boolean) => { }
if (didInsertComment) {
const newCursorPosition = new Position(position.line + 1, editor.document.lineAt(position.line + 1).text.length); private templateToSnippet(template: string): SnippetString {
editor.selection = new Selection(newCursorPosition, newCursorPosition); let snippetIndex = 1;
} template = template.replace(/^\s*(?=(\/|[ ]\*))/gm, '');
return didInsertComment; template = template.replace(/^(\/\*\*\s*\*[ ]*)$/m, (x) => x + `\${${snippetIndex++}}`);
}); template = template.replace(/\* @param([ ]\{\S+\})?\s+(\S+)\s*$/gm, (_param, type, post) => {
let out = '* @param ';
if (type === ' {any}') {
out += `{\$\{${snippetIndex++}:any\}} `;
} else if (type) {
out += type + ' ';
}
out += post + ` \${${snippetIndex++}}`;
return out;
});
return new SnippetString(template);
} }
/** /**