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.5
Permissions in 0.2.5
Files
follow.ts4.1 KB
// Open tabs follow the files on disk. Each workspace with a file or a live
// diff open is watched: a file changed by anything (an agent, a terminal,
// git, another editor) reloads in its tab, and a diff that follows the
// repository reads its changes again, also when the index changes.
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Scope from "effect/Scope";
import { Fs, Host } from "divergence/effect";
import { liveDiff } from "./tabs.ts";
import type { Content, Tab, TabsState } from "./tabs.ts";
export const makeFollow = Effect.fn("Tabs.makeFollow")(function* (
state: TabsState,
redraw: () => void,
refreshFile: (tab: Tab) => Effect.Effect<void>,
) {
const host = yield* Host;
const fs = yield* Fs;
const scope = yield* Effect.scope;
// A diff tab made again from its key, or one that follows the repository,
// reads the file's changes, whole, as Source Control opens it. A read that
// fails says so in the tab, and only the latest read of a tab counts.
const diffReads = new Map<string, number>();
const readDiff = Effect.fn("Tabs.readDiff")(function* (tab: Tab) {
const ticket = (diffReads.get(tab.id) ?? 0) + 1;
diffReads.set(tab.id, ticket);
const content = yield* host
.call("host/git.diff", { workspaceId: tab.workspaceId, path: tab.path, staged: !!tab.staged, wholeFile: true })
.pipe(
Effect.map((result): Content => ({ diff: result.diff })),
Effect.catchTag(["HostCallFailed", "NeedsReview", "PermissionNotGranted"], (error) =>
Effect.succeed<Content>({ error: `Could not read the changes: ${error.message}` }),
),
);
if (!state.tabs.includes(tab) || diffReads.get(tab.id) !== ticket) return;
const was = state.contents[tab.id];
if (was?.diff === content.diff && was?.error === content.error) return;
state.contents[tab.id] = content;
redraw();
});
const watches = new Map<string, Scope.Closeable>();
const followed = (workspaceId: string) =>
state.tabs.some((tab) => tab.workspaceId === workspaceId && (tab.kind === "file" || liveDiff(tab)));
const changed = (workspaceId: string, paths: readonly string[]) =>
Effect.forEach(
state.tabs.filter((tab) => tab.workspaceId === workspaceId),
(tab) =>
tab.kind === "file" && paths.includes(tab.path)
? refreshFile(tab)
: liveDiff(tab) && (paths.includes(tab.path) || paths.includes(".git/index"))
? readDiff(tab)
: Effect.void,
{ discard: true },
);
const unwatched = (error: { message: string }) =>
Effect.logWarning(`tabs: the workspace's files are not watched: ${error.message}`);
const watch = Effect.fn("Tabs.watch")(function* (workspaceId: string) {
if (watches.has(workspaceId) || !followed(workspaceId)) return;
const own = yield* Scope.fork(scope);
watches.set(workspaceId, own);
const root = yield* host.call("host/workspaces.list", {}).pipe(
Effect.map(({ workspaces }) => workspaces.find((workspace) => workspace.id === workspaceId)?.path ?? null),
Effect.catchTag(["HostCallFailed", "NeedsReview", "PermissionNotGranted"], (error) => Effect.as(unwatched(error), null)),
);
if (root === null) return;
const onChange = (paths: readonly string[]) => {
const relative = paths.flatMap((path) => (path.startsWith(`${root}/`) ? [path.slice(root.length + 1)] : []));
if (relative.length) Effect.runFork(Effect.forkIn(changed(workspaceId, relative), own, { startImmediately: true }));
};
yield* fs.watch(".", onChange, { workspaceId }).pipe(
Effect.provideService(Scope.Scope, own),
Effect.catchTag(["HostCallFailed", "NeedsReview", "PermissionNotGranted"], unwatched),
);
});
// A workspace's watch ends once nothing open there is followed.
const unwatch = Effect.fn("Tabs.unwatch")(function* (workspaceId: string) {
const own = watches.get(workspaceId);
if (!own || followed(workspaceId)) return;
watches.delete(workspaceId);
yield* Scope.close(own, Exit.void);
});
const follow = (tab: Tab) => Effect.forkIn(watch(tab.workspaceId), scope, { startImmediately: true });
return { readDiff, follow, unwatch };
});
No comments yet.