import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import clsx from "clsx"; import type { GroupImperativeHandle, Layout } from "react-resizable-panels"; import type { RunConfig } from "@/platform/ipc"; import type { RunTargetValid } from "@/platform/remoteSettings"; import { useAgentRunSettings, useWorkflowSettings } from "@/platform/workflowSettings"; import { EmptyState, Icon, IconButton, Menu, SplitGroup, SplitHandle, SplitPanel, Tabs, type TabItem, T, showContextMenu, } from "@/ui"; import { executeAction } from "@/platform/actions"; import { formatBinding, hostBinding, shortcutLabel } from "@/platform/keymap"; import { ConsoleModel } from "./console"; import { ConsoleView } from "./ConsoleView"; import { TerminalConsole } from "./TerminalConsole"; import { runDirectory } from "./navigate"; import { remoteLabel } from "./remote"; import { acceptsInput, baseConfigOf, canRunSelectedTests, clearConsole, closeStdin, debugSession, failedTests, rerunFailed, rerunSession, resizeTerminal, runSelectedTests, runtime, sendStdin, setPendingInput, stopSession, useRun, writeTerminal, type RunSession, isLive, pinSession, } from "./store"; import { rerunFailedConfigs } from "./testing/rerun"; import { formatDuration, nodeOutput } from "./testing"; import { TestStatusBar, TestTreeView } from "./TestTreeView"; import { StackTracePanel, useStackTraces, type StackTraceTab } from "./stackTrace"; import { closeRunTab, runTabMenu } from "./runTabs"; import { showApplicationUpdate, useApplicationUpdates } from "./applicationUpdate"; import { isUpdatable } from "./updatePolicies"; import { hotReload, hotRestart, supportsHotReload } from "./flutterHotReload"; import { configIcon } from "./configs"; import { agentName } from "./agentConfig"; import { archivedAgentSessions, closeAgentArchive } from "./agentRuns"; import styles from "./Run.module.css"; import { tx } from "@/platform/i18n"; const TEST_SPLIT_DEFAULTS = { horizontal: { tests: 34, console: 66 }, vertical: { tests: 48, console: 52 }, }; function failed(s: RunSession): boolean { return s.status === "failed" || (s.status === "finished" && s.exitCode !== 0 && !s.stopped); } /** * Tab icon, IDEA's three states: the green execution arrow while the process is alive, * the red error badge for a failed exit, and once the process ended the run * configuration's own icon (Services keeps the same model, see services.tsx). */ export function sessionIcon(s: RunSession): string { if (isLive(s)) return s.config.isTest ? "check" : "run"; if (failed(s)) return "error"; // Ended (exit or stop): the configuration's own icon — the green arrow stays "still running". return configIcon(s.config); } export function SessionPanel({ session, showLifecycle = true, }: { session: RunSession; /** False in the Services detail: the window's own toolbar already offers Run/Debug/Stop for the selection. */ showLifecycle?: boolean; }) { const updating = useApplicationUpdates((s) => s.ids.has(session.id)); const version = useRun((s) => s.versions[session.id] ?? 0); const rt = runtime(session.id); const [follow, setFollow] = useState(true); // Settings › Console › Use soft wraps is the default; the toolbar toggle overrides it for this tab. const defaultWrap = useWorkflowSettings((s) => s.console.softWrap); const [wrapOverride, setWrapOverride] = useState(null); const softWrap = wrapOverride ?? defaultWrap; const [selectedTest, setSelectedTest] = useState(null); const [findOpen, setFindOpen] = useState(false); const body = useRef(null); const [stackedTests, setStackedTests] = useState(false); const testSplit = useRef(null); const testLayouts = useRef>({ ...TEST_SPLIT_DEFAULTS }); const testOrientation = stackedTests ? "vertical" : "horizontal"; const hasTests = !!rt?.tests; useLayoutEffect(() => { const pane = body.current; if (!hasTests || !pane) return; const measure = () => setStackedTests(pane.clientWidth < 560); measure(); if (typeof ResizeObserver === "undefined") return; const resize = new ResizeObserver(measure); resize.observe(pane); return () => resize.disconnect(); }, [hasTests]); useLayoutEffect(() => { if (hasTests) testSplit.current?.setLayout(testLayouts.current[testOrientation]); }, [hasTests, testOrientation]); const running = isLive(session); const cwd = runDirectory(session.config.cwd); const baseConfig = useRun(() => baseConfigOf(session)); // Test results mutate in place; their version refreshes the selection. const failedSelection = useMemo( () => (running ? [] : rerunFailedConfigs(baseConfig, failedTests(session.id))), [running, baseConfig, session.id, version], // eslint-disable-line react-hooks/exhaustive-deps ); const [resolvedSelection, setResolvedSelection] = useState<{ request: Promise; supported: boolean; } | null>(null); useEffect(() => { let current = true; if (failedSelection instanceof Promise) { void failedSelection.then((configs) => { if (current) setResolvedSelection({ request: failedSelection, supported: configs.length > 0 }); }); } return () => { current = false; }; }, [failedSelection]); const canRerunFailed = failedSelection instanceof Promise ? resolvedSelection?.request === failedSelection && resolvedSelection.supported : failedSelection.length > 0; // Selecting a test or a group with captured output shows just that output (like IDEA). const testModel = useMemo(() => { const node = selectedTest ? rt?.tests?.get(selectedTest) : undefined; const output = node ? nodeOutput(node) : []; if (output.length === 0) return null; const m = new ConsoleModel(); m.append({ kind: "stdout", text: output.join("\n") + "\n" }); return m; }, [selectedTest, rt, version]); // eslint-disable-line react-hooks/exhaustive-deps if (!rt) return null; const toggleWrap = () => setWrapOverride(!softWrap); const consoleColumn = (
{rt.tests && } {rt.console.dropped > 0 && !testModel && (
{tx`${rt.console.dropped.toLocaleString()} earlier lines were discarded`}
)} {testModel && (
)} {rt.terminal && !testModel ? ( writeTerminal(session.id, data)} onResize={(cols, rows) => resizeTerminal(session.id, cols, rows)} aria-label={tx`${session.config.name} terminal`} /> ) : ( clearConsole(session.id)} input={ !testModel && acceptsInput(session) ? { pending: rt.input, onChange: (text) => setPendingInput(session.id, text), onSubmit: (line) => void sendStdin(session.id, line), onEof: () => void closeStdin(session.id), } : undefined } aria-label={tx`${session.config.name} output`} /> )}
); return (
{showLifecycle && (
rerunSession(session.id)} /> {/* Switch the launch mode: the same configuration started under the debugger. */} void debugSession(session.id)} /> {rt.tests && ( void rerunFailed(session.id)} /> )} void stopSession(session.id)} /> {isUpdatable(session.config) && ( showApplicationUpdate(session.id)} /> )} {supportsHotReload(session.config) && ( <> hotReload(session.id)} /> hotRestart(session.id)} /> )}
)}
{!rt.terminal && ( <> setFindOpen((v) => !v)} /> setFollow((v) => !v)} /> )} clearConsole(session.id)} />
void executeAction("run.editConfigs", session.config.id)} />
{rt.tests ? ( { if (meta.isUserInteraction) testLayouts.current[testOrientation] = meta.requestedLayout ?? layout; }} > canRunSelectedTests(session.id, ids)} onRunTests={(ids, debug) => void runSelectedTests(session.id, ids, debug)} /> {consoleColumn} ) : ( consoleColumn )}
); } function sessionItems(sessions: readonly RunSession[], targets: readonly RunTargetValid[]): TabItem[] { return sessions.map((s) => ({ id: s.id, label: remoteLabel(s.config, targets), text: remoteLabel(s.config, targets), icon: sessionIcon(s), color: failed(s) ? "var(--danger)" : undefined, // An agent's command that ended, however it ended. dimmed: agentName(s.config) !== null && !isLive(s), closable: true, pinned: s.pinned, tooltip: s.status === "finished" ? tx`Finished in ${formatDuration((s.endedAt ?? s.startedAt) - s.startedAt)}, exit code ${s.exitCode ?? "?"}` : undefined, })); } /** The selected tab: a run session or an analyzed stack trace (IDEA shows both in Run). */ function currentTab( sessions: readonly RunSession[], traces: readonly StackTraceTab[], active: string | null, ): { session: RunSession } | { trace: StackTraceTab } | null { const session = sessions.find((s) => s.id === active); if (session) return { session }; const trace = traces.find((t) => t.id === active); if (trace) return { trace }; if (sessions.length) return { session: sessions[sessions.length - 1]! }; return traces.length ? { trace: traces[traces.length - 1]! } : null; } /** Session (and stack trace) tabs in the tool window header. */ export function RunTabs() { const sessions = useRun((s) => s.sessions); const traces = useStackTraces((s) => s.tabs); const runTargets = useWorkflowSettings((s) => s.runTargets); const active = useRun((s) => s.activeSession); const { tabLimit } = useAgentRunSettings(); const current = currentTab(sessions, traces, active); if (!current) return null; const archived = archivedAgentSessions(sessions, tabLimit); const shown = archived.length ? sessions.filter((s) => !archived.includes(s)) : sessions; const items: TabItem[] = [ ...sessionItems(shown, runTargets), ...traces.map((t) => ({ id: t.id, label: t.title, text: t.title, closable: true })), ]; const activeId = "session" in current ? current.session.id : current.trace.id; return ( useRun.setState({ activeSession: id })} onClose={(id) => void closeRunTab(id)} onTogglePin={(id) => { const s = sessions.find((x) => x.id === id); if (s) pinSession(s.id, !s.pinned); }} onContextMenu={(id, e) => { e.preventDefault(); showContextMenu(e, runTabMenu(id), { owner: e.currentTarget }); }} panelIdPrefix="openidea-run" trailing={archived.length > 0 && } /> ); } /** The folder of archived agent tabs at the end of the strip, with their number. */ function AgentArchive({ sessions, activeId }: { sessions: RunSession[]; activeId: string }) { const anchor = useRef(null); const [open, setOpen] = useState(false); const holdsActive = sessions.some((s) => s.id === activeId); const count = sessions.length; return ( <> setOpen(false)} aria-label="Archived agent tabs" items={[ ...sessions.map((s) => ({ id: s.id, label: s.config.name, verbatim: true, icon: sessionIcon(s), checked: s.id === activeId, run: () => useRun.setState({ activeSession: s.id }), })), { kind: "separator" as const }, { label: "Close All Archived Tabs", run: () => closeAgentArchive() }, ]} /> ); } export function RunView() { const sessions = useRun((s) => s.sessions); const traces = useStackTraces((s) => s.tabs); const active = useRun((s) => s.activeSession); const current = currentTab(sessions, traces, active); if (!current) { // IDEA's empty Run tool window: the three ways to run something. const chooser = shortcutLabel("run.chooseConfig"); return (
To run your code, do one of the following:
  • Click the Run icon in the editor gutter
  • {'Select "Run…" in the editor context menu'}
  • Launch a{" "} {chooser && ({chooser})}
); } const id = "session" in current ? current.session.id : current.trace.id; return (
{"session" in current ? ( ) : ( )}
); }