Skip to content

Commit d6f7b6a

Browse files
Keen Yee Liauayazhafiz
authored andcommitted
fix: use replacementSpan for completion if provided (#442)
The language service returns an optional replacement span, but the information is not propagated to the editor.
1 parent 7007563 commit d6f7b6a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

server/src/completion.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import * as ts from 'typescript/lib/tsserverlibrary';
910
import * as lsp from 'vscode-languageserver';
1011

12+
import {tsTextSpanToLspRange} from './utils';
13+
1114
// TODO: Move this to `@angular/language-service`.
1215
enum 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
*/
6165
export 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
}

server/src/session.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ export class Session {
353353
if (!completions) {
354354
return;
355355
}
356-
return completions.entries.map((e) => tsCompletionEntryToLspCompletionItem(e, position));
356+
return completions.entries.map(
357+
(e) => tsCompletionEntryToLspCompletionItem(e, position, scriptInfo));
357358
}
358359

359360
/**

0 commit comments

Comments
 (0)