Files
openidea/src/features/services/tree.ts
T
Arnaud Ritti 39f6162c12 feat(plugins): services views in the Services tool window (#1760)
A plugin view declared with `location = "services"` (plugin API 1.3, `engines.openidea ^1.3`) shows its tree in the Services tool window, next to Docker and the run configurations. The nodes, the toolbar (acting on one selected node) and their events come from `view-provider`; `viewItem.context` menus give the nodes a right-click menu. The pane beside the tree comes from a new optional export, `service-provider.detail(view, key)` (worlds `plugin-services` / `services`), which returns typed tabs: a message, read-only text or a process log. The IDE switches tabs without calling the plugin.

For a log, the UI names only the node and tab (`plugin_service_log`); the host asks the plugin for the pane again and runs its program with the plugin's `process:` grant in the project root, with the arguments of the toggles that are on, streaming its output into the log pane Docker uses until it exits (the exit code is shown), the pane closes or the Services window is hidden. A cancelled request starts nothing, lines are cut at 16 KiB, and revoking the grant or cancelling kills the program and what it started. The log pane moves to `src/ui/LogPane` and drops output that arrives after its source was stopped. The Rust SDK gains `openidea_plugin::services` (`Services`, `Tab`, `Pane`, `ProcessLog`). All components are rebuilt for the WIT change.

Closes #1736

Co-authored-by: arnaud-ritti <77437157+arnaud-ritti@users.noreply.github.com>
2026-11-07 09:39:18 +08:00

99 lines
3.4 KiB
TypeScript

/**
* The Services tree host: the root nodes service providers contribute, the
* selection and expansion, and which provider owns a node.
*/
import { create } from "zustand";
import type { MenuItem } from "@/ui";
import { serviceProviders, type ServiceProviderContribution, type ServiceTreeItem } from "@/workbench/contributions";
/** The service view (Services tool window tab) hosting the tree. */
export const SERVICES_TREE_VIEW = "services";
interface TreeState {
/** Root nodes per provider id (reported by each provider's `useRoots`). */
roots: Record<string, ServiceTreeItem[]>;
selectedKey: string | null;
selectedKeys: Set<string>;
expanded: Set<string>;
}
export const useServicesTree = create<TreeState>(() => ({
roots: {},
selectedKey: null,
selectedKeys: new Set(),
expanded: new Set(["docker:default", "docker:default:group:containers"]),
}));
/** Keys of nodes asking to start expanded that were shown once (collapsing them then sticks). */
const seenExpanded = new Set<string>();
export function setProviderRoots(provider: string, items: ServiceTreeItem[]) {
useServicesTree.setState((s) => {
if (s.roots[provider] === items) return s;
const fresh: string[] = [];
const visit = (i: ServiceTreeItem) => {
if (i.expanded && !seenExpanded.has(i.key)) {
seenExpanded.add(i.key);
fresh.push(i.key);
}
i.children?.forEach(visit);
};
items.forEach(visit);
const roots = { ...s.roots, [provider]: items };
return fresh.length ? { roots, expanded: new Set([...s.expanded, ...fresh]) } : { roots };
});
}
export function clearProviderRoots(provider: string) {
useServicesTree.setState((s) => {
if (!(provider in s.roots)) return s;
const roots = { ...s.roots };
delete roots[provider];
return { roots };
});
}
export interface Located {
item: ServiceTreeItem;
provider: string;
parent: ServiceTreeItem | null;
}
/** Every node by key, with the provider owning it. */
export function indexTree(roots: Record<string, readonly ServiceTreeItem[]>): Map<string, Located> {
const out = new Map<string, Located>();
const visit = (provider: string, item: ServiceTreeItem, parent: ServiceTreeItem | null) => {
out.set(item.key, { item, provider, parent });
item.children?.forEach((c) => visit(provider, c, item));
};
for (const [provider, items] of Object.entries(roots)) items.forEach((i) => visit(provider, i, null));
return out;
}
/** Roots of all providers, in provider order. */
export function orderedRoots(
roots: Record<string, readonly ServiceTreeItem[]>,
providers: readonly ServiceProviderContribution[],
): ServiceTreeItem[] {
return providers.flatMap((p) => roots[p.id] ?? []);
}
/** Keys of every expandable node (Expand All). */
export function expandableKeys(items: readonly ServiceTreeItem[]): string[] {
const out: string[] = [];
const visit = (i: ServiceTreeItem) => {
if (i.children?.length || i.expandable) {
out.push(i.key);
i.children?.forEach(visit);
}
};
items.forEach(visit);
return out;
}
/** The "Add Service" menu: every provider's entries, separated per provider. */
export function addServiceMenu(providers = serviceProviders()): MenuItem[] {
const groups = providers.map((p) => p.addMenu?.() ?? []).filter((g) => g.length > 0);
return groups.flatMap((g, i) => (i === 0 ? g : [{ kind: "separator" } as MenuItem, ...g]));
}