Preserve FXML source ranges and extend shared Component projection
This commit is contained in:
+21
-1
@@ -149,6 +149,21 @@ public final class IntellijDocuments implements HostServices.Documents, AutoClos
|
||||
}
|
||||
}
|
||||
|
||||
private void refreshClean(State s) throws java.io.IOException {
|
||||
var manager = FileDocumentManager.getInstance();
|
||||
if (manager.isDocumentUnsaved(s.document)) return;
|
||||
String disk = Files.readString(Path.of(s.file.getPath()));
|
||||
if (disk.equals(s.document.getText())) {
|
||||
s.disk = disk; // Native autosave already persisted this revision; do not reload it.
|
||||
return;
|
||||
}
|
||||
if (!disk.equals(s.disk)) {
|
||||
s.file.refresh(false, false);
|
||||
manager.reloadFromDisk(s.document);
|
||||
s.disk = disk;
|
||||
}
|
||||
}
|
||||
|
||||
private DocumentSnapshot snapshot(State s) {
|
||||
return new DocumentSnapshot(
|
||||
canonicalUri(s.file),
|
||||
@@ -176,12 +191,17 @@ public final class IntellijDocuments implements HostServices.Documents, AutoClos
|
||||
}
|
||||
|
||||
public CompletionStage<DocumentSnapshot> snapshot() {
|
||||
return run(() -> IntellijDocuments.this.snapshot(s));
|
||||
return run(
|
||||
() -> {
|
||||
refreshClean(s);
|
||||
return IntellijDocuments.this.snapshot(s);
|
||||
});
|
||||
}
|
||||
|
||||
public CompletionStage<DocumentSnapshot> apply(EditBatch edits) {
|
||||
return run(
|
||||
() -> {
|
||||
refreshClean(s);
|
||||
edits.applyTo(IntellijDocuments.this.snapshot(s));
|
||||
var ranges = new ArrayList<>(edits.replacements());
|
||||
ranges.sort(Comparator.comparingInt(EditBatch.Replacement::start));
|
||||
|
||||
+50
@@ -109,4 +109,54 @@ public final class IntellijDocumentsTest extends BasePlatformTestCase {
|
||||
Files.deleteIfExists(directory);
|
||||
}
|
||||
}
|
||||
|
||||
public void testCleanExternalReloadAndRangeCaretPreservation() throws Exception {
|
||||
Path file = Files.createTempFile("designer-reload-", ".fxml");
|
||||
Files.writeString(file, "prefix value suffix");
|
||||
com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess.allowRootAccess(
|
||||
getTestRootDisposable(), file.toRealPath().toString());
|
||||
try (var docs = new IntellijDocuments(getProject());
|
||||
var handle = docs.open(file.toUri()).toCompletableFuture().get()) {
|
||||
var initial = handle.snapshot().toCompletableFuture().get();
|
||||
Files.writeString(file, "prefix external suffix");
|
||||
var current = handle.snapshot().toCompletableFuture().get();
|
||||
assertEquals("prefix external suffix", current.text());
|
||||
assertFalse(current.dirty());
|
||||
assertTrue(initial.revision() != current.revision());
|
||||
var vf =
|
||||
com.intellij.openapi.vfs.LocalFileSystem.getInstance().refreshAndFindFileByNioFile(file);
|
||||
var document =
|
||||
com.intellij.openapi.fileEditor.FileDocumentManager.getInstance().getDocument(vf);
|
||||
var editor =
|
||||
com.intellij.openapi.editor.EditorFactory.getInstance()
|
||||
.createEditor(document, getProject());
|
||||
try {
|
||||
editor.getCaretModel().moveToOffset(current.text().length());
|
||||
editor
|
||||
.getSelectionModel()
|
||||
.setSelection(current.text().indexOf("suffix"), current.text().length());
|
||||
var updated =
|
||||
handle
|
||||
.apply(
|
||||
new EditBatch(
|
||||
current.revision(),
|
||||
"Range edit",
|
||||
List.of(new EditBatch.Replacement(7, 15, "X"))))
|
||||
.toCompletableFuture()
|
||||
.get();
|
||||
assertEquals("prefix X suffix", updated.text());
|
||||
assertEquals("suffix", editor.getSelectionModel().getSelectedText());
|
||||
assertEquals(updated.text().length(), editor.getCaretModel().getOffset());
|
||||
com.intellij.openapi.fileEditor.FileDocumentManager.getInstance().saveDocument(document);
|
||||
var saved = handle.snapshot().toCompletableFuture().get();
|
||||
assertEquals(updated.revision(), saved.revision());
|
||||
assertFalse(saved.dirty());
|
||||
handle.save(updated.revision()).toCompletableFuture().get();
|
||||
} finally {
|
||||
com.intellij.openapi.editor.EditorFactory.getInstance().releaseEditor(editor);
|
||||
}
|
||||
} finally {
|
||||
Files.deleteIfExists(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user