Skip to content

Commit f9d76be

Browse files
committed
Use snippet for jsdoc completion. Fixes microsoft#21105
1 parent 8faa03f commit f9d76be

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

extensions/typescript/src/features/jsDocCompletionProvider.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict';
77

8-
import { Position, Selection, Range, CompletionItemProvider, CompletionItemKind, TextDocument, CancellationToken, CompletionItem, window, commands, Uri, ProviderResult, TextEditor, SnippetString } from 'vscode';
8+
import { Position, Range, CompletionItemProvider, CompletionItemKind, TextDocument, CancellationToken, CompletionItem, window, commands, Uri, ProviderResult, TextEditor, SnippetString } from 'vscode';
99

1010
import { ITypescriptServiceClient } from '../typescriptService';
1111
import { FileLocationRequestArgs, DocCommandTemplateResponse } from '../protocol';
@@ -122,18 +122,28 @@ export default class JsDocCompletionHelper implements CompletionItemProvider {
122122
if (!res || !res.body) {
123123
return false;
124124
}
125-
const commentText = res.body.newText;
126-
return editor.edit(
127-
edits => edits.insert(position, commentText),
125+
return editor.insertSnippet(
126+
this.templateToSnippet(res.body.newText),
127+
position,
128128
{ undoStopBefore: false, undoStopAfter: true });
129-
}, () => false)
130-
.then((didInsertComment: boolean) => {
131-
if (didInsertComment) {
132-
const newCursorPosition = new Position(position.line + 1, editor.document.lineAt(position.line + 1).text.length);
133-
editor.selection = new Selection(newCursorPosition, newCursorPosition);
134-
}
135-
return didInsertComment;
136-
});
129+
}, () => false);
130+
}
131+
132+
private templateToSnippet(template: string): SnippetString {
133+
let snippetIndex = 1;
134+
template = template.replace(/^\s*(?=(\/|[ ]\*))/gm, '');
135+
template = template.replace(/^(\/\*\*\s*\*[ ]*)$/m, (x) => x + `\${${snippetIndex++}}`);
136+
template = template.replace(/\* @param([ ]\{\S+\})?\s+(\S+)\s*$/gm, (_param, type, post) => {
137+
let out = '* @param ';
138+
if (type === ' {any}') {
139+
out += `{\$\{${snippetIndex++}:any\}} `;
140+
} else if (type) {
141+
out += type + ' ';
142+
}
143+
out += post + ` \${${snippetIndex++}}`;
144+
return out;
145+
});
146+
return new SnippetString(template);
137147
}
138148

139149
/**

0 commit comments

Comments
 (0)