diff --git a/org.eclipse.wildwebdeveloper.tests/src/org/eclipse/wildwebdeveloper/tests/TestHTML.java b/org.eclipse.wildwebdeveloper.tests/src/org/eclipse/wildwebdeveloper/tests/TestHTML.java
index 2523c14d04..a73e2be385 100644
--- a/org.eclipse.wildwebdeveloper.tests/src/org/eclipse/wildwebdeveloper/tests/TestHTML.java
+++ b/org.eclipse.wildwebdeveloper.tests/src/org/eclipse/wildwebdeveloper/tests/TestHTML.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2019, 2024 Red Hat Inc. and others.
+ * Copyright (c) 2019, 2026 Red Hat Inc. and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
@@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.wildwebdeveloper.tests;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.eclipse.core.resources.IFile;
@@ -104,10 +105,39 @@ public void autoCloseTags() throws Exception {
ITextEditor editor = (ITextEditor) IDE
.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file);
IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
+ editor.getSelectionProvider().setSelection(new TextSelection(document, 4, 0));
document.replace(4, 0, ">");
+ editor.getSelectionProvider().setSelection(new TextSelection(document, 5, 0));
assertTrue(
DisplayHelper.waitForCondition(PlatformUI.getWorkbench().getDisplay(), 5000,
() -> "".equals(document.get())),
"Autoclose not done");
}
+
+ @Test
+ public void autoCloseIgnoresStaleResponse() throws Exception {
+ final IProject project = ResourcesPlugin.getWorkspace().getRoot()
+ .getProject("testHTMLFile" + System.currentTimeMillis());
+ project.create(null);
+ project.open(null);
+ final IFile file = project.getFile("autoCloseStale.html");
+ file.create("");
+ // moved away before the response can be processed on this thread
+ editor.getSelectionProvider().setSelection(new TextSelection(document, 0, 0));
+ // a second request, answered after the first one
+ editor.getSelectionProvider().setSelection(new TextSelection(document, 4, 0));
+ document.replace(4, 0, ">");
+ editor.getSelectionProvider().setSelection(new TextSelection(document, 5, 0));
+ assertTrue(
+ DisplayHelper.waitForCondition(PlatformUI.getWorkbench().getDisplay(), 5000,
+ () -> document.get().startsWith("")),
+ "Autoclose not done");
+ assertFalse(document.get().contains(""), "Stale response was applied");
+ }
}
diff --git a/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/html/autoinsert/HTMLAutoInsertReconciler.java b/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/html/autoinsert/HTMLAutoInsertReconciler.java
index efd268e6a4..eca27fa322 100644
--- a/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/html/autoinsert/HTMLAutoInsertReconciler.java
+++ b/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/html/autoinsert/HTMLAutoInsertReconciler.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2022, 2025 Red Hat Inc. and others.
+ * Copyright (c) 2022, 2026 Red Hat Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@@ -32,6 +32,7 @@
import org.eclipse.wildwebdeveloper.Activator;
import org.eclipse.wildwebdeveloper.html.HTMLLanguageServerAPI;
import org.eclipse.wildwebdeveloper.html.autoinsert.AutoInsertParams.AutoInsertKind;
+import org.eclipse.wildwebdeveloper.util.EditorSnapshot;
/**
* {@link IReconciler} implementation used to support auto close tags / auto
@@ -56,12 +57,13 @@ private void autoInsert(DocumentEvent event) {
if (event == null || viewer == null) {
return;
}
- IDocument document = event.getDocument();
+ final ITextViewer currentViewer = viewer;
+ final IDocument document = event.getDocument();
if (document == null || event.getLength() != 0 || event.getText().length() != 1) {
return;
}
- int offset = event.getOffset() + 1;
+ final int offset = event.getOffset() + 1;
char c = event.getText().charAt(0);
if (c != '>' && c != '/' && c != '=') {
return;
@@ -84,49 +86,42 @@ private void autoInsert(DocumentEvent event) {
break;
}
+ final EditorSnapshot snapshot = EditorSnapshot.of(currentViewer, document, offset);
+ final Display display = currentViewer.getTextWidget().getDisplay();
TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri.toString());
LanguageServers.forDocument(document).collectAll((w, ls) -> CompletableFuture.completedFuture(ls))
.thenAccept(lss -> lss.stream().filter(HTMLLanguageServerAPI.class::isInstance)
.map(HTMLLanguageServerAPI.class::cast).findAny().ifPresent(info -> {
- // The document is bound with HTML language server, consumes the html/autoInsert
- final Display display = viewer.getTextWidget().getDisplay();
- CompletableFuture.supplyAsync(() -> {
+ CompletableFuture.runAsync(() -> {
try {
AutoInsertParams params = new AutoInsertParams();
params.setTextDocument(identifier);
params.setKind(autoInsertKind.name());
params.setPosition(LSPEclipseUtils.toPosition(offset, document));
- // consumes html/autoInsert from HTML language server
- info.autoInsert(params)
- .thenAccept(r -> {
- if (r != null) {
- display.asyncExec(() -> {
- try {
- // we receive a text like
- // $0
- // $0 should be used for set the cursor.
- String text = r.replace("$0", "").replace("$1", "");
- int index = r.indexOf("$1");
-
- int replaceLength = 0;
- document.replace(offset, replaceLength, text);
- if (index != -1) {
- viewer.setSelectedRange(offset + index, 0);
- }
- // viewer.setSelectedRange(offset, c)
- } catch (BadLocationException e) {
- // Do nothing
- }
- });
-
+ info.autoInsert(params).thenAccept(r -> {
+ if (r != null) {
+ display.asyncExec(() -> {
+ if (viewer != currentViewer || !snapshot.isCurrent()) {
+ return;
+ }
+ try {
+ String text = r.replace("$0", "").replace("$1", "");
+ int index = r.indexOf("$1");
+ document.replace(offset, 0, text);
+ if (index != -1) {
+ currentViewer.setSelectedRange(offset + index, 0);
+ }
+ } catch (BadLocationException e) {
+ // Do nothing
}
});
+ }
+ });
} catch (BadLocationException e) {
// Do nothing
}
- return null;
});
}));
}
diff --git a/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/util/EditorSnapshot.java b/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/util/EditorSnapshot.java
new file mode 100644
index 0000000000..486b237ccc
--- /dev/null
+++ b/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/util/EditorSnapshot.java
@@ -0,0 +1,43 @@
+/*******************************************************************************
+ * Copyright (c) 2026 Vogella GmbH and others.
+ *
+ * This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License 2.0
+ * which is available at https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Lars Vogel (Vogella GmbH) - initial implementation
+ *******************************************************************************/
+package org.eclipse.wildwebdeveloper.util;
+
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IDocumentExtension4;
+import org.eclipse.jface.text.ITextViewer;
+
+/**
+ * Editor state at the time of an asynchronous request, to detect responses which no longer apply.
+ */
+public record EditorSnapshot(ITextViewer viewer, IDocument document, long modificationStamp, int caretOffset) {
+
+ public static EditorSnapshot of(ITextViewer viewer, IDocument document, int caretOffset) {
+ return new EditorSnapshot(viewer, document, modificationStampOf(document), caretOffset);
+ }
+
+ /** Must be called on the UI thread. */
+ public boolean isCurrent() {
+ var widget = viewer.getTextWidget();
+ if (widget == null || widget.isDisposed() || viewer.getDocument() != document
+ || modificationStampOf(document) != modificationStamp) {
+ return false;
+ }
+ var selection = viewer.getSelectedRange();
+ return selection.x == caretOffset && selection.y == 0;
+ }
+
+ private static long modificationStampOf(IDocument document) {
+ return document instanceof IDocumentExtension4 extension ? extension.getModificationStamp()
+ : IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP;
+ }
+}
diff --git a/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/vue/autoinsert/VueAutoInsertReconciler.java b/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/vue/autoinsert/VueAutoInsertReconciler.java
index 9391dc061c..7182362ba2 100644
--- a/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/vue/autoinsert/VueAutoInsertReconciler.java
+++ b/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/vue/autoinsert/VueAutoInsertReconciler.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2022, 2025 Red Hat Inc. and others.
+ * Copyright (c) 2022, 2026 Red Hat Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@@ -30,6 +30,7 @@
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.swt.widgets.Display;
+import org.eclipse.wildwebdeveloper.util.EditorSnapshot;
import org.eclipse.wildwebdeveloper.vue.VueLanguageServerAPI;
/**
@@ -50,33 +51,33 @@ private void autoInsert(DocumentEvent event) {
if (event == null || viewer == null) {
return;
}
- IDocument document = event.getDocument();
+ final ITextViewer currentViewer = viewer;
+ final IDocument document = event.getDocument();
if (document == null || event.getLength() != 0) {
return;
}
- int offset = event.getOffset();
+ final int offset = event.getOffset();
+ final int selectionOffset = offset + event.getText().length();
URI uri = LSPEclipseUtils.toUri(document);
if (uri == null) {
return;
}
+ final EditorSnapshot snapshot = EditorSnapshot.of(currentViewer, document, selectionOffset);
+ final Display display = currentViewer.getTextWidget().getDisplay();
TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri.toString());
LanguageServers.forDocument(document).collectAll((w, ls) -> CompletableFuture.completedFuture(ls))
.thenAccept(lss -> lss.stream().filter(VueLanguageServerAPI.class::isInstance)
.map(VueLanguageServerAPI.class::cast).findAny().ifPresent(info -> {
- // The document is bound with HTML language server, consumes the html/autoInsert
- final Display display = viewer.getTextWidget().getDisplay();
- CompletableFuture.supplyAsync(() -> {
-
+ CompletableFuture.runAsync(() -> {
try {
-
AutoInsertParams params = new AutoInsertParams();
params.setTextDocument(identifier);
- params.setSelection(LSPEclipseUtils.toPosition(offset + event.getText().length(), document));
-
+ params.setSelection(LSPEclipseUtils.toPosition(selectionOffset, document));
+
AutoInsertLastChange changeEvent = new AutoInsertLastChange();
final var range = new Range(LSPEclipseUtils.toPosition(offset, document),
LSPEclipseUtils.toPosition(offset + event.fLength, document));
@@ -86,44 +87,33 @@ private void autoInsert(DocumentEvent event) {
changeEvent.setRangeOffset(offset);
params.setChange(changeEvent);
- // consumes String or AutoInsertResponse from Vue Server
- info.autoInsert(params)
- .thenAccept(response -> {
- if (response != null) {
- display.asyncExec(() -> {
- try {
- // we receive a text like
- // $0
- // $0 should be used for set the cursor.
- String newText = response.map(Function.identity(), AutoInsertResponse::getNewText);
-
- String text = newText.replace("$0", "").replace("$1", "");
-
-
- int index = newText.indexOf("$0");
-
- int replaceLength = 0;
- int replacePosition = offset + event.getText().length();
- if (response.isRight()) {
- replacePosition = LSPEclipseUtils.toOffset(response.getRight().getRange().getStart(), document);
- }
- document.replace(replacePosition, replaceLength, text);
- if (index != -1) {
- viewer.setSelectedRange(replacePosition + index, 0);
- }
- // viewer.setSelectedRange(offset, c)
- } catch (BadLocationException e) {
- ILog.get().error(e.getMessage(), e);
- }
- });
-
+ info.autoInsert(params).thenAccept(response -> {
+ if (response != null) {
+ display.asyncExec(() -> {
+ if (viewer != currentViewer || !snapshot.isCurrent()) {
+ return;
+ }
+ try {
+ String newText = response.map(Function.identity(), AutoInsertResponse::getNewText);
+ String text = newText.replace("$0", "").replace("$1", "");
+ int index = newText.indexOf("$0");
+ int replacePosition = selectionOffset;
+ if (response.isRight()) {
+ replacePosition = LSPEclipseUtils.toOffset(response.getRight().getRange().getStart(), document);
+ }
+ document.replace(replacePosition, 0, text);
+ if (index != -1) {
+ currentViewer.setSelectedRange(replacePosition + index, 0);
+ }
+ } catch (BadLocationException e) {
+ ILog.get().error(e.getMessage(), e);
}
});
+ }
+ });
} catch (BadLocationException e) {
- // Do nothing
ILog.get().error(e.getMessage(), e);
}
- return null;
});
}));
}
@@ -139,7 +129,6 @@ public void documentAboutToBeChanged(DocumentEvent e) {
@Override
public void documentChanged(DocumentEvent e) {
- System.out.println(e.toString());
autoInsert(e);
}