Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat(debugger): add command history navigation with arrow keys (#7617)

liffddb7d86dda2
diff --git a/frontend/src/components/debugger/__tests__/debugger-code.test.tsx b/frontend/src/components/debugger/__tests__/debugger-code.test.tsx
new file mode 100644
index 0000000000..45c161747c
--- /dev/null
+++ b/frontend/src/components/debugger/__tests__/debugger-code.test.tsx
@@ -0,0 +1,149 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { fireEvent, render, screen } from "@testing-library/react";
+import { beforeEach, describe, expect, it, type Mock, vi } from "vitest";
+import { TooltipProvider } from "@/components/ui/tooltip";
+import { useInputHistory } from "@/hooks/useInputHistory";
+import { Debugger } from "../debugger-code";
+
+// Mock CodeMirror language extensions
+vi.mock("@uiw/codemirror-extensions-langs", () => ({
+ langs: {
+ shell: () => [],
+ python: () => [],
+ },
+}));
+
+// Mock useInputHistory hook
+vi.mock("@/hooks/useInputHistory");
+
+const renderWithProvider = (ui: React.ReactElement) => {
+ return render(<TooltipProvider>{ui}</TooltipProvider>);
+};
+
+// Default mock implementation
+const mockNavigateUp = vi.fn();
+const mockNavigateDown = vi.fn();
+const mockAddToHistory = vi.fn();
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ (useInputHistory as Mock).mockReturnValue({
+ history: [],
+ navigateUp: mockNavigateUp,
+ navigateDown: mockNavigateDown,
+ addToHistory: mockAddToHistory,
+ });
+});
+
+describe("Debugger", () => {
+ it("should render debugger output and controls", () => {
+ const onSubmit = vi.fn();
+ renderWithProvider(<Debugger code="(Pdb) test" onSubmit={onSubmit} />);
+
+ // Check that control buttons are rendered
+ expect(screen.getByTestId("debugger-next-button")).toBeInTheDocument();
+ expect(screen.getByTestId("debugger-continue-button")).toBeInTheDocument();
+ expect(screen.getByTestId("debugger-stack-button")).toBeInTheDocument();
+ expect(screen.getByTestId("debugger-help-button")).toBeInTheDocument();
+ });
+
+ it("should call onSubmit with 'n' when next button is clicked", () => {
+ const onSubmit = vi.fn();
+ renderWithProvider(<Debugger code="" onSubmit={onSubmit} />);
+
+ fireEvent.click(screen.getByTestId("debugger-next-button"));
+ expect(onSubmit).toHaveBeenCalledWith("n");
+ });
+
+ it("should call onSubmit with 'c' when continue button is clicked", () => {
+ const onSubmit = vi.fn();
+ renderWithProvider(<Debugger code="" onSubmit={onSubmit} />);
+
+ fireEvent.click(screen.getByTestId("debugger-continue-button"));
+ expect(onSubmit).toHaveBeenCalledWith("c");
+ });
+
+ it("should call onSubmit with 'bt' when stack button is clicked", () => {
+ const onSubmit = vi.fn();
+ renderWithProvider(<Debugger code="" onSubmit={onSubmit} />);
+
+ fireEvent.click(screen.getByTestId("debugger-stack-button"));
+ expect(onSubmit).toHaveBeenCalledWith("bt");
+ });
+
+ it("should call onSubmit with 'help' when help button is clicked", () => {
+ const onSubmit = vi.fn();
+ renderWithProvider(<Debugger code="" onSubmit={onSubmit} />);
+
+ fireEvent.click(screen.getByTestId("debugger-help-button"));
+ expect(onSubmit).toHaveBeenCalledWith("help");
+ });
+});
+
+describe("Debugger command history integration", () => {
+ it("should initialize useInputHistory hook with correct props", () => {
+ const onSubmit = vi.fn();
+ renderWithProvider(<Debugger code="" onSubmit={onSubmit} />);
+
+ expect(useInputHistory).toHaveBeenCalled();
+ const calls = (useInputHistory as Mock).mock.calls;
+ expect(calls.length).toBeGreaterThan(0);
+
+ // Verify hook is called with value and setValue
+ const lastCall = calls[calls.length - 1][0];
+ expect(lastCall).toHaveProperty("value");
+ expect(lastCall).toHaveProperty("setValue");
+ expect(typeof lastCall.setValue).toBe("function");
+ });
+
+ it("should call navigateUp when ArrowUp is pressed in input area", () => {
+ const onSubmit = vi.fn();
+ const { container } = renderWithProvider(
+ <Debugger code="" onSubmit={onSubmit} />,
+ );
+
+ // Find the debugger input wrapper div (parent of CodeMirror)
+ const inputWrapper =
+ container.querySelector(".debugger-input")?.parentElement;
+ expect(inputWrapper).toBeTruthy();
+
+ fireEvent.keyDown(inputWrapper!, { key: "ArrowUp" });
+
+ expect(mockNavigateUp).toHaveBeenCalled();
+ });
+
+ it("should call navigateDown when ArrowDown is pressed in input area", () => {
+ const onSubmit = vi.fn();
+ const { container } = renderWithProvider(
+ <Debugger code="" onSubmit={onSubmit} />,
+ );
+
+ // Find the debugger input wrapper div (parent of CodeMirror)
+ const inputWrapper =
+ container.querySelector(".debugger-input")?.parentElement;
+ expect(inputWrapper).toBeTruthy();
+
+ fireEvent.keyDown(inputWrapper!, { key: "ArrowDown" });
+
+ expect(mockNavigateDown).toHaveBeenCalled();
+ });
+
+ it("should not call navigation functions for other keys", () => {
+ const onSubmit = vi.fn();
+ const { container } = renderWithProvider(
+ <Debugger code="" onSubmit={onSubmit} />,
+ );
+
+ const inputWrapper =
+ container.querySelector(".debugger-input")?.parentElement;
+ expect(inputWrapper).toBeTruthy();
+
+ fireEvent.keyDown(inputWrapper!, { key: "ArrowLeft" });
+ fireEvent.keyDown(inputWrapper!, { key: "ArrowRight" });
+ fireEvent.keyDown(inputWrapper!, { key: "a" });
+
+ expect(mockNavigateUp).not.toHaveBeenCalled();
+ expect(mockNavigateDown).not.toHaveBeenCalled();
+ });
+});
diff --git a/frontend/src/components/debugger/debugger-code.tsx b/frontend/src/components/debugger/debugger-code.tsx
index ff75d8cc7a..53f8b0a1b9 100644
--- a/frontend/src/components/debugger/debugger-code.tsx
+++ b/frontend/src/components/debugger/debugger-code.tsx
@@ -19,7 +19,7 @@ import { Button } from "../ui/button";
import { Tooltip } from "../ui/tooltip";
import "./debugger-code.css";
import { useKeydownOnElement } from "@/hooks/useHotkey";
-import { Functions } from "@/utils/functions";
+import { useInputHistory } from "@/hooks/useInputHistory";
interface Props {
code: string;
@@ -79,10 +79,15 @@ const DebuggerInput: React.FC<{
const [value, setValue] = React.useState("");
const ref = React.useRef<HTMLDivElement>(null);
- // Capture some events to prevent default behavior
+ const { navigateUp, navigateDown, addToHistory } = useInputHistory({
+ value,
+ setValue,
+ });
+
+ // Capture some events for command history navigation
useKeydownOnElement(ref, {
- ArrowUp: Functions.NOOP,
- ArrowDown: Functions.NOOP,
+ ArrowUp: navigateUp,
+ ArrowDown: navigateDown,
});
return (
@@ -111,6 +116,7 @@ const DebuggerInput: React.FC<{
if (!v) {
return true;
}
+ addToHistory(v);
onSubmit(v);
setValue("");
return true;
diff --git a/frontend/src/components/editor/output/console/ConsoleOutput.tsx b/frontend/src/components/editor/output/console/ConsoleOutput.tsx
index 6dfb589a32..6da1bd788e 100644
--- a/frontend/src/components/editor/output/console/ConsoleOutput.tsx
+++ b/frontend/src/components/editor/output/console/ConsoleOutput.tsx
@@ -11,6 +11,7 @@ import type { CellId } from "@/core/cells/ids";
import { isInternalCellName } from "@/core/cells/names";
import type { WithResponse } from "@/core/cells/types";
import type { OutputMessage } from "@/core/kernel/messages";
+import { useInputHistory } from "@/hooks/useInputHistory";
import { useSelectAllContent } from "@/hooks/useSelectAllContent";
import { cn } from "@/utils/cn";
import { invariant } from "@/utils/invariant";
@@ -216,6 +217,13 @@ const StdInput = (props: {
response?: string;
isPdb: boolean;
}) => {
+ const [value, setValue] = React.useState("");
+
+ const { navigateUp, navigateDown, addToHistory } = useInputHistory({
+ value,
+ setValue,
+ });
+
return (
<div className="flex gap-2 items-center pt-2">
{renderText(props.output)}
@@ -226,13 +234,31 @@ const StdInput = (props: {
type="text"
autoComplete="off"
autoFocus={true}
+ value={value}
+ onChange={(e) => setValue(e.target.value)}
icon={<ChevronRightIcon className="w-5 h-5" />}
className="m-0 h-8 focus-visible:shadow-xs-solid"
placeholder="stdin"
- // Capture the keydown event to prevent default behavior
+ // Capture the keydown event for history navigation and submission
onKeyDownCapture={(e) => {
+ if (e.key === "ArrowUp") {
+ navigateUp();
+ e.preventDefault();
+ return;
+ }
+
+ if (e.key === "ArrowDown") {
+ navigateDown();
+ e.preventDefault();
+ return;
+ }
+
if (e.key === "Enter" && !e.shiftKey) {
- props.onSubmit(e.currentTarget.value);
+ if (value) {
+ addToHistory(value);
+ props.onSubmit(value);
+ setValue("");
+ }
e.preventDefault();
e.stopPropagation();
}
diff --git a/frontend/src/hooks/__tests__/useInputHistory.test.tsx b/frontend/src/hooks/__tests__/useInputHistory.test.tsx
new file mode 100644
index 0000000000..db3740f097
--- /dev/null
+++ b/frontend/src/hooks/__tests__/useInputHistory.test.tsx
@@ -0,0 +1,225 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { act, renderHook } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+import { useInputHistory } from "../useInputHistory";
+
+describe("useInputHistory", () => {
+ it("should initialize with empty history", () => {
+ const setValue = vi.fn();
+ const { result } = renderHook(() =>
+ useInputHistory({ value: "", setValue }),
+ );
+ expect(result.current.history).toEqual([]);
+ });
+
+ it("should add command to history", () => {
+ const setValue = vi.fn();
+ const { result } = renderHook(() =>
+ useInputHistory({ value: "", setValue }),
+ );
+
+ act(() => {
+ result.current.addToHistory("test command");
+ });
+
+ expect(result.current.history).toEqual(["test command"]);
+ });
+
+ it("should not add duplicate consecutive commands", () => {
+ const setValue = vi.fn();
+ const { result } = renderHook(() =>
+ useInputHistory({ value: "", setValue }),
+ );
+
+ act(() => {
+ result.current.addToHistory("test");
+ result.current.addToHistory("test");
+ result.current.addToHistory("test");
+ });
+
+ expect(result.current.history).toEqual(["test"]);
+ });
+
+ it("should allow same command after different command", () => {
+ const setValue = vi.fn();
+ const { result } = renderHook(() =>
+ useInputHistory({ value: "", setValue }),
+ );
+
+ act(() => {
+ result.current.addToHistory("first");
+ result.current.addToHistory("second");
+ result.current.addToHistory("first");
+ });
+
+ expect(result.current.history).toEqual(["first", "second", "first"]);
+ });
+
+ it("should navigate up through history", () => {
+ const setValue = vi.fn();
+ const { result, rerender } = renderHook(
+ ({ value }) => useInputHistory({ value, setValue }),
+ { initialProps: { value: "" } },
+ );
+
+ act(() => {
+ result.current.addToHistory("first");
+ result.current.addToHistory("second");
+ result.current.addToHistory("third");
+ });
+
+ act(() => {
+ result.current.navigateUp();
+ });
+
+ expect(setValue).toHaveBeenLastCalledWith("third");
+
+ // Update the value prop to simulate state change
+ rerender({ value: "third" });
+
+ act(() => {
+ result.current.navigateUp();
+ });
+
+ expect(setValue).toHaveBeenLastCalledWith("second");
+ });
+
+ it("should navigate down through history", () => {
+ const setValue = vi.fn();
+ const { result, rerender } = renderHook(
+ ({ value }) => useInputHistory({ value, setValue }),
+ { initialProps: { value: "" } },
+ );
+
+ act(() => {
+ result.current.addToHistory("first");
+ result.current.addToHistory("second");
+ });
+
+ // Navigate up twice
+ act(() => {
+ result.current.navigateUp();
+ });
+ rerender({ value: "second" });
+
+ act(() => {
+ result.current.navigateUp();
+ });
+ rerender({ value: "first" });
+
+ // Now navigate down
+ act(() => {
+ result.current.navigateDown();
+ });
+
+ expect(setValue).toHaveBeenLastCalledWith("second");
+ });
+
+ it("should preserve pending input when navigating", () => {
+ const setValue = vi.fn();
+ const { result } = renderHook(
+ ({ value }) => useInputHistory({ value, setValue }),
+ { initialProps: { value: "pending input" } },
+ );
+
+ act(() => {
+ result.current.addToHistory("previous command");
+ });
+
+ // Navigate up
+ act(() => {
+ result.current.navigateUp();
+ });
+
+ expect(setValue).toHaveBeenLastCalledWith("previous command");
+
+ // Navigate down should restore pending input
+ act(() => {
+ result.current.navigateDown();
+ });
+
+ expect(setValue).toHaveBeenLastCalledWith("pending input");
+ });
+
+ it("should not navigate when history is empty", () => {
+ const setValue = vi.fn();
+ const { result } = renderHook(() =>
+ useInputHistory({ value: "current", setValue }),
+ );
+
+ act(() => {
+ result.current.navigateUp();
+ });
+
+ expect(setValue).not.toHaveBeenCalled();
+
+ act(() => {
+ result.current.navigateDown();
+ });
+
+ expect(setValue).not.toHaveBeenCalled();
+ });
+
+ it("should not navigate past the oldest command", () => {
+ const setValue = vi.fn();
+ const { result, rerender } = renderHook(
+ ({ value }) => useInputHistory({ value, setValue }),
+ { initialProps: { value: "" } },
+ );
+
+ act(() => {
+ result.current.addToHistory("only command");
+ });
+
+ act(() => {
+ result.current.navigateUp();
+ });
+
+ expect(setValue).toHaveBeenCalledTimes(1);
+ expect(setValue).toHaveBeenLastCalledWith("only command");
+
+ rerender({ value: "only command" });
+
+ // Try to navigate up again - should not change
+ act(() => {
+ result.current.navigateUp();
+ });
+
+ // setValue should not be called again
+ expect(setValue).toHaveBeenCalledTimes(1);
+ });
+
+ it("should reset navigation state after adding to history", () => {
+ const setValue = vi.fn();
+ const { result, rerender } = renderHook(
+ ({ value }) => useInputHistory({ value, setValue }),
+ { initialProps: { value: "" } },
+ );
+
+ act(() => {
+ result.current.addToHistory("first");
+ result.current.addToHistory("second");
+ });
+
+ // Navigate up
+ act(() => {
+ result.current.navigateUp();
+ });
+
+ expect(setValue).toHaveBeenLastCalledWith("second");
+ rerender({ value: "second" });
+
+ // Add a new command
+ act(() => {
+ result.current.addToHistory("third");
+ });
+
+ // Navigate up should now show 'third'
+ act(() => {
+ result.current.navigateUp();
+ });
+
+ expect(setValue).toHaveBeenLastCalledWith("third");
+ });
+});
diff --git a/frontend/src/hooks/useInputHistory.ts b/frontend/src/hooks/useInputHistory.ts
new file mode 100644
index 0000000000..c9feeb08b1
--- /dev/null
+++ b/frontend/src/hooks/useInputHistory.ts
@@ -0,0 +1,103 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+import * as React from "react";
+
+interface UseInputHistoryOptions {
+ /** Current input value */
+ value: string;
+ /** Callback to update the input value */
+ setValue: (value: string) => void;
+}
+
+interface UseInputHistoryReturn {
+ /** Command history array */
+ history: string[];
+ /** Navigate to previous command (ArrowUp) */
+ navigateUp: () => void;
+ /** Navigate to next command (ArrowDown) */
+ navigateDown: () => void;
+ /** Add a command to history and reset navigation state */
+ addToHistory: (command: string) => void;
+}
+
+/**
+ * Hook for managing input command history with up/down arrow navigation.
+ *
+ * Features:
+ * - ArrowUp navigates to previous commands
+ * - ArrowDown navigates forward in history
+ * - Preserves current input when starting to navigate
+ * - Avoids adding duplicate consecutive commands
+ */
+export function useInputHistory({
+ value,
+ setValue,
+}: UseInputHistoryOptions): UseInputHistoryReturn {
+ // Command history state - using useState for reactivity
+ const [history, setHistory] = React.useState<string[]>([]);
+ const historyIndexRef = React.useRef<number>(-1);
+ // Store the current input when navigating history
+ const pendingInputRef = React.useRef<string>("");
+ // Keep a ref to history for use in callbacks without causing re-renders
+ const historyRef = React.useRef(history);
+ historyRef.current = history;
+
+ const navigateUp = React.useCallback(() => {
+ const currentHistory = historyRef.current;
+ if (currentHistory.length === 0) {
+ return;
+ }
+
+ const currentIndex = historyIndexRef.current;
+
+ // Save current input if we're starting to navigate
+ if (currentIndex === -1) {
+ pendingInputRef.current = value;
+ }
+ // Navigate to previous command
+ const newIndex = Math.min(currentIndex + 1, currentHistory.length - 1);
+ if (newIndex !== currentIndex) {
+ historyIndexRef.current = newIndex;
+ setValue(currentHistory[currentHistory.length - 1 - newIndex]);
+ }
+ }, [value, setValue]);
+
+ const navigateDown = React.useCallback(() => {
+ const currentHistory = historyRef.current;
+ if (currentHistory.length === 0) {
+ return;
+ }
+
+ const currentIndex = historyIndexRef.current;
+
+ // Navigate to next command
+ if (currentIndex > 0) {
+ const newIndex = currentIndex - 1;
+ historyIndexRef.current = newIndex;
+ setValue(currentHistory[currentHistory.length - 1 - newIndex]);
+ } else if (currentIndex === 0) {
+ // Return to pending input
+ historyIndexRef.current = -1;
+ setValue(pendingInputRef.current);
+ }
+ }, [setValue]);
+
+ const addToHistory = React.useCallback((command: string) => {
+ setHistory((prev) => {
+ // Add to history if it's not a duplicate of the last command
+ if (prev.length === 0 || prev[prev.length - 1] !== command) {
+ return [...prev, command];
+ }
+ return prev;
+ });
+ // Reset history navigation state
+ historyIndexRef.current = -1;
+ pendingInputRef.current = "";
+ }, []);
+
+ return {
+ history,
+ navigateUp,
+ navigateDown,
+ addToHistory,
+ };
+}