Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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,
() -> "<foo></foo>".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("<foo\n<bar".getBytes(), true, false, null);
ITextEditor editor = (ITextEditor) IDE
.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file);
IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
final int end = document.getLength();
editor.getSelectionProvider().setSelection(new TextSelection(document, end, 0));
document.replace(end, 0, ">");
// 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("<foo></foo>")),
"Autoclose not done");
assertFalse(document.get().contains("</bar>"), "Stale response was applied");
}
}
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -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
Expand All @@ -56,12 +57,13 @@
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;
Expand All @@ -84,49 +86,42 @@
break;
}

final EditorSnapshot snapshot = EditorSnapshot.of(currentViewer, document, offset);
Comment thread
vogella marked this conversation as resolved.
Resolved
final Display display = currentViewer.getTextWidget().getDisplay();
Comment thread
vogella marked this conversation as resolved.
Resolved
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</foo>
// $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;
});
}));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -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;

/**
Expand All @@ -50,33 +51,33 @@
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);
Comment thread
vogella marked this conversation as resolved.
Resolved
final Display display = currentViewer.getTextWidget().getDisplay();
Comment thread
vogella marked this conversation as resolved.
Resolved
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));
Expand All @@ -86,44 +87,33 @@
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</foo>
// $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;
});
}));
}
Expand All @@ -139,7 +129,6 @@

@Override
public void documentChanged(DocumentEvent e) {
System.out.println(e.toString());
autoInsert(e);
}

Expand Down
Loading