Keep form edits anchored across native autosaves
This commit is contained in:
@@ -403,6 +403,13 @@ final class BindingDocument {
|
||||
t.insert(),
|
||||
t.openEnd(),
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<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) {
|
||||
var host = owner(slot);
|
||||
if (!TYPES.containsKey(type)) throw new IllegalArgumentException("Choose a palette control.");
|
||||
|
||||
@@ -122,6 +122,21 @@ class FormDocumentTest {
|
||||
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
|
||||
void implicitToolbarChildrenStayInOneList() throws Exception {
|
||||
source =
|
||||
|
||||
Reference in New Issue
Block a user