From 6310e818b86509dec4d7c6b5dff831a89edaf1f9 Mon Sep 17 00:00:00 2001 From: johnsoncodehk Date: Mon, 26 Dec 2022 01:31:10 +0800 Subject: [PATCH] fix: revert client autoInsertion.ts to fix #2238 close #2238 --- .../src/features/autoInsertion.ts | 45 ++++++++----------- .../src/plugins/vue-autoinsert-space.ts | 28 ++++++------ 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/packages/vscode-language-client/src/features/autoInsertion.ts b/packages/vscode-language-client/src/features/autoInsertion.ts index 85557a019..603f23fb9 100644 --- a/packages/vscode-language-client/src/features/autoInsertion.ts +++ b/packages/vscode-language-client/src/features/autoInsertion.ts @@ -30,13 +30,7 @@ export async function register( } function onDidChangeTextDocument({ document, contentChanges, reason }: vscode.TextDocumentChangeEvent) { - if ( - !isEnabled - || contentChanges.length !== 1 - || !contentChanges[0].text // delete - || reason === vscode.TextDocumentChangeReason.Undo - || reason === vscode.TextDocumentChangeReason.Redo - ) { + if (!isEnabled || contentChanges.length === 0 || reason === vscode.TextDocumentChangeReason.Undo || reason === vscode.TextDocumentChangeReason.Redo) { return; } const activeDocument = vscode.window.activeTextEditor?.document; @@ -85,33 +79,32 @@ export async function register( lastChange: vscode.TextDocumentContentChangeEvent, provider: (document: vscode.TextDocument, position: vscode.Position, lastChange: vscode.TextDocumentContentChangeEvent, isCancel: () => boolean) => Thenable, ) { + const rangeStart = lastChange.range.start; const version = document.version; timeout = setTimeout(() => { - const position = vscode.window.activeTextEditor?.selections.length === 1 && vscode.window.activeTextEditor.selections[0].active; - if (position) { - provider(document, position, lastChange, () => vscode.window.activeTextEditor?.document.version !== version).then(text => { - if (text && isEnabled) { - const activeEditor = vscode.window.activeTextEditor; - if (activeEditor) { - const activeDocument = activeEditor.document; - if (document === activeDocument && activeDocument.version === version) { - if (typeof text === 'string') { - const selections = activeEditor.selections; - if (selections.length && selections.some(s => s.active.isEqual(position))) { - activeEditor.insertSnippet(new vscode.SnippetString(text), selections.map(s => s.active)); - } - else { - activeEditor.insertSnippet(new vscode.SnippetString(text), position); - } + const position = new vscode.Position(rangeStart.line, rangeStart.character + lastChange.text.length); + provider(document, position, lastChange, () => vscode.window.activeTextEditor?.document.version !== version).then(text => { + if (text && isEnabled) { + const activeEditor = vscode.window.activeTextEditor; + if (activeEditor) { + const activeDocument = activeEditor.document; + if (document === activeDocument && activeDocument.version === version) { + if (typeof text === 'string') { + const selections = activeEditor.selections; + if (selections.length && selections.some(s => s.active.isEqual(position))) { + activeEditor.insertSnippet(new vscode.SnippetString(text), selections.map(s => s.active)); } else { - activeEditor.insertSnippet(new vscode.SnippetString(text.newText), text.range); + activeEditor.insertSnippet(new vscode.SnippetString(text), position); } } + else { + activeEditor.insertSnippet(new vscode.SnippetString(text.newText), text.range); + } } } - }); - } + } + }); timeout = undefined; }, 100); } diff --git a/vue-language-tools/vue-language-service/src/plugins/vue-autoinsert-space.ts b/vue-language-tools/vue-language-service/src/plugins/vue-autoinsert-space.ts index 114e3b603..b38bd8d8c 100644 --- a/vue-language-tools/vue-language-service/src/plugins/vue-autoinsert-space.ts +++ b/vue-language-tools/vue-language-service/src/plugins/vue-autoinsert-space.ts @@ -10,7 +10,7 @@ export default function (): LanguageServicePlugin { context = _context; }, - async doAutoInsert(document, position) { + async doAutoInsert(document, _, { lastChange }) { if (document.languageId === 'html' || document.languageId === 'jade') { @@ -18,18 +18,20 @@ export default function (): LanguageServicePlugin { if (!enabled) return; - const prev = document.getText({ - start: { line: position.line, character: position.character - 2 }, - end: position, - }); - if (prev === '{{') { - const next = document.getText({ - start: position, - end: { line: position.line, character: position.character + 2 }, - }); - if (next === '}}') { - return ` $0 `; - } + if ( + lastChange.text === '{}' + && document.getText({ + start: { line: lastChange.range.start.line, character: lastChange.range.start.character - 1 }, + end: { line: lastChange.range.start.line, character: lastChange.range.start.character + 3 } + }) === '{{}}' + ) { + return { + newText: ` $0 `, + range: { + start: { line: lastChange.range.start.line, character: lastChange.range.start.character + 1 }, + end: { line: lastChange.range.start.line, character: lastChange.range.start.character + 1 } + }, + }; } } },