Connect minimal hosts to snapshots, target workers and shared Kit editing

This commit is contained in:
2026-11-12 11:07:45 +01:00
parent 28c1e47c95
commit 7db7218a6e
52 changed files with 4236 additions and 29 deletions
@@ -23,6 +23,14 @@ import java.util.concurrent.atomic.AtomicLong;
* store.
*/
public final class IntellijDocuments implements HostServices.Documents, AutoCloseable {
private final java.util.concurrent.CopyOnWriteArrayList<java.util.function.Consumer<URI>>
changes = new java.util.concurrent.CopyOnWriteArrayList<>();
public AutoCloseable onChange(java.util.function.Consumer<URI> listener) {
changes.add(listener);
return () -> changes.remove(listener);
}
private final Project project;
private final com.intellij.openapi.Disposable lifetime;
private final Map<Path, State> states = new HashMap<>();
@@ -40,6 +48,17 @@ public final class IntellijDocuments implements HostServices.Documents, AutoClos
this.project = Objects.requireNonNull(project);
lifetime = this::disposeResources;
Disposer.register(project, lifetime);
EditorFactory.getInstance()
.getEventMulticaster()
.addDocumentListener(
new DocumentListener() {
public void documentChanged(DocumentEvent event) {
var file = FileDocumentManager.getInstance().getFile(event.getDocument());
if (file != null && file.isInLocalFileSystem())
changes.forEach(listener -> listener.accept(canonicalUri(file)));
}
},
lifetime);
}
private <T> CompletionStage<T> edt(Callable<T> action) {
@@ -57,18 +76,19 @@ public final class IntellijDocuments implements HostServices.Documents, AutoClos
}
});
if (ApplicationManager.getApplication().isDispatchThread()) task.run();
else ApplicationManager.getApplication().invokeLater(task, ModalityState.any());
else ApplicationManager.getApplication().invokeLater(task, ModalityState.nonModal());
return future;
}
public CompletionStage<DocumentSession> open(URI uri) {
return edt(
() -> {
Path path = Path.of(uri).toRealPath();
Path supplied = Path.of(uri).toAbsolutePath().normalize();
Path path = supplied.toRealPath();
State state = states.get(path);
if (state == null) {
state = new State();
state.file = LocalFileSystem.getInstance().refreshAndFindFileByNioFile(path);
state.file = LocalFileSystem.getInstance().refreshAndFindFileByNioFile(supplied);
if (state.file == null) throw new IllegalArgumentException("Missing file: " + path);
state.document = FileDocumentManager.getInstance().getDocument(state.file);
if (state.document == null) throw new IllegalArgumentException("Not a text document");
@@ -90,6 +110,17 @@ public final class IntellijDocuments implements HostServices.Documents, AutoClos
});
}
public CompletionStage<List<DocumentSnapshot>> snapshots() {
return edt(
() -> {
unsaved().toCompletableFuture().join();
for (var file : FileEditorManager.getInstance(project).getOpenFiles())
if (file.isInLocalFileSystem() && "fxml".equalsIgnoreCase(file.getExtension()))
open(Path.of(file.getPath()).toUri()).toCompletableFuture().join().close();
return states.values().stream().map(this::snapshot).toList();
});
}
public CompletionStage<List<DocumentSnapshot>> unsaved() {
return edt(
() -> {
@@ -110,9 +141,17 @@ public final class IntellijDocuments implements HostServices.Documents, AutoClos
});
}
private static URI canonicalUri(VirtualFile file) {
try {
return Path.of(file.getPath()).toRealPath().toUri();
} catch (java.io.IOException e) {
return Path.of(file.getPath()).toAbsolutePath().normalize().toUri();
}
}
private DocumentSnapshot snapshot(State s) {
return new DocumentSnapshot(
Path.of(s.file.getPath()).toUri(),
canonicalUri(s.file),
s.revision.get(),
s.document.getText(),
s.document.isWritable() && s.file.isWritable(),