Skip to content

Commit 02ba623

Browse files
authored
fix: replace the closing symbol for inputs/outputs. (#2142)
The Angular Language Service does not return `InsertReplaceEdit`. There is no need to allow the developer to choose how to insert the completion. For example, `<button (c|) />`. ^^__________Insert edit ^^ ^________Replace edit If the LS returns the `InsertReplaceEdit` as shown above, selecting "Insert" by the developer results in `(click)="")`, and selecting "Replace" results in `(click)=""`. Now in the vscode, the default `editor.suggest.insertMode` value for HTML is `Replace`, for ts is `Insert`, So this leads to a bug in the ts file. Fixes #2137
1 parent 8adc17a commit 02ba623

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

server/src/completion.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ function ngCompletionKindToLspCompletionItemKind(kind: CompletionKind): lsp.Comp
106106
* @param scriptInfo
107107
*/
108108
export function tsCompletionEntryToLspCompletionItem(
109-
entry: ts.CompletionEntry, position: lsp.Position, scriptInfo: ts.server.ScriptInfo,
110-
insertReplaceSupport: boolean): lsp.CompletionItem {
109+
entry: ts.CompletionEntry, position: lsp.Position,
110+
scriptInfo: ts.server.ScriptInfo): lsp.CompletionItem {
111111
const item = lsp.CompletionItem.create(entry.name);
112112
// Even though `entry.kind` is typed as ts.ScriptElementKind, it's
113113
// really Angular's CompletionKind. This is because ts.ScriptElementKind does
@@ -121,7 +121,7 @@ export function tsCompletionEntryToLspCompletionItem(
121121
// from 'entry.name'. For example, a method name could be 'greet', but the
122122
// insertText is 'greet()'.
123123
const insertText = entry.insertText || entry.name;
124-
item.textEdit = createTextEdit(scriptInfo, entry, position, insertText, insertReplaceSupport);
124+
item.textEdit = createTextEdit(scriptInfo, entry, position, insertText);
125125

126126
// If the user enables the config `includeAutomaticOptionalChainCompletions`, the `insertText`
127127
// range will include the dot. the `insertText` should be assigned to the `filterText` to filter
@@ -141,18 +141,27 @@ export function tsCompletionEntryToLspCompletionItem(
141141

142142
function createTextEdit(
143143
scriptInfo: ts.server.ScriptInfo, entry: ts.CompletionEntry, position: lsp.Position,
144-
insertText: string, insertReplaceSupport: boolean) {
144+
insertText: string) {
145145
if (entry.replacementSpan === undefined) {
146146
return lsp.TextEdit.insert(position, insertText);
147-
} else if (insertReplaceSupport) {
148-
const replacementRange = tsTextSpanToLspRange(scriptInfo, entry.replacementSpan);
149-
const tsPosition = lspPositionToTsPosition(scriptInfo, position);
150-
const insertLength = tsPosition - entry.replacementSpan.start;
151-
const insertionRange =
152-
tsTextSpanToLspRange(scriptInfo, {...entry.replacementSpan, length: insertLength});
153-
return lsp.InsertReplaceEdit.create(insertText, insertionRange, replacementRange);
154147
} else {
148+
/**
149+
* The Angular Language Service does not return `InsertReplaceEdit`.
150+
* There is no need to allow the developer to choose how to insert the completion.
151+
*
152+
* For example, `<button (c|) />`.
153+
* ^^__________Insert edit
154+
* ^^ ^________Replace edit
155+
*
156+
* If the LS returns the `InsertReplaceEdit` as shown above, selecting "Insert" by the developer
157+
* results in `(click)="")`, and selecting "Replace" results in `(click)=""`.
158+
*
159+
* Now in the vscode, the default `editor.suggest.insertMode` value for HTML is `Replace`, for
160+
* ts is `Insert`, So this leads to a bug in the ts file.
161+
*
162+
* Fixes https://github.com/angular/vscode-ng-language-service/issues/2137
163+
*/
155164
return lsp.TextEdit.replace(
156165
tsTextSpanToLspRange(scriptInfo, entry.replacementSpan), insertText);
157166
}
158-
}
167+
}

server/src/session.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,12 +1236,8 @@ export class Session {
12361236
if (!completions) {
12371237
return null;
12381238
}
1239-
const clientSupportsInsertReplaceCompletion =
1240-
this.clientCapabilities.textDocument?.completion?.completionItem?.insertReplaceSupport ??
1241-
false;
12421239
return completions.entries.map(
1243-
(e) => tsCompletionEntryToLspCompletionItem(
1244-
e, params.position, scriptInfo, clientSupportsInsertReplaceCompletion));
1240+
(e) => tsCompletionEntryToLspCompletionItem(e, params.position, scriptInfo));
12451241
}
12461242

12471243
private onCompletionResolve(item: lsp.CompletionItem): lsp.CompletionItem {

0 commit comments

Comments
 (0)