/** "hierarchy" tool window: call hierarchy (⌃⌥H) and type hierarchy (⌃H). */ import { useCallback, useMemo, useRef } from "react"; import { create } from "zustand"; import { basename, relativePath, useProject } from "@/platform/project"; import { maybeEditorService, useOccurrenceNavigator } from "@/workbench/contributions"; import { EmptyState, IconButton, Spinner, SymbolBadge, Tabs, showContextMenu, Tree, badgeKind, type TreeHandle, type TreeRowState, T, } from "@/ui"; import { openLocation } from "./ipc"; import { closeHierarchyTab, pinHierarchyTab, ROOT_ID, selectHierarchyTab, tabHierarchy, directionLabel, hierarchyTitle, hierarchyTarget, loadChildren, prefetchLevels, refreshHierarchy, noneFoundLabel, setDirection, useHierarchy, type HierarchyNode, } from "./hierarchy"; import styles from "./HierarchyView.module.css"; import { tx } from "@/platform/i18n"; type Node = { kind: "root"; id: string } | { kind: "item"; id: string; node: HierarchyNode }; const OPTIONS_KEY = "openidea.hierarchy.view.v1"; /** Autoscroll to Source: selecting a node shows it in the editor (IDEA: off). Persisted. */ export const useHierarchyOptions = create<{ autoscroll: boolean }>(() => { try { return { autoscroll: false, ...JSON.parse(globalThis.localStorage?.getItem(OPTIONS_KEY) ?? "{}") }; } catch { return { autoscroll: false }; } }); useHierarchyOptions.subscribe((s) => { try { globalThis.localStorage?.setItem(OPTIONS_KEY, JSON.stringify(s)); } catch { /* storage unavailable */ } }); /** Tool window header: the `Callers of foo` content tabs. */ export function HierarchyTabs() { const state = useHierarchy(); const tabs = state.tabs.flatMap((tab) => { const h = tabHierarchy(state, tab); return h.root ? [{ tab, label: hierarchyTitle(h.mode, h.direction, h.root.name) }] : []; }); if (!tabs.length || state.active === null) return null; return ( ({ id: String(tab.id), label, closable: true, pinned: tab.pinned }))} activeId={String(state.active)} onSelect={(id) => selectHierarchyTab(Number(id))} onClose={(id) => closeHierarchyTab(Number(id))} onTogglePin={(id) => { const tab = tabs.find((t) => t.tab.id === Number(id))?.tab; if (tab) pinHierarchyTab(tab.id, !tab.pinned); }} onContextMenu={(id, e) => { const tab = tabs.find((t) => t.tab.id === Number(id))?.tab; if (!tab) return; // IDEA's content tab menu. showContextMenu(e, [ { id: "close", label: "Close Tab", run: () => closeHierarchyTab(tab.id) }, { id: "closeAll", label: "Close All Tabs", run: () => tabs.forEach((t) => closeHierarchyTab(t.tab.id)) }, { id: "closeOthers", label: "Close Other Tabs", disabled: tabs.length < 2, run: () => tabs.forEach((t) => t.tab.id !== tab.id && closeHierarchyTab(t.tab.id)), }, { kind: "separator" }, { id: "pin", label: tab.pinned ? "Unpin Tab" : "Pin Tab", run: () => pinHierarchyTab(tab.id, !tab.pinned) }, ]); }} /> ); } function HierarchyToolbar() { const mode = useHierarchy((s) => s.mode); const direction = useHierarchy((s) => s.direction); const autoscroll = useHierarchyOptions((s) => s.autoscroll); return (
setDirection("incoming")} /> setDirection("outgoing")} /> useHierarchyOptions.setState({ autoscroll: !autoscroll })} />
); } export function HierarchyView() { const { mode, direction, root, children, failed, notes, loading, expanded, generation } = useHierarchy(); const projectRoot = useProject((s) => s.project?.root ?? ""); const roots = useMemo(() => (root ? [{ kind: "root", id: ROOT_ID }] : []), [root]); const getChildren = useCallback( (n: Node): Node[] | undefined => children.get(n.id)?.map((c) => ({ kind: "item", id: c.id, node: c }) as Node), [children], ); const isExpandable = useCallback( (n: Node) => { const loaded = children.get(n.id); return loaded === undefined || loaded.length > 0; }, [children], ); const tree = useRef(null); const targetOf = (n: Node) => { if (n.kind === "item") return hierarchyTarget(mode, direction, n.node.item); return root ? { path: root.path, line: root.line, col: root.col } : null; }; const show = (n: Node, focus: boolean) => { const t = targetOf(n); if (t) void maybeEditorService()?.openFile(t.path, { selection: { line: t.line, col: t.col }, focus }); }; // Every node is an occurrence, as in IDEA's hierarchy views. useOccurrenceNavigator("hierarchy", (dir, focus) => { const step = tree.current?.selectOccurrence(dir); if (!step) return null; show(step.node as Node, focus); return step; }); if (!root) return ( Use Call Hierarchy (⌃⌥H) or Type Hierarchy (⌃H) on a symbol to see its hierarchy here ); const renderNode = (n: Node, _state: TreeRowState) => { if (n.kind === "root") { return ( {root.name} {root.detail && {root.detail}} {basename(root.path)}:{root.line + 1} {loading && } {failed.has(ROOT_ID) && ( failed to load )} {!loading && !failed.has(ROOT_ID) && children.get(ROOT_ID)?.length === 0 && ( {noneFoundLabel(mode, direction)} )} ); } const { item } = n.node; const calls = item.fromRanges.length; return ( {item.name} {item.detail && {item.detail}} {basename(item.path)}:{item.selectionRange.start.line + 1} {calls > 1 && {tx`(${calls} calls)`}} {n.node.recursion && ( recursive )} {notes.has(n.id) && ( {notes.get(n.id)} )} {failed.has(n.id) && ( failed to load )} ); }; return (
{notes.has(ROOT_ID) && (
{notes.get(ROOT_ID)}
)}
key={generation} aria-label={mode === "call" ? "Call hierarchy" : "Type hierarchy"} roots={roots} getKey={(n) => n.id} getLabel={(n) => (n.kind === "root" ? root.name : n.node.item.name)} getChildren={getChildren} isExpandable={isExpandable} loadChildren={(n) => (n.kind === "item" ? loadChildren(n.node) : undefined)} onExpandedChange={(opened, n, open) => { useHierarchy.setState({ expanded: [...opened] }); const shown = open ? children.get(n.id) : undefined; if (shown) prefetchLevels(shown); }} defaultExpanded={expanded} handleRef={tree} isOccurrence={() => true} onSelectionChange={(_sel, n) => { if (n && useHierarchyOptions.getState().autoscroll) show(n, false); }} renderNode={renderNode} activateExpandable multiSelect={false} onActivate={(n) => { const target = n.kind === "root" ? root : hierarchyTarget(mode, direction, n.node.item); void openLocation(target.path, target.line, target.col); }} />
); }