66 * found in the LICENSE file at https://angular.io/license
77 */
88
9+ import * as ts from 'typescript/lib/tsserverlibrary' ;
910import * as lsp from 'vscode-languageserver' ;
1011
12+ import { tsTextSpanToLspRange } from './utils' ;
13+
1114// TODO: Move this to `@angular/language-service`.
1215enum CompletionKind {
1316 attribute = 'attribute' ,
@@ -57,9 +60,11 @@ function ngCompletionKindToLspCompletionItemKind(kind: CompletionKind): lsp.Comp
5760 * Convert ts.CompletionEntry to LSP Completion Item.
5861 * @param entry completion entry
5962 * @param position position where completion is requested.
63+ * @param scriptInfo
6064 */
6165export function tsCompletionEntryToLspCompletionItem (
62- entry : ts . CompletionEntry , position : lsp . Position ) : lsp . CompletionItem {
66+ entry : ts . CompletionEntry , position : lsp . Position ,
67+ scriptInfo : ts . server . ScriptInfo ) : lsp . CompletionItem {
6368 const item = lsp . CompletionItem . create ( entry . name ) ;
6469 // Even though `entry.kind` is typed as ts.ScriptElementKind, it's
6570 // really Angular's CompletionKind. This is because ts.ScriptElementKind does
@@ -69,6 +74,12 @@ export function tsCompletionEntryToLspCompletionItem(
6974 item . kind = ngCompletionKindToLspCompletionItemKind ( kind ) ;
7075 item . detail = entry . kind ;
7176 item . sortText = entry . sortText ;
72- item . textEdit = lsp . TextEdit . insert ( position , entry . name ) ;
77+ // Text that actually gets inserted to the document. It could be different
78+ // from 'entry.name'. For example, a method name could be 'greet', but the
79+ // insertText is 'greet()'.
80+ const insertText = entry . insertText || entry . name ;
81+ item . textEdit = entry . replacementSpan ?
82+ lsp . TextEdit . replace ( tsTextSpanToLspRange ( scriptInfo , entry . replacementSpan ) , insertText ) :
83+ lsp . TextEdit . insert ( position , insertText ) ;
7384 return item ;
7485}
0 commit comments