дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-19 08:05:40 +02:00
Порівняти коміти
1
Коміти
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
950c56234e |
No files matched your search
@@ -9,6 +9,7 @@ import {
|
||||
Spinner,
|
||||
SymbolBadge,
|
||||
Tabs,
|
||||
showContextMenu,
|
||||
Tree,
|
||||
badgeKind,
|
||||
type TreeHandle,
|
||||
@@ -17,8 +18,11 @@ import {
|
||||
} from "@/ui";
|
||||
import { openLocation } from "./ipc";
|
||||
import {
|
||||
closeHierarchy,
|
||||
closeHierarchyTab,
|
||||
pinHierarchyTab,
|
||||
ROOT_ID,
|
||||
selectHierarchyTab,
|
||||
tabHierarchy,
|
||||
directionLabel,
|
||||
hierarchyTitle,
|
||||
hierarchyTarget,
|
||||
@@ -54,22 +58,44 @@ useHierarchyOptions.subscribe((s) => {
|
||||
}
|
||||
});
|
||||
|
||||
/** Tool window header: `Callers of foo` content tab. */
|
||||
/** Tool window header: the `Callers of foo` content tabs. */
|
||||
export function HierarchyTabs() {
|
||||
const mode = useHierarchy((s) => s.mode);
|
||||
const direction = useHierarchy((s) => s.direction);
|
||||
const root = useHierarchy((s) => s.root);
|
||||
if (!root) return null;
|
||||
const label = hierarchyTitle(mode, direction, root.name);
|
||||
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 (
|
||||
<Tabs
|
||||
variant="compact"
|
||||
className={styles.tabs}
|
||||
aria-label="Hierarchies"
|
||||
items={[{ id: "current", label, closable: true }]}
|
||||
activeId="current"
|
||||
onSelect={() => undefined}
|
||||
onClose={closeHierarchy}
|
||||
items={tabs.map(({ tab, label }) => ({ 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) },
|
||||
]);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -114,7 +140,7 @@ function HierarchyToolbar() {
|
||||
}
|
||||
|
||||
export function HierarchyView() {
|
||||
const { mode, direction, root, children, failed, notes, loading, generation } = useHierarchy();
|
||||
const { mode, direction, root, children, failed, notes, loading, expanded, generation } = useHierarchy();
|
||||
const projectRoot = useProject((s) => s.project?.root ?? "");
|
||||
const roots = useMemo<Node[]>(() => (root ? [{ kind: "root", id: ROOT_ID }] : []), [root]);
|
||||
|
||||
@@ -224,11 +250,12 @@ export function HierarchyView() {
|
||||
getChildren={getChildren}
|
||||
isExpandable={isExpandable}
|
||||
loadChildren={(n) => (n.kind === "item" ? loadChildren(n.node) : undefined)}
|
||||
onExpandedChange={(_expanded, n, open) => {
|
||||
onExpandedChange={(opened, n, open) => {
|
||||
useHierarchy.setState({ expanded: [...opened] });
|
||||
const shown = open ? children.get(n.id) : undefined;
|
||||
if (shown) prefetchLevels(shown);
|
||||
}}
|
||||
defaultExpanded={[ROOT_ID]}
|
||||
defaultExpanded={expanded}
|
||||
handleRef={tree}
|
||||
isOccurrence={() => true}
|
||||
onSelectionChange={(_sel, n) => {
|
||||
|
||||
@@ -306,3 +306,45 @@ describe("closing the hierarchy tab", () => {
|
||||
expect(hide).toHaveBeenCalledWith("hierarchy", { removeFromStripe: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe("hierarchy tabs", () => {
|
||||
it("replace the selected tab unless it is pinned, and keep each tab's tree", async () => {
|
||||
const { closeHierarchyTab, pinHierarchyTab, selectHierarchyTab, tabHierarchy } = await import("./hierarchy");
|
||||
const names = () => {
|
||||
const s = useHierarchy.getState();
|
||||
return s.tabs.map((t) => `${tabHierarchy(s, t).mode}:${t.pinned ? "pinned" : ""}`);
|
||||
};
|
||||
resetHierarchy();
|
||||
await showHierarchy("call");
|
||||
await showHierarchy("type");
|
||||
expect(names()).toEqual(["type:"]);
|
||||
const first = useHierarchy.getState().active!;
|
||||
pinHierarchyTab(first, true);
|
||||
await showHierarchy("call");
|
||||
expect(names()).toEqual(["type:pinned", "call:"]);
|
||||
const second = useHierarchy.getState().active!;
|
||||
expect(
|
||||
useHierarchy
|
||||
.getState()
|
||||
.children.get(ROOT_ID)
|
||||
?.map((n) => n.item.name),
|
||||
).toEqual(["main"]);
|
||||
|
||||
selectHierarchyTab(first);
|
||||
expect(useHierarchy.getState().mode).toBe("type");
|
||||
selectHierarchyTab(second);
|
||||
expect(useHierarchy.getState().mode).toBe("call");
|
||||
expect(
|
||||
useHierarchy
|
||||
.getState()
|
||||
.children.get(ROOT_ID)
|
||||
?.map((n) => n.item.name),
|
||||
).toEqual(["main"]);
|
||||
|
||||
closeHierarchyTab(second);
|
||||
expect(useHierarchy.getState()).toMatchObject({ active: first, mode: "type" });
|
||||
expect(names()).toEqual(["type:pinned"]);
|
||||
closeHierarchyTab(first);
|
||||
expect(useHierarchy.getState()).toMatchObject({ root: null, tabs: [], active: null });
|
||||
});
|
||||
});
|
||||
@@ -54,8 +54,27 @@ interface HierarchyState {
|
||||
/** Parent id → why its children may be missing callers. */
|
||||
notes: ReadonlyMap<string, string>;
|
||||
loading: boolean;
|
||||
/** Ids of the expanded nodes, so a tab shown again keeps its tree open. */
|
||||
expanded: readonly string[];
|
||||
/** Bumped per (re)query so stale responses are dropped and the tree resets. */
|
||||
generation: number;
|
||||
/** Content tabs; the selected one's hierarchy is the state above. */
|
||||
tabs: readonly HierarchyTab[];
|
||||
active: number | null;
|
||||
}
|
||||
|
||||
/** The hierarchy of a tab that isn't selected. */
|
||||
export type SavedHierarchy = Pick<
|
||||
HierarchyState,
|
||||
"mode" | "direction" | "root" | "children" | "failed" | "notes" | "loading" | "expanded"
|
||||
>;
|
||||
|
||||
export interface HierarchyTab {
|
||||
id: number;
|
||||
/** Pinned: a new hierarchy opens beside it instead of taking its place. */
|
||||
pinned: boolean;
|
||||
/** Null for the selected tab. */
|
||||
saved: SavedHierarchy | null;
|
||||
}
|
||||
|
||||
export const ROOT_ID = "root";
|
||||
@@ -68,9 +87,89 @@ export const useHierarchy = create<HierarchyState>(() => ({
|
||||
failed: new Set(),
|
||||
notes: new Map(),
|
||||
loading: false,
|
||||
expanded: [ROOT_ID],
|
||||
generation: 0,
|
||||
tabs: [],
|
||||
active: null,
|
||||
}));
|
||||
|
||||
let tabSeq = 0;
|
||||
|
||||
function saved(s: HierarchyState): SavedHierarchy {
|
||||
const { mode, direction, root, children, failed, notes, loading, expanded } = s;
|
||||
return { mode, direction, root, children, failed, notes, loading, expanded };
|
||||
}
|
||||
|
||||
/** A tab's mode, direction and root: the live state's for the selected one. */
|
||||
export function tabHierarchy(s: HierarchyState, tab: HierarchyTab): SavedHierarchy {
|
||||
return tab.saved ?? saved(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* As IDEA: a new hierarchy takes the place of the selected tab unless it is pinned,
|
||||
* then it opens in a tab of its own.
|
||||
*/
|
||||
function openTab() {
|
||||
const s = useHierarchy.getState();
|
||||
const current = s.tabs.find((t) => t.id === s.active);
|
||||
if (current && !current.pinned) return;
|
||||
cancelInflight();
|
||||
const id = ++tabSeq;
|
||||
useHierarchy.setState({
|
||||
tabs: [
|
||||
...s.tabs.map((t) => (t.id === s.active ? { ...t, saved: saved(s) } : t)),
|
||||
{ id, pinned: false, saved: null },
|
||||
],
|
||||
active: id,
|
||||
root: null,
|
||||
children: new Map(),
|
||||
failed: new Set(),
|
||||
notes: new Map(),
|
||||
loading: false,
|
||||
expanded: [ROOT_ID],
|
||||
generation: s.generation + 1,
|
||||
});
|
||||
}
|
||||
|
||||
export function selectHierarchyTab(id: number) {
|
||||
const s = useHierarchy.getState();
|
||||
const target = s.tabs.find((t) => t.id === id);
|
||||
if (id === s.active || !target?.saved) return;
|
||||
cancelInflight();
|
||||
const next = target.saved;
|
||||
useHierarchy.setState({
|
||||
...next,
|
||||
loading: false,
|
||||
generation: s.generation + 1,
|
||||
active: id,
|
||||
tabs: s.tabs.map((t) => (t.id === s.active ? { ...t, saved: saved(s) } : t.id === id ? { ...t, saved: null } : t)),
|
||||
});
|
||||
// Its requests were dropped when it was left: load it again.
|
||||
if (next.loading) void loadRoot();
|
||||
}
|
||||
|
||||
/** Closes a tab; the tool window goes with the last one. */
|
||||
export function closeHierarchyTab(id: number) {
|
||||
const s = useHierarchy.getState();
|
||||
const at = s.tabs.findIndex((t) => t.id === id);
|
||||
if (at < 0) return;
|
||||
const rest = s.tabs.filter((t) => t.id !== id);
|
||||
if (!rest.length) {
|
||||
closeHierarchy();
|
||||
return;
|
||||
}
|
||||
if (id !== s.active) {
|
||||
useHierarchy.setState({ tabs: rest });
|
||||
return;
|
||||
}
|
||||
useHierarchy.setState({ tabs: rest, active: null });
|
||||
selectHierarchyTab((s.tabs[at + 1] ?? s.tabs[at - 1])!.id);
|
||||
}
|
||||
|
||||
export function pinHierarchyTab(id: number, pinned: boolean) {
|
||||
useHierarchy.setState((s) => ({ tabs: s.tabs.map((t) => (t.id === id ? { ...t, pinned } : t)) }));
|
||||
}
|
||||
|
||||
/** Call hierarchy requests the current tree still waits for. */
|
||||
const inflight = new Set<RequestId>();
|
||||
|
||||
@@ -88,7 +187,10 @@ export function resetHierarchy() {
|
||||
failed: new Set(),
|
||||
notes: new Map(),
|
||||
loading: false,
|
||||
expanded: [ROOT_ID],
|
||||
generation: s.generation + 1,
|
||||
tabs: [],
|
||||
active: null,
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -248,7 +350,14 @@ async function loadRoot(quiet = false): Promise<RootResult> {
|
||||
if (!root) return "stale";
|
||||
const generation = s.generation + 1;
|
||||
cancelInflight();
|
||||
useHierarchy.setState({ generation, children: new Map(), failed: new Set(), notes: new Map(), loading: true });
|
||||
useHierarchy.setState({
|
||||
generation,
|
||||
children: new Map(),
|
||||
failed: new Set(),
|
||||
notes: new Map(),
|
||||
loading: true,
|
||||
expanded: [ROOT_ID],
|
||||
});
|
||||
try {
|
||||
const { items, incomplete } = await withDocument(root.path, (doc) =>
|
||||
request(s.mode, doc, root.path, root.line, root.col, s.direction),
|
||||
@@ -365,6 +474,7 @@ export async function showHierarchy(mode: HierarchyMode) {
|
||||
hint("Open a file to show the hierarchy");
|
||||
return;
|
||||
}
|
||||
openTab();
|
||||
const s = useHierarchy.getState();
|
||||
const direction = s.mode === mode ? s.direction : mode === "call" ? "incoming" : "outgoing";
|
||||
useHierarchy.setState({
|
||||
|
||||
Reference in new issue
Заблокувати користувача