Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

fix(debugger): let session tabs be closed (#1472)

payailk8869f1ce1c9e
diff --git a/src/features/debugger/__tests__/toolWindow.test.tsx b/src/features/debugger/__tests__/toolWindow.test.tsx
index 214484166..281cb2753 100644
--- a/src/features/debugger/__tests__/toolWindow.test.tsx
+++ b/src/features/debugger/__tests__/toolWindow.test.tsx
@@ -25,6 +25,9 @@ vi.mock("@/platform/ipc", async (importOriginal) => {
};
});
+const confirmTerminate = vi.hoisted(() => vi.fn(async () => false));
+vi.mock("@/workbench/confirmTerminate", () => ({ confirmTerminate }));
+
import { useDebug, type DebugSession } from "../state";
import { DebugTabs, DebugToolWindow } from "../ui/DebugToolWindow";
@@ -82,4 +85,49 @@ describe("debug tool window", () => {
// A multi-line rendering stays on its row.
expect(screen.getByText(/dtype: object/).textContent).toBe("high 7.86 low 7.66 Name: 772, dtype: object");
});
+
+ it("closes a session tab and moves the selection to the one left", async () => {
+ useDebug.setState({
+ sessions: {
+ 1: { ...session, status: "terminated" },
+ 2: { ...session, id: 2, name: "app", status: "terminated" },
+ },
+ active: 1,
+ });
+ render(<DebugTabs />);
+ await act(async () => fireEvent.click(screen.getAllByRole("button", { name: "Close Tab" })[0]!));
+ expect(screen.getAllByRole("tab").map((tab) => tab.textContent)).toEqual(["app"]);
+ expect(useDebug.getState().active).toBe(2);
+ expect(useDebug.getState().sessions).not.toHaveProperty("1");
+ });
+
+ it("passes the selection of a closed tab to the tab on its left", async () => {
+ useDebug.setState({
+ sessions: {
+ 4: { ...session, id: 4, name: "a", status: "terminated" },
+ 5: { ...session, id: 5, name: "b", status: "terminated" },
+ 6: { ...session, id: 6, name: "c", status: "terminated" },
+ },
+ active: 5,
+ });
+ render(<DebugTabs />);
+ await act(async () => fireEvent.click(screen.getAllByRole("button", { name: "Close Tab" })[1]!));
+ expect(useDebug.getState().active).toBe(4);
+ });
+
+ it("keeps a live session when the user doesn't terminate it", async () => {
+ useDebug.setState({ sessions: { 8: { ...session, id: 8, name: "server" } }, active: 8 });
+ render(<DebugTabs />);
+ await act(async () => fireEvent.click(screen.getAllByRole("button", { name: "Close Tab" })[0]!));
+ expect(confirmTerminate).toHaveBeenCalledWith("server");
+ expect(useDebug.getState().sessions).toHaveProperty("8");
+ });
+
+ it("leaves nothing selected once the last tab is closed", async () => {
+ useDebug.setState({ sessions: { 3: { ...session, id: 3, status: "terminated" } }, active: 3 });
+ render(<DebugTabs />);
+ await act(async () => fireEvent.click(screen.getAllByRole("button", { name: "Close Tab" })[0]!));
+ expect(useDebug.getState().active).toBeNull();
+ expect(useDebug.getState().sessions).toEqual({});
+ });
});
diff --git a/src/features/debugger/state.ts b/src/features/debugger/state.ts
index 130e533d5..b3663f694 100644
--- a/src/features/debugger/state.ts
+++ b/src/features/debugger/state.ts
@@ -23,6 +23,7 @@ import {
type ThreadInfo,
type Variable,
} from "@/platform/ipc";
+import { confirmTerminate } from "@/workbench/confirmTerminate";
import { maybeEditorService, notifications, toolWindowService, useDebugRunSessions } from "@/workbench/contributions";
import { breakpointsOf, consumeTemporary, flushBreakpoints, refreshStates, resetStatuses } from "./breakpoints/store";
import { interpolateLog, logParts, suspends } from "./breakpoints/model";
@@ -632,6 +633,51 @@ export async function stopSession(id: DebugSessionId, options: { stopProcess?: b
await call(commands.runStop(root.process)).catch(() => undefined);
}
+/**
+ * Closes a session's tab: a live session is stopped first once the user agrees (the Run
+ * tab's question), then the window forgets it — and its children.
+ */
+export async function closeSessionTab(id: DebugSessionId): Promise<boolean> {
+ const s = useDebug.getState().sessions[id];
+ if (!s) return false;
+ if (isLive(s)) {
+ if (workflowSettings().run.confirmTerminate && !(await confirmTerminate(s.name))) return false;
+ await stopSession(s.id);
+ }
+ removeSession(s.id);
+ return true;
+}
+
+/** Forgets a session and its children; a selected one passes the selection to the tab on its left. */
+export function removeSession(id: DebugSessionId) {
+ const st = useDebug.getState();
+ const gone = new Set<DebugSessionId>([id]);
+ for (const s of Object.values(st.sessions)) if (s.parent === id) gone.add(s.id);
+
+ const order = Object.values(st.sessions).map((s) => s.id);
+ const sessions: Record<number, DebugSession> = {};
+ for (const s of Object.values(st.sessions)) if (!gone.has(s.id)) sessions[s.id] = s;
+ const console = { ...st.console };
+ for (const goneId of gone) delete console[goneId];
+ const at = order.indexOf(id);
+ const neighbour =
+ order
+ .slice(0, at)
+ .reverse()
+ .find((n) => sessions[n]) ??
+ order.slice(at + 1).find((n) => sessions[n]) ??
+ null;
+
+ useDebug.setState({
+ sessions,
+ active: st.active != null && sessions[st.active] ? st.active : neighbour,
+ console,
+ });
+ for (const goneId of gone) owned.delete(goneId);
+ updateExecutionPoint();
+ publishContext();
+}
+
export async function rerunSession(id: DebugSessionId) {
const s = useDebug.getState().sessions[id];
if (!s) return;
diff --git a/src/features/debugger/ui/DebugToolWindow.tsx b/src/features/debugger/ui/DebugToolWindow.tsx
index 70d990d53..93c65092d 100644
--- a/src/features/debugger/ui/DebugToolWindow.tsx
+++ b/src/features/debugger/ui/DebugToolWindow.tsx
@@ -26,6 +26,7 @@ import { frameLabel, groupFrames } from "../frames";
import { blurDebugView, focusDebugView } from "./focusedView";
import {
addWatch,
+ closeSessionTab,
evaluate,
selectFrame,
selectSession,
@@ -120,6 +121,7 @@ export function DebugTabs() {
text: s.name,
icon: "debug",
italic: s.status === "terminated",
+ closable: true,
tooltip: statusText(s),
}));
return (
@@ -130,6 +132,7 @@ export function DebugTabs() {
items={items}
activeId={active != null ? String(active) : null}
onSelect={(id) => selectSession(+id)}
+ onClose={(id) => void closeSessionTab(+id)}
panelIdPrefix="openidea-debug"
/>
);
diff --git a/src/features/run/dialogs.tsx b/src/features/run/dialogs.tsx
index 3c7e53e9a..67f1a3660 100644
--- a/src/features/run/dialogs.tsx
+++ b/src/features/run/dialogs.tsx
@@ -52,24 +52,7 @@ export async function confirmRerun(name: string, instances = 1): Promise<boolean
return answer !== null;
}
-/** IDEA's question before closing a Run tab whose process still runs. Resolves true for Terminate. */
-export async function confirmTerminate(name: string): Promise<boolean> {
- const answer = await confirmChecked({
- title: tx`Process '${name}' Is Running`,
- message: tx`Do you want to terminate the process '${name}'?`,
- ok: "Terminate",
- checkbox: "Do not ask again",
- });
- if (answer?.checked && isTauri()) {
- try {
- await call(commands.settingsSet("global", "run.confirmTerminate", "false"));
- await loadSettings();
- } catch {
- /* keep asking */
- }
- }
- return answer !== null;
-}
+export { confirmTerminate } from "@/workbench/confirmTerminate";
/** Asks for the value of `$Prompt$`; null when cancelled. */
export function promptValue(title: string): Promise<string | null> {
diff --git a/src/workbench/confirmTerminate.ts b/src/workbench/confirmTerminate.ts
new file mode 100644
index 000000000..0838fe9b6
--- /dev/null
+++ b/src/workbench/confirmTerminate.ts
@@ -0,0 +1,26 @@
+import { tx } from "@/platform/i18n";
+import { call, commands, isTauri } from "@/platform/ipc";
+import { loadSettings } from "@/platform/project";
+import { confirmChecked } from "./messages";
+
+/**
+ * IDEA's question before closing a Run or Debug tab whose process still runs (asked while
+ * `run.confirmTerminate` is on). Resolves true for Terminate.
+ */
+export async function confirmTerminate(name: string): Promise<boolean> {
+ const answer = await confirmChecked({
+ title: tx`Process '${name}' Is Running`,
+ message: tx`Do you want to terminate the process '${name}'?`,
+ ok: "Terminate",
+ checkbox: "Do not ask again",
+ });
+ if (answer?.checked && isTauri()) {
+ try {
+ await call(commands.settingsSet("global", "run.confirmTerminate", "false"));
+ await loadSettings();
+ } catch {
+ /* keep asking */
+ }
+ }
+ return answer !== null;
+}