Keep form edits anchored across native autosaves
This commit is contained in:
@@ -403,6 +403,13 @@ final class BindingDocument {
|
|||||||
t.insert(),
|
t.insert(),
|
||||||
t.openEnd(),
|
t.openEnd(),
|
||||||
attributes + ">" + eol + child + eol + "</" + container.getTagName() + ">");
|
attributes + ">" + eol + child + eol + "</" + container.getTagName() + ">");
|
||||||
|
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);
|
return new Replacement(t.closeStart(), t.closeStart(), eol + child + eol);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -446,18 +446,7 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
|
|||||||
bindings.setExpanded(false);
|
bindings.setExpanded(false);
|
||||||
formEditor =
|
formEditor =
|
||||||
new FormEditor(
|
new FormEditor(
|
||||||
edit ->
|
this::editForm,
|
||||||
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.");
|
|
||||||
})),
|
|
||||||
index -> {
|
index -> {
|
||||||
if (loading || projection == null || kit.editor().getFxomDocument() == null) return;
|
if (loading || projection == null || kit.editor().getFxomDocument() == null) return;
|
||||||
String id = projection.canvasId(index);
|
String id = projection.canvasId(index);
|
||||||
@@ -763,6 +752,36 @@ public final class DesignerPane extends BorderPane implements AutoCloseable {
|
|||||||
: null;
|
: 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) {
|
private void replace(String text, String name) {
|
||||||
if (draft == null || pending) return;
|
if (draft == null || pending) return;
|
||||||
submit(difference(draft.text(), text), name);
|
submit(difference(draft.text(), text), name);
|
||||||
|
|||||||
@@ -197,6 +197,33 @@ final class FormDocument {
|
|||||||
return xml.append(owner, snippet, "");
|
return xml.append(owner, snippet, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** A native autosave can strip trailing whitespace between selection and apply. */
|
||||||
|
static List<Replacement> 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) {
|
Edit add(String type, Slot slot, int at) {
|
||||||
var host = owner(slot);
|
var host = owner(slot);
|
||||||
if (!TYPES.containsKey(type)) throw new IllegalArgumentException("Choose a palette control.");
|
if (!TYPES.containsKey(type)) throw new IllegalArgumentException("Choose a palette control.");
|
||||||
|
|||||||
@@ -122,6 +122,21 @@ class FormDocumentTest {
|
|||||||
assertThrows(IllegalArgumentException.class, () -> set("autoCompleteTextField", "width", "20"));
|
assertThrows(IllegalArgumentException.class, () -> set("autoCompleteTextField", "width", "20"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void nativeSaveBetweenPaletteSelectionAndApply() throws Exception {
|
||||||
|
source = source.replace("<GridPane/>", "<GridPane> \n</GridPane>");
|
||||||
|
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
|
@Test
|
||||||
void implicitToolbarChildrenStayInOneList() throws Exception {
|
void implicitToolbarChildrenStayInOneList() throws Exception {
|
||||||
source =
|
source =
|
||||||
|
|||||||
@@ -127,6 +127,25 @@ public final class ConnectionWorkflowProbe implements StartupActivity.DumbAware
|
|||||||
choose("form-palette", type);
|
choose("form-palette", type);
|
||||||
choose("form-destination", destination);
|
choose("form-destination", destination);
|
||||||
String before = source();
|
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");
|
click(pane, "Insert control");
|
||||||
edited(before, "fx:id=\"" + id + "\"");
|
edited(before, "fx:id=\"" + id + "\"");
|
||||||
step("Inserted " + type + " into " + destination);
|
step("Inserted " + type + " into " + destination);
|
||||||
@@ -208,6 +227,25 @@ public final class ConnectionWorkflowProbe implements StartupActivity.DumbAware
|
|||||||
file = edt(() -> LocalFileSystem.getInstance().refreshAndFindFileByNioFile(path));
|
file = edt(() -> LocalFileSystem.getInstance().refreshAndFindFileByNioFile(path));
|
||||||
pane = openDesigner(project, file);
|
pane = openDesigner(project, file);
|
||||||
step("Opened registered Neitzel Designer");
|
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);
|
controls(true);
|
||||||
String grid = "GridPane [0] / children";
|
String grid = "GridPane [0] / children";
|
||||||
add("StatusBar", grid, "statusBar");
|
add("StatusBar", grid, "statusBar");
|
||||||
|
|||||||
Reference in New Issue
Block a user