|
5 | 5 |
|
6 | 6 | 'use strict'; |
7 | 7 |
|
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'; |
9 | 9 |
|
10 | 10 | import { ITypescriptServiceClient } from '../typescriptService'; |
11 | 11 | import { FileLocationRequestArgs, DocCommandTemplateResponse } from '../protocol'; |
@@ -122,18 +122,28 @@ export default class JsDocCompletionHelper implements CompletionItemProvider { |
122 | 122 | if (!res || !res.body) { |
123 | 123 | return false; |
124 | 124 | } |
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, |
128 | 128 | { 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); |
137 | 147 | } |
138 | 148 |
|
139 | 149 | /** |
|
0 commit comments