[html] auto close triggers on undo/redo and breaks undo/redo history. Fixes #34484

This commit is contained in:
Martin Aeschlimann 2021-09-16 17:43:47 +02:00
parent da0fa2dc8f
commit 5aebde7b48
No known key found for this signature in database
GPG key ID: 2609A01E695523E3

View file

@ -3,13 +3,13 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { window, workspace, Disposable, TextDocumentContentChangeEvent, TextDocument, Position, SnippetString } from 'vscode';
import { window, workspace, Disposable, TextDocument, Position, SnippetString, TextDocumentChangeEvent, TextDocumentChangeReason } from 'vscode';
import { Runtime } from './htmlClient';
export function activateTagClosing(tagProvider: (document: TextDocument, position: Position) => Thenable<string>, supportedLanguages: { [id: string]: boolean }, configName: string, runtime: Runtime): Disposable {
let disposables: Disposable[] = [];
workspace.onDidChangeTextDocument(event => onDidChangeTextDocument(event.document, event.contentChanges), null, disposables);
const disposables: Disposable[] = [];
workspace.onDidChangeTextDocument(onDidChangeTextDocument, null, disposables);
let isEnabled = false;
updateEnabledState();
@ -25,11 +25,11 @@ export function activateTagClosing(tagProvider: (document: TextDocument, positio
function updateEnabledState() {
isEnabled = false;
let editor = window.activeTextEditor;
const editor = window.activeTextEditor;
if (!editor) {
return;
}
let document = editor.document;
const document = editor.document;
if (!supportedLanguages[document.languageId]) {
return;
}
@ -39,33 +39,34 @@ export function activateTagClosing(tagProvider: (document: TextDocument, positio
isEnabled = true;
}
function onDidChangeTextDocument(document: TextDocument, changes: readonly TextDocumentContentChangeEvent[]) {
if (!isEnabled) {
function onDidChangeTextDocument({ document, contentChanges, reason }: TextDocumentChangeEvent) {
if (!isEnabled || contentChanges.length === 0 || reason === TextDocumentChangeReason.Undo) {
return;
}
let activeDocument = window.activeTextEditor && window.activeTextEditor.document;
if (document !== activeDocument || changes.length === 0) {
const activeDocument = window.activeTextEditor && window.activeTextEditor.document;
if (document !== activeDocument) {
return;
}
if (timeout) {
timeout.dispose();
}
let lastChange = changes[changes.length - 1];
let lastCharacter = lastChange.text[lastChange.text.length - 1];
const lastChange = contentChanges[contentChanges.length - 1];
const lastCharacter = lastChange.text[lastChange.text.length - 1];
if (lastChange.rangeLength > 0 || lastCharacter !== '>' && lastCharacter !== '/') {
return;
}
let rangeStart = lastChange.range.start;
let version = document.version;
const rangeStart = lastChange.range.start;
const version = document.version;
timeout = runtime.timer.setTimeout(() => {
let position = new Position(rangeStart.line, rangeStart.character + lastChange.text.length);
const position = new Position(rangeStart.line, rangeStart.character + lastChange.text.length);
tagProvider(document, position).then(text => {
if (text && isEnabled) {
let activeEditor = window.activeTextEditor;
const activeEditor = window.activeTextEditor;
if (activeEditor) {
let activeDocument = activeEditor.document;
const activeDocument = activeEditor.document;
if (document === activeDocument && activeDocument.version === version) {
let selections = activeEditor.selections;
const selections = activeEditor.selections;
if (selections.length && selections.some(s => s.active.isEqual(position))) {
activeEditor.insertSnippet(new SnippetString(text), selections.map(s => s.active));
} else {