Add shared source-preserving Repeat binding inspector

This commit is contained in:
2026-11-13 14:06:16 +01:00
parent 510ce35b60
commit 0300062fb6
10 changed files with 481 additions and 54 deletions
@@ -237,4 +237,108 @@ class BindingDocumentTest {
.entries
.isEmpty());
}
private BindingDocument repeats(String text) throws Exception {
return new BindingDocument(text, BindingDocument.Kind.REPEAT);
}
@Test
void repeatCreatesContainersAndEscapesFields() throws Exception {
for (String root :
List.of(
"<VBox/>",
"<VBox xmlns:f='http://javafx.com/fxml/1'><f:define/></VBox>",
"<VBox xmlns:f='http://javafx.com/fxml/1'><f:define><"
+ P
+ "BindingList><elements/></"
+ P
+ "BindingList></f:define></VBox>")) {
String text = "<?neitzel-model app.Person?>\r\n" + root;
var value = BindingDocument.Values.repeat("pane", "app.Child", "a'&<");
String result = apply(text, repeats(text).write(null, value, false));
assertEquals(value, repeats(result).entries.getFirst().values());
assertTrue(result.startsWith("<?neitzel-model app.Person?>\r\n"));
assertFalse(result.contains(" control="));
assertTrue(repeats(result).errors().stream().anyMatch(e -> e.contains("Repeat collection")));
}
}
private String repeatSource() {
return existing()
.replace(
"<app.Unknown",
"<Repeat pane = 'rows' component='app.Child' collection = 'visits' custom='keep'><!--"
+ " inner --></Repeat><app.Unknown")
.replace("</VBox>", "<VBox fx:id='rows'/><VBox fx:id='other'/></VBox>");
}
@Test
void repeatChangesAllFieldsWithoutRewritingOtherBindings() throws Exception {
String text = repeatSource();
var doc = repeats(text);
int index = doc.entries.getFirst().index();
var value = BindingDocument.Values.repeat("other", "app.OtherChild", "home.visits");
String changed = apply(text, doc.write(index, value, false));
assertEquals(
text.replace("pane = 'rows'", "pane = 'other'")
.replace("component='app.Child'", "component='app.OtherChild'")
.replace("collection = 'visits'", "collection = 'home.visits'"),
changed);
assertTrue(repeats(changed).write(index, value, false).isEmpty());
String removed = apply(changed, repeats(changed).remove(index));
assertTrue(repeats(removed).entries.isEmpty());
assertEquals(1, new BindingDocument(removed).entries.size());
assertTrue(removed.contains("<!-- preserve -->"));
}
@Test
void repeatConflictsAreScopedToThePaneAndReplacedAtomically() throws Exception {
String text =
repeatSource()
.replace(
"<app.Unknown",
"<Repeat pane='rows' component='app.Child' collection='visits'/><app.Unknown");
var doc = repeats(text);
var value = BindingDocument.Values.repeat("rows", "app.NewChild", "visits");
assertTrue(doc.errors().stream().anyMatch(e -> e.contains("Duplicate Repeat pane")));
assertThrows(BindingDocument.Conflict.class, () -> doc.write(null, value, false));
String result = apply(text, doc.write(null, value, true));
assertEquals(1, repeats(result).entries.size());
assertEquals(1, new BindingDocument(result).entries.size());
assertTrue(repeats(result).errors().isEmpty());
// A DirectBinding to a property of the same pane remains independent.
var direct = new BindingDocument.Values("rows", "visible", "name", "view", "");
String withDirect = apply(result, new BindingDocument(result).write(null, direct, false));
assertEquals(1, repeats(withDirect).entries.size());
}
@Test
void repeatNestedSyntaxAndImportIdentityArePreserved() throws Exception {
String text =
repeatSource()
.replace(
"collection = 'visits' custom='keep'><!-- inner -->",
"custom='keep'><collection>visits</collection>");
var doc = repeats(text);
var entry = doc.entries.getFirst();
assertFalse(entry.editable());
assertThrows(
IllegalArgumentException.class,
() ->
doc.write(
entry.index(),
BindingDocument.Values.repeat("rows", "app.Child", "visits"),
false));
assertTrue(repeats(apply(text, doc.remove(entry.index()))).entries.isEmpty());
assertTrue(
repeats(text.replace("<?import de.neitzel.fx.view.*?>", "<?import app.Repeat?>"))
.entries
.isEmpty());
assertTrue(
repeats(repeatSource().replace("pane = 'rows'", "pane = 'missing'")).errors().stream()
.anyMatch(e -> e.contains("Missing or ambiguous Repeat pane")));
assertTrue(
repeats(repeatSource().replace("component='app.Child'", "component='' ")).errors().stream()
.anyMatch(e -> e.contains("Repeat Component")));
}
}
@@ -644,6 +644,164 @@ public final class ProjectionHostChecks {
await(() -> fx(() -> source(pane).getText().equals(full)));
}
private static void repeatField(DesignerPane pane, String name, String value) throws Exception {
fx(
() -> {
((TextField) pane.lookup("#repeat-" + name)).setText(value);
return null;
});
}
private static void selectRepeat(DesignerPane pane, String name) throws Exception {
fx(
() -> {
var list = (ListView<Object>) pane.lookup("#repeat-bindings");
list.getSelectionModel()
.select(
list.getItems().stream()
.filter(e -> e.toString().startsWith(name + " ←"))
.findFirst()
.orElseThrow());
return null;
});
}
private static void repeatHistory(
DesignerPane pane, DocumentSession document, String before, String after) throws Exception {
click(pane, "Undo");
await(() -> documentText(document).equals(before));
click(pane, "Redo");
await(() -> documentText(document).equals(after));
await(() -> fx(() -> source(pane).getText().equals(after)));
}
private static void repeatBindingEdits(DesignerPane pane, DocumentSession document, String full)
throws Exception {
String base =
full.replace(
"<VBox fx:id=\"visits\"/>",
"<VBox fx:id=\"visits\"/><VBox fx:id=\"otherVisits\"/><!-- Repeat edit sentinel -->");
check(!base.equals(full), "Repeat fixture pane missing");
set(document, base);
await(() -> fx(() -> source(pane).getText().equals(base)));
bindingTab(pane, "Repeat bindings");
selectRepeat(pane, "visits");
click(pane, "Remove Repeat binding");
String removed =
base.replace(
"<Repeat pane=\"visits\" component=\"fixture.components.AddressForm\""
+ " collection=\"visits\"/>",
"");
await(() -> documentText(document).equals(removed));
repeatHistory(pane, document, base, removed);
click(pane, "New Repeat binding");
repeatField(pane, "pane", "visits");
repeatField(pane, "component", "fixture.components.AddressForm");
repeatField(pane, "collection", "visits");
check(documentText(document).equals(removed), "Repeat form changed source before Apply");
click(pane, "Apply Repeat binding");
await(() -> !documentText(document).equals(removed));
String created = documentText(document);
check(
created.contains(
"de.neitzel.fx.view.Repeat pane=\"visits\" component=\"fixture.components.AddressForm\""
+ " collection=\"visits\""),
"Repeat creation failed");
repeatHistory(pane, document, removed, created);
selectRepeat(pane, "visits");
repeatField(pane, "pane", "otherVisits");
repeatField(pane, "component", "fixture.components.VisitRow");
click(pane, "Apply Repeat binding");
String changed =
created.replace(
"Repeat pane=\"visits\" component=\"fixture.components.AddressForm\"",
"Repeat pane=\"otherVisits\" component=\"fixture.components.VisitRow\"");
await(() -> documentText(document).equals(changed));
repeatHistory(pane, document, created, changed);
document
.save(document.snapshot().toCompletableFuture().get().revision())
.toCompletableFuture()
.get();
check(
java.nio.file.Files.readString(
java.nio.file.Path.of(document.snapshot().toCompletableFuture().get().document()))
.equals(changed),
"Repeat save changed source");
click(pane, "Validate Repeat bindings");
await(
() ->
fx(
() ->
((Label) pane.lookup("#repeat-status"))
.getText()
.contains("Collection and Component compatibility")));
click(pane, "Render drafts");
await(
() ->
fx(
() ->
object(pane, "home").getSceneGraphObject() instanceof SnapshotRegion r
&& r.image() != null
&& r.statusText().equals("Target runtime")));
click(pane, "New Repeat binding");
repeatField(pane, "pane", "otherVisits");
repeatField(pane, "component", "fixture.components.AddressForm");
repeatField(pane, "collection", "visits");
click(pane, "Apply Repeat binding");
await(
() ->
fx(
() ->
((Label) pane.lookup("#repeat-status"))
.getText()
.contains("Replace conflicting bindings")));
check(documentText(document).equals(changed), "Repeat conflict silently replaced");
fx(
() -> {
((CheckBox) pane.lookup("#repeat-replace")).setSelected(true);
return null;
});
click(pane, "Apply Repeat binding");
await(() -> !documentText(document).equals(changed));
String replaced = documentText(document);
check(
!replaced.contains("component=\"fixture.components.VisitRow\""),
"Repeat replacement retained old declaration");
repeatHistory(pane, document, changed, replaced);
selectRepeat(pane, "otherVisits");
repeatField(pane, "collection", "invalid path");
click(pane, "Apply Repeat binding");
String invalid = replaced.replace("collection=\"visits\"/>", "collection=\"invalid path\"/>");
await(() -> documentText(document).equals(invalid));
document
.save(document.snapshot().toCompletableFuture().get().revision())
.toCompletableFuture()
.get();
check(
java.nio.file.Files.readString(
java.nio.file.Path.of(document.snapshot().toCompletableFuture().get().document()))
.equals(invalid),
"Invalid Repeat was not saved");
click(pane, "Render drafts");
await(
() ->
fx(
() ->
pane.lookupAll(".label").stream()
.anyMatch(
n ->
n instanceof Label l
&& l.getText() != null
&& l.getText().startsWith("Preview blocked:")
&& l.getText().contains("Repeat collection"))));
repeatHistory(pane, document, replaced, invalid);
set(document, full);
await(() -> fx(() -> source(pane).getText().equals(full)));
System.out.println(
"PASS Repeat inspector: create, change, remove, conflict, exact undo/redo, save, target"
+ " load and invalid draft");
}
private static void viewModelBindings(
DesignerPane pane, DocumentSession document, String original) throws Exception {
bindingTab(pane, "Discover targets");
@@ -1022,6 +1180,7 @@ public final class ProjectionHostChecks {
converterEdits(pane, document, full);
directBindingEdits(pane, document, full);
computedBindingEdits(pane, document, full);
repeatBindingEdits(pane, document, full);
document
.save(document.snapshot().toCompletableFuture().get().revision())
.toCompletableFuture()