Official
tabs
The tabbed detail pane beside the chat: files to edit and diffs (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/tabs@0.2.4
Permissions in 0.2.4
Files
main.ts4.5 KB
// Files, diffs, terminals and diagrams: this plugin's tab kinds, which show
// in the side split or a task's pane (specs/tasks-and-splits/05), opened
// through the divergence/tabs service and, for third-party plugins, the
// legacy shared openTab/closeTab requests. The file-editor capability is
// resolved before the kinds register; their subscriptions and service follow.
import * as Effect from "effect/Effect";
import * as Stream from "effect/Stream";
import { App, Events, Host, Layouts, Services, runPlugin } from "divergence/effect";
import type { EffectTabDefinition } from "divergence/effect";
import type { IconName } from "divergence";
import { Tabs, followApp } from "./controller.ts";
import { Tabs as TabsService } from "./contract.ts";
import { fileOfKey, isDirty, runFinished, tabFromKey, tabName, workspaceOfKey } from "./tabs.ts";
import type { Tab } from "./tabs.ts";
import { renderItem, tabIcon } from "./views.tsx";
const program = Effect.gen(function* () {
const tabs = yield* Tabs;
const host = yield* Host;
const layouts = yield* Layouts;
// A diff tab made again from its key (a restart, a row dragged from Source Control) reads
// the working tree's changes of that file, whole, as Source Control opens it.
const readDiff = (tab: Tab) =>
host.call("host/git.diff", { workspaceId: tab.workspaceId, path: tab.path, wholeFile: true }).pipe(
Effect.tap((result) =>
Effect.sync(() => {
tabs.state.contents[tab.id] = { diff: result.diff };
layouts.redraw();
}),
),
Effect.ignore({ log: "Warn", message: "Could not read the diff" }),
);
// A tab's key is its id; one the app brings back after a restart is
// made again from it, its file read again.
const tabOf = (key: string): Tab | undefined => {
const known = tabs.state.tabs.find((tab) => tab.id === key);
if (known) return known;
const made = tabFromKey(tabs.state, key);
if (made?.kind === "file" && tabs.nativeFiles) tabs.ensureFile(made);
else if (made?.kind === "file") Effect.runFork(tabs.readFile(made, made.id));
if (made?.kind === "diff") Effect.runFork(readDiff(made));
return made;
};
const kind = (name: Tab["kind"]): EffectTabDefinition<object> => ({
opens: "side",
title: (key: string) => {
const tab = tabOf(key);
return tab ? tabName(tab) : key;
},
icon: tabIcon(name) as IconName,
// A file's and a diff's tabs show the file tree's icon for the file.
file: fileOfKey,
// In the side split, a workspace's tabs show beside its chats only.
workspace: workspaceOfKey,
render: (ctx) => {
const tab = tabOf(ctx.key);
return tab ? renderItem(ctx, tabs, tab) : null;
},
// A file with unsaved changes asks before its tab closes.
beforeClose: (ctx) => {
const tab = tabOf(ctx.key);
return tab && isDirty(tabs.state.contents[tab.id]) ? `Discard the unsaved changes to ${tabName(tab)}?` : null;
},
});
yield* layouts.kinds({ file: kind("file"), diff: kind("diff"), terminal: kind("terminal"), diagram: kind("diagram") });
const app = yield* App;
const events = yield* Events;
yield* app.changes.pipe(
Stream.runForEach((next) => Effect.forkScoped(followApp(tabs, next), { startImmediately: true })),
Effect.forkScoped({ startImmediately: true }),
);
// A run may have changed the open files: native editors advance their
// reload revision, while an older kernel takes the compatibility read path.
// Only finished runs are asked for; an older runtime sends every agent event.
yield* events.stream("agent", { kinds: ["run_finished"] }).pipe(
Stream.runForEach((event) =>
runFinished(event.kind)
? Effect.forEach(
tabs.state.tabs.filter((tab) => tab.kind === "file"),
(tab) => tabs.refreshFile(tab),
{ discard: true },
)
: Effect.void,
),
Effect.forkScoped({ startImmediately: true }),
);
yield* followApp(tabs, yield* app.state);
// The service goes last: everything above registers synchronously, and a
// provider that arrives later only adds calls, never views.
const services = yield* Services;
const handle = yield* services.provide(TabsService, {
open: (params, _ctx) => tabs.open(params).pipe(Effect.map((tab) => ({ id: tab.id }))),
close: (params, _ctx) => tabs.closeByRef(params),
active: (_params, _ctx) => tabs.activeRef(),
});
tabs.emitActive((tab) => handle.emit("activeChanged", tab));
});
export default runPlugin(Tabs.layer, program);
No comments yet.