Add source highlighting and revision-checked diagnostic fixes in both hosts

This commit is contained in:
2026-11-13 18:19:39 +01:00
parent 8a4f456e38
commit 9bc4569407
18 changed files with 1033 additions and 14 deletions
@@ -0,0 +1,112 @@
package de.neitzel.fxdesigner.kit;
import static org.junit.jupiter.api.Assertions.*;
import de.neitzel.fxdesigner.contracts.*;
import de.neitzel.fxdesigner.host.HostServices.*;
import de.neitzel.fxdesigner.protocol.BindingMetadata.Problem;
import java.net.URI;
import java.util.*;
import org.junit.jupiter.api.Test;
class DiagnosticActionsTest {
private static final String XML =
"<?neitzel-model app.Person?>\r\n"
+ "<?import de.neitzel.fx.view.*?><VBox"
+ " xmlns:fx='http://javafx.com/fxml/1'><fx:define><BindingList><!-- before"
+ " --><DirectBinding control='name' property='text' path='name' direction = 'bad'"
+ " extra='keep'/><!-- after --></BindingList></fx:define><Label fx:id='name'"
+ " text='bad'/></VBox>";
private DocumentSnapshot snapshot(String text, boolean writable) {
return new DocumentSnapshot(URI.create("file:/View.fxml"), 7, text, writable, true);
}
private Diagnostic direction(DocumentSnapshot d) {
return FxmlDiagnostics.local(d).stream()
.filter(p -> p.message().startsWith("Invalid or missing direction:"))
.findFirst()
.orElseThrow();
}
private String apply(DocumentSnapshot d, QuickFix fix) {
return new EditBatch(d.revision(), fix.label(), fix.edits()).applyTo(d);
}
@Test
void directionChoicesAreExplicitAndChangeOnlyTheAttributeValue() {
var d = snapshot(XML, true);
var problem = direction(d);
assertEquals(
"bad",
XML.substring(problem.range().orElseThrow().start(), problem.range().orElseThrow().end()));
assertEquals(3, problem.fixes().size());
for (var fix : problem.fixes()) {
String value = fix.label().substring(fix.label().lastIndexOf(' ') + 1);
assertEquals(XML.replace("direction = 'bad'", "direction = '" + value + "'"), apply(d, fix));
}
}
@Test
void missingAndEmptyDirectionKeepSourceAndHighlightAnActualRange() {
for (String text :
List.of(XML.replace(" direction = 'bad'", ""), XML.replace("'bad' extra", "'' extra"))) {
var d = snapshot(text, true);
var p = direction(d);
assertTrue(p.range().orElseThrow().end() > p.range().orElseThrow().start());
String fixed = apply(d, p.fixes().getFirst());
assertTrue(fixed.contains("<!-- before -->"));
assertTrue(fixed.contains("extra='keep'"));
assertFalse(
FxmlDiagnostics.local(snapshot(fixed, true)).stream()
.anyMatch(x -> x.message().startsWith("Invalid or missing direction:")));
}
}
@Test
void duplicateRemovalPreservesFirstDeclarationAndSurroundingComments() {
String declaration = "<ComputedBinding control='name' property='text' method='#name'/>";
String text =
XML.replace("'bad' extra", "'view' extra")
.replace("<!-- after -->", "<!-- after -->" + declaration + "<!-- end -->");
var d = snapshot(text, true);
var p =
FxmlDiagnostics.local(d).stream()
.filter(x -> x.message().startsWith("Duplicate binding target:"))
.findFirst()
.orElseThrow();
assertEquals(text.replace(declaration, ""), apply(d, p.fixes().getFirst()));
}
@Test
void targetFixesRequireKnownSimpleDeclarationsAndNeverRemoveControls() {
var p = new Problem("name", "text", "ERROR", "unknown path");
var d = snapshot(XML, true);
assertEquals(1, FxmlDiagnostics.target(d, List.of(p)).getFirst().fixes().size());
for (String text :
List.of(
XML.replace("extra='keep'", "fx:id='binding'"),
XML.replace(
"extra='keep'/>", "extra='keep'><converter>app.C</converter></DirectBinding>"),
XML.replace("DirectBinding", "app.UnknownBinding")))
assertTrue(
FxmlDiagnostics.target(snapshot(text, true), List.of(p)).getFirst().fixes().isEmpty());
assertTrue(
FxmlDiagnostics.target(d, List.of(new Problem("name", "text", "WARNING", "unverified")))
.getFirst()
.fixes()
.isEmpty());
}
@Test
void readOnlyAndStaleSourceCannotApplyFixes() {
assertTrue(direction(snapshot(XML, false)).fixes().isEmpty());
var d = snapshot(XML, true);
var fix = direction(d).fixes().getFirst();
var edit = new EditBatch(d.revision(), fix.label(), fix.edits());
assertThrows(
ConcurrentModificationException.class,
() -> edit.applyTo(new DocumentSnapshot(d.document(), 8, XML, true, true)));
assertThrows(IllegalStateException.class, () -> edit.applyTo(snapshot(XML, false)));
}
}
@@ -760,6 +760,137 @@ public final class ProjectionHostChecks {
+ " withdrawal, correction, exact undo/redo and malformed FXML recovery");
}
@SuppressWarnings("unchecked")
private static void diagnosticFixes(DesignerPane pane, DocumentSession document, String full)
throws Exception {
String bad = full.replaceFirst("direction=\"both\"", "direction = 'bad'");
set(document, bad);
await(() -> fx(() -> source(pane).getText().equals(bad)));
bindingTab(pane, "Problems");
var issue =
diagnostics(pane).stream()
.filter(d -> d.message().startsWith("Invalid or missing direction:"))
.findFirst()
.orElseThrow();
fx(
() -> {
((ListView<de.neitzel.fxdesigner.host.HostServices.Diagnostic>)
pane.lookup("#diagnostics-list"))
.getSelectionModel()
.select(issue);
return null;
});
click(pane, "Go to FXML");
await(() -> fx(() -> !pane.lookupAll(".diagnostic-highlight").isEmpty()));
fx(
() -> {
var area = source(pane);
int caret = area.getCaretPosition();
area.setWrapText(true);
area.setScrollTop(100);
area.setScrollLeft(20);
pane.applyCss();
pane.layout();
check(area.getCaretPosition() == caret, "Highlighting changed caret");
var combo =
(ComboBox<de.neitzel.fxdesigner.host.HostServices.QuickFix>)
pane.lookup("#diagnostic-fixes");
combo
.getSelectionModel()
.select(
combo.getItems().stream()
.filter(f -> f.label().equals("Set binding direction to both"))
.findFirst()
.orElseThrow());
return null;
});
fx(
() -> {
pane.applyCss();
pane.layout();
var area = source(pane);
var text =
area.lookupAll(".text").stream()
.filter(
n ->
n instanceof javafx.scene.text.Text t
&& t.getText().equals(area.getText()))
.map(n -> (javafx.scene.text.Text) n)
.findFirst()
.orElseThrow();
for (var mark : area.lookupAll(".diagnostic-highlight")) {
var d = (de.neitzel.fxdesigner.host.HostServices.Diagnostic) mark.getUserData();
var range = d.range().orElseThrow();
var shape =
new javafx.scene.shape.Path(text.underlineShape(range.start(), range.end()));
shape.setStrokeWidth(1.5);
var expected = text.localToScene(shape.getBoundsInLocal());
var actual = mark.localToScene(mark.getBoundsInLocal());
check(
Math.abs(expected.getMinX() - actual.getMinX()) < .5
&& Math.abs(expected.getMinY() - actual.getMinY()) < .5
&& Math.abs(expected.getMaxX() - actual.getMaxX()) < .5
&& Math.abs(expected.getMaxY() - actual.getMaxY()) < .5,
"Underline is not aligned with wrapped source glyphs: "
+ expected
+ " / "
+ actual);
}
var image = java.nio.file.Files.createTempFile("designer-source-highlights-", ".png");
javax.imageio.ImageIO.write(
javafx.embed.swing.SwingFXUtils.fromFXImage(pane.snapshot(null, null), null),
"png",
image.toFile());
System.out.println("Diagnostic source image: " + image);
return null;
});
click(pane, "Apply diagnostic fix");
String fixed = bad.replace("direction = 'bad'", "direction = 'both'");
await(() -> documentText(document).equals(fixed));
repeatHistory(pane, document, bad, fixed);
await(
() ->
diagnostics(pane).stream()
.noneMatch(d -> d.message().startsWith("Invalid or missing direction:")));
String duplicate =
"<de.neitzel.fx.view.DirectBinding control=\"rankInput\" property=\"text\" path=\"rank\""
+ " direction=\"view\"/>";
String duplicated =
full.replace("</BindingList>", "<!--keep-->" + duplicate + "</BindingList>");
set(document, duplicated);
await(() -> fx(() -> source(pane).getText().equals(duplicated)));
bindingTab(pane, "Problems");
var conflict =
diagnostics(pane).stream()
.filter(d -> d.message().startsWith("Duplicate binding target:"))
.findFirst()
.orElseThrow();
fx(
() -> {
((ListView<de.neitzel.fxdesigner.host.HostServices.Diagnostic>)
pane.lookup("#diagnostics-list"))
.getSelectionModel()
.select(conflict);
((ComboBox<?>) pane.lookup("#diagnostic-fixes")).getSelectionModel().selectFirst();
return null;
});
click(pane, "Apply diagnostic fix");
String removed = duplicated.replace(duplicate, "");
await(() -> documentText(document).equals(removed));
repeatHistory(pane, document, duplicated, removed);
set(document, full);
await(() -> fx(() -> source(pane).getText().equals(full)));
fx(
() -> {
source(pane).setWrapText(false);
return null;
});
click(pane, "Split");
System.out.println(
"PASS diagnostic fixes: source underlines, wrap/scroll without caret edits, explicit"
+ " direction and duplicate removal, lexical preservation and single-step Undo/Redo");
}
private static void structureEdits(DesignerPane pane, DocumentSession document, String full)
throws Exception {
String base =
@@ -1778,6 +1909,7 @@ public final class ProjectionHostChecks {
tableEdits(pane, document, full);
structureEdits(pane, document, full);
diagnosticsEdits(pane, document, full);
diagnosticFixes(pane, document, full);
document
.save(document.snapshot().toCompletableFuture().get().revision())
.toCompletableFuture()