From 43267824d78fe4c6454bcbf71cb2d7e535cde227 Mon Sep 17 00:00:00 2001 From: Konrad Neitzel Date: Tue, 24 Nov 2026 15:24:07 +0100 Subject: [PATCH] Keep form edits anchored across native autosaves --- .../fxdesigner/kit/BindingDocument.java | 7 +++ .../neitzel/fxdesigner/kit/DesignerPane.java | 43 +++++++++++++------ .../neitzel/fxdesigner/kit/FormDocument.java | 27 ++++++++++++ .../fxdesigner/kit/FormDocumentTest.java | 15 +++++++ .../idea/ConnectionWorkflowProbe.java | 38 ++++++++++++++++ 5 files changed, 118 insertions(+), 12 deletions(-) diff --git a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/BindingDocument.java b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/BindingDocument.java index fb44a11..b499c93 100644 --- a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/BindingDocument.java +++ b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/BindingDocument.java @@ -403,6 +403,13 @@ final class BindingDocument { t.insert(), t.openEnd(), attributes + ">" + eol + child + eol + ""); + int line = text.lastIndexOf('\n', t.closeStart() - 1) + 1; + String indent = text.substring(line, t.closeStart()); + if (indent.isBlank()) { + // Keep the closing tag on its indented line. Blank lines with spaces can trigger + // IntelliJ's strip-trailing-whitespace edit on the next automatic save. + return new Replacement(line, line, indent + " " + child + eol); + } return new Replacement(t.closeStart(), t.closeStart(), eol + child + eol); } } diff --git a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/DesignerPane.java b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/DesignerPane.java index 806b82d..519e609 100644 --- a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/DesignerPane.java +++ b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/DesignerPane.java @@ -446,18 +446,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { bindings.setExpanded(false); formEditor = new FormEditor( - edit -> - flush() - .whenComplete( - (ok, error) -> - fx( - () -> { - if (error != null) report(error); - else if (draft.writable() && draft.text().equals(edit.source())) - submit(edit.changes(), "Edit form"); - else - status.setText("Document changed; select the control again."); - })), + this::editForm, index -> { if (loading || projection == null || kit.editor().getFxomDocument() == null) return; String id = projection.canvasId(index); @@ -763,6 +752,36 @@ public final class DesignerPane extends BorderPane implements AutoCloseable { : null; } + private void editForm(FormDocument.Edit edit) { + flush() + .whenComplete( + (ok, error) -> + fx( + () -> { + if (error != null) { + report(error); + return; + } + if (pending) { + status.setText("Finishing the previous edit; repeat the form action."); + return; + } + pending = true; + pendingBase = edit.source(); + pendingVisual = edit.changes(); + document + .snapshot() + .thenCompose( + latest -> + document.apply( + new EditBatch( + latest.revision(), + "Edit form", + FormDocument.forCurrentText(edit, latest.text())))) + .whenComplete(this::updated); + })); + } + private void replace(String text, String name) { if (draft == null || pending) return; submit(difference(draft.text(), text), name); diff --git a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/FormDocument.java b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/FormDocument.java index 65b219a..48be59c 100644 --- a/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/FormDocument.java +++ b/designer-kit-ui/src/main/java/de/neitzel/fxdesigner/kit/FormDocument.java @@ -197,6 +197,33 @@ final class FormDocument { return xml.append(owner, snippet, ""); } + /** A native autosave can strip trailing whitespace between selection and apply. */ + static List forCurrentText(Edit edit, String current) { + if (edit.source().equals(current)) return edit.changes(); + var normalized = new StringBuilder(); + int[] offsets = new int[edit.source().length() + 1]; + var stripped = java.util.regex.Pattern.compile("(?m)[ \t]+$").matcher(edit.source()); + int start = 0; + while (stripped.find()) { + for (int i = start; i < stripped.start(); i++) { + offsets[i] = normalized.length(); + normalized.append(edit.source().charAt(i)); + } + Arrays.fill(offsets, stripped.start(), stripped.end(), normalized.length()); + start = stripped.end(); + } + for (int i = start; i < edit.source().length(); i++) { + offsets[i] = normalized.length(); + normalized.append(edit.source().charAt(i)); + } + offsets[edit.source().length()] = normalized.length(); + if (!current.contentEquals(normalized)) + throw new ConcurrentModificationException("Form changed; select the control again."); + return edit.changes().stream() + .map(r -> new Replacement(offsets[r.start()], offsets[r.end()], r.text())) + .toList(); + } + Edit add(String type, Slot slot, int at) { var host = owner(slot); if (!TYPES.containsKey(type)) throw new IllegalArgumentException("Choose a palette control."); diff --git a/designer-kit-ui/src/test/java/de/neitzel/fxdesigner/kit/FormDocumentTest.java b/designer-kit-ui/src/test/java/de/neitzel/fxdesigner/kit/FormDocumentTest.java index 221f627..60070bc 100644 --- a/designer-kit-ui/src/test/java/de/neitzel/fxdesigner/kit/FormDocumentTest.java +++ b/designer-kit-ui/src/test/java/de/neitzel/fxdesigner/kit/FormDocumentTest.java @@ -122,6 +122,21 @@ class FormDocumentTest { assertThrows(IllegalArgumentException.class, () -> set("autoCompleteTextField", "width", "20")); } + @Test + void nativeSaveBetweenPaletteSelectionAndApply() throws Exception { + source = source.replace("", " \n"); + var edit = doc().add("Label", slot("", "children"), 0); + var saved = source.replace("> \n", ">\n"); + var changes = FormDocument.forCurrentText(edit, saved); + var result = + new EditBatch(2, "Insert Label", changes) + .applyTo(new DocumentSnapshot(URI.create("file:///test.fxml"), 2, saved, true, false)); + assertTrue(new FormDocument(result).items.stream().anyMatch(i -> i.id().equals("label"))); + assertThrows( + ConcurrentModificationException.class, + () -> FormDocument.forCurrentText(edit, saved.replace("GridPane", "VBox"))); + } + @Test void implicitToolbarChildrenStayInOneList() throws Exception { source = diff --git a/integration-tests/idea/ConnectionWorkflowProbe.java b/integration-tests/idea/ConnectionWorkflowProbe.java index 1f361ee..c2fd4fa 100644 --- a/integration-tests/idea/ConnectionWorkflowProbe.java +++ b/integration-tests/idea/ConnectionWorkflowProbe.java @@ -127,6 +127,25 @@ public final class ConnectionWorkflowProbe implements StartupActivity.DumbAware choose("form-palette", type); choose("form-destination", destination); String before = source(); + fx( + () -> { + var b = + (Button) + pane.lookupAll(".button").stream() + .filter(n -> n instanceof Button bt && bt.getText().equals("Insert control")) + .findFirst() + .orElseThrow(); + if (b.isDisabled()) + throw new AssertionError("Insert control disabled for " + destination); + step( + "Selected insert " + + ((ComboBox) pane.lookup("#form-palette")).getValue() + + " / " + + ((ComboBox) pane.lookup("#form-destination")).getValue() + + " at " + + ((Spinner) pane.lookup("#form-position")).getValue()); + return null; + }); click(pane, "Insert control"); edited(before, "fx:id=\"" + id + "\""); step("Inserted " + type + " into " + destination); @@ -208,6 +227,25 @@ public final class ConnectionWorkflowProbe implements StartupActivity.DumbAware file = edt(() -> LocalFileSystem.getInstance().refreshAndFindFileByNioFile(path)); pane = openDesigner(project, file); step("Opened registered Neitzel Designer"); + fx( + () -> { + for (var n : pane.lookupAll(".label")) + if (n instanceof Label label + && (label.getText() != null && label.getText().startsWith("Context ") + || "form-message".equals(label.getId()))) { + label + .textProperty() + .addListener( + (o, a, b) -> { + try { + step("Editor: " + b); + } catch (Exception e) { + throw new IllegalStateException(e); + } + }); + } + return null; + }); controls(true); String grid = "GridPane [0] / children"; add("StatusBar", grid, "statusBar");