Файли
openidea/src/features/database/resultTabActions.ts
T
Mr0Bread 7d146ef1b7 feat(database): reuse a console's result tabs on each run, pin tabs to keep them (#3113)
Running a console added a new Result N tab per run. As in IntelliJ, a run now replaces that console's unpinned result tabs (in place of the first of them); a run without result sets keeps them and shows Output. Pin Tab (tab menu or the result toolbar) keeps a result: its close button becomes Unpin and close gestures spare it; Close All but Pinned closes the rest. Explain Plan replaces only an unpinned plan tab, and the 12-tab limit never evicts pinned or just-created tabs. The shared Tabs component gets an opt-in pinned item flag.

Co-authored-by: Mr0Bread <[email protected]>
2026-11-13 17:37:59 +08:00

132 рядки
4.8 KiB
TypeScript

import { registerActions, type ActionContext } from "@/platform/actions";
import { promptInput } from "@/workbench/inputDialogs";
import type { MenuItem } from "@/ui";
import { OUTPUT_TAB, closeResult, pinResult, useDatabase } from "./store";
function target({ arg, ctx }: ActionContext): number | null {
const id = typeof arg === "number" ? arg : ctx.dbResultsFocus ? useDatabase.getState().activeResult : null;
return id === OUTPUT_TAB || useDatabase.getState().results.some((r) => r.id === id) ? id : null;
}
function closable(c: ActionContext): boolean {
const id = target(c);
return id !== null && id !== OUTPUT_TAB;
}
function closeOthers(id: number) {
for (const tab of useDatabase.getState().results) if (tab.id !== id) closeResult(tab.id);
}
function pinned(id: number | null): boolean {
return !!useDatabase.getState().results.find((r) => r.id === id)?.pinned;
}
function cycle(direction: 1 | -1, focus: boolean) {
const { results, activeResult } = useDatabase.getState();
const ids = [OUTPUT_TAB, ...results.map((r) => r.id)];
const index = ids.indexOf(activeResult ?? OUTPUT_TAB);
if (index < 0) return;
const next = ids[(index + direction + ids.length) % ids.length]!;
if (focus) {
const button = document.getElementById(`db-results-tab-${next}`);
button?.focus();
// An inline editor can hand focus back to its grid when it blurs.
if (document.activeElement !== button) button?.focus();
}
useDatabase.setState({ activeResult: next });
}
export function registerResultTabActions(): () => void {
return registerActions([
{
id: "db.closeResultTab",
title: "Close",
category: "Database",
icon: "close",
keyWhen: "dbResultsFocus && !popupOpen",
enabled: closable,
run: (c) => closeResult(target(c)!),
},
{
id: "db.closeOtherResultTabs",
title: "Close Others",
category: "Database",
keyWhen: "dbResultsFocus && !popupOpen",
enabled: (c) => target(c) !== null && useDatabase.getState().results.some((r) => r.id !== target(c)),
run: (c) => closeOthers(target(c)!),
},
{
id: "db.closeAllResultTabs",
title: "Close All",
category: "Database",
keyWhen: "dbResultsFocus && !popupOpen",
enabled: (c) => target(c) !== null && useDatabase.getState().results.length > 0,
run: () => closeOthers(OUTPUT_TAB),
},
{
id: "db.closeAllButPinnedResultTabs",
title: "Close All but Pinned",
category: "Database",
keyWhen: "dbResultsFocus && !popupOpen",
enabled: (c) => {
const { results } = useDatabase.getState();
return target(c) !== null && results.some((r) => r.pinned) && results.some((r) => !r.pinned);
},
run: () => {
for (const tab of useDatabase.getState().results) if (!tab.pinned) closeResult(tab.id);
},
},
{
id: "db.pinResultTab",
title: "Pin / Unpin Tab",
category: "Database",
icon: "pin",
keyWhen: "dbResultsFocus && !popupOpen",
enabled: closable,
run: (c) => {
const id = target(c)!;
pinResult(id, !pinned(id));
},
},
{
id: "db.renameResultTab",
title: "Rename Tab",
category: "Database",
icon: "edit",
keyWhen: "dbResultsFocus && !popupOpen",
enabled: closable,
run: async (c) => {
const id = target(c);
const tab = useDatabase.getState().results.find((r) => r.id === id);
if (!tab) return;
const title = await promptInput({ title: "Rename Tab", label: "Tab name:", initial: tab.title });
if (title === null) return;
useDatabase.setState((s) => ({ results: s.results.map((r) => (r.id === id ? { ...r, title } : r)) }));
},
},
...([1, -1] as const).map((direction) => ({
id: direction === 1 ? "db.nextResultTab" : "db.previousResultTab",
title: direction === 1 ? "Select Next Tab" : "Select Previous Tab",
category: "Database",
keyWhen: "dbResultsFocus && !popupOpen",
enabled: (c: ActionContext) => target(c) !== null && useDatabase.getState().results.length > 0,
run: ({ ctx }: ActionContext) => cycle(direction, !!ctx.dbResultsFocus),
})),
]);
}
export function resultTabMenu(id: number): MenuItem[] {
return [
{ kind: "action", action: "db.closeResultTab", arg: id },
{ kind: "action", action: "db.closeOtherResultTabs", arg: id },
{ kind: "action", action: "db.closeAllResultTabs", arg: id },
{ kind: "action", action: "db.closeAllButPinnedResultTabs", arg: id },
{ kind: "separator" },
{ kind: "action", action: "db.pinResultTab", arg: id, label: pinned(id) ? "Unpin Tab" : "Pin Tab" },
{ kind: "action", action: "db.renameResultTab", arg: id },
{ kind: "separator" },
{ kind: "action", action: "db.nextResultTab", arg: id },
{ kind: "action", action: "db.previousResultTab", arg: id },
];
}