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.

improvement: detect and warn about duplicate shortcuts (#7412)

Myles Scolnick2ed29941d2de
diff --git a/frontend/src/components/editor/controls/duplicate-shortcut-banner.tsx b/frontend/src/components/editor/controls/duplicate-shortcut-banner.tsx
new file mode 100644
index 0000000000..b76ce1a683
--- /dev/null
+++ b/frontend/src/components/editor/controls/duplicate-shortcut-banner.tsx
@@ -0,0 +1,50 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { AlertTriangleIcon } from "lucide-react";
+import { KeyboardHotkeys } from "@/components/shortcuts/renderShortcut";
+import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
+import type { DuplicateGroup } from "@/hooks/useDuplicateShortcuts";
+
+interface DuplicateShortcutBannerProps {
+ duplicates: DuplicateGroup[];
+}
+
+/**
+ * Banner component that warns about duplicate keyboard shortcuts.
+ * Displays a warning when multiple actions share the same key binding.
+ */
+export const DuplicateShortcutBanner: React.FC<
+ DuplicateShortcutBannerProps
+> = ({ duplicates }) => {
+ // Don't render if no duplicates
+ if (duplicates.length === 0) {
+ return null;
+ }
+
+ return (
+ <Alert variant="warning" className="mb-4">
+ <AlertTriangleIcon className="h-4 w-4" />
+ <AlertTitle>Duplicate shortcuts</AlertTitle>
+ <AlertDescription>
+ <p className="mb-2">
+ Multiple actions are assigned to the same keyboard shortcut:
+ </p>
+ <ul className="space-y-2">
+ {duplicates.map(({ key, actions }) => (
+ <li key={key} className="text-xs">
+ <div className="flex items-center gap-2 mb-1">
+ <KeyboardHotkeys shortcut={key} />
+ <span className="font-semibold">is used by:</span>
+ </div>
+ <ul className="ml-6 list-disc">
+ {actions.map(({ action, name }) => (
+ <li key={action}>{name}</li>
+ ))}
+ </ul>
+ </li>
+ ))}
+ </ul>
+ </AlertDescription>
+ </Alert>
+ );
+};
diff --git a/frontend/src/components/editor/controls/keyboard-shortcuts.tsx b/frontend/src/components/editor/controls/keyboard-shortcuts.tsx
index c6d9beb928..b07abc5364 100644
--- a/frontend/src/components/editor/controls/keyboard-shortcuts.tsx
+++ b/frontend/src/components/editor/controls/keyboard-shortcuts.tsx
@@ -1,7 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { atom, useAtom, useAtomValue } from "jotai";
-import { EditIcon, XIcon } from "lucide-react";
+import { AlertTriangleIcon, EditIcon, XIcon } from "lucide-react";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
@@ -15,6 +15,7 @@ import {
} from "@/core/hotkeys/hotkeys";
import { isPlatformMac } from "@/core/hotkeys/shortcuts";
import { useRequestClient } from "@/core/network/requests";
+import { useDuplicateShortcuts } from "../../../hooks/useDuplicateShortcuts";
import { useHotkey } from "../../../hooks/useHotkey";
import { KeyboardHotkeys } from "../../shortcuts/renderShortcut";
import {
@@ -25,6 +26,7 @@ import {
DialogPortal,
DialogTitle,
} from "../../ui/dialog";
+import { DuplicateShortcutBanner } from "./duplicate-shortcut-banner";
export const keyboardShortcutsAtom = atom(false);
@@ -37,6 +39,10 @@ export const KeyboardShortcuts: React.FC = () => {
const [config, setConfig] = useResolvedMarimoConfig();
const hotkeys = useAtomValue(hotkeysAtom);
const { saveUserConfig } = useRequestClient();
+ const { duplicates, hasDuplicate, getDuplicatesFor } = useDuplicateShortcuts(
+ hotkeys,
+ "Markdown",
+ );
useHotkey("global.showHelp", () => setIsOpen((v) => !v));
@@ -214,6 +220,9 @@ export const KeyboardShortcuts: React.FC = () => {
);
}
+ const isDuplicate = hasDuplicate(action);
+ const duplicateActions = isDuplicate ? getDuplicatesFor(action) : [];
+
return (
<div
key={action}
@@ -231,7 +240,20 @@ export const KeyboardShortcuts: React.FC = () => {
<div className="w-3 h-3" />
)}
<KeyboardHotkeys className="justify-end" shortcut={hotkey.key} />
- <span>{hotkey.name.toLowerCase()}</span>
+ <div className="flex items-center gap-1">
+ <span>{hotkey.name.toLowerCase()}</span>
+ {isDuplicate && (
+ <div className="group relative inline-flex">
+ <AlertTriangleIcon className="w-3 h-3 text-(--yellow-11)" />
+ <div className="invisible group-hover:visible absolute left-0 top-5 z-10 w-max max-w-xs rounded-md bg-(--yellow-2) border border-(--yellow-7) p-2 text-xs text-(--yellow-11) shadow-md">
+ Also used by:{" "}
+ {duplicateActions
+ .map((a) => hotkeys.getHotkey(a).name.toLowerCase())
+ .join(", ")}
+ </div>
+ </div>
+ )}
+ </div>
</div>
);
};
@@ -279,6 +301,7 @@ export const KeyboardShortcuts: React.FC = () => {
<DialogHeader>
<DialogTitle>Shortcuts</DialogTitle>
</DialogHeader>
+ <DuplicateShortcutBanner duplicates={duplicates} />
<div className="flex flex-row gap-3">
<div className="w-1/2">
{renderGroup("Editing")}
diff --git a/frontend/src/components/editor/output/__tests__/ansi-reduce.test.ts b/frontend/src/components/editor/output/__tests__/ansi-reduce.test.ts
index 18565ef142..e1f0695d44 100644
--- a/frontend/src/components/editor/output/__tests__/ansi-reduce.test.ts
+++ b/frontend/src/components/editor/output/__tests__/ansi-reduce.test.ts
@@ -534,13 +534,13 @@ describe("AnsiReducer streaming with append()", () => {
describe("AnsiReducer color preservation", () => {
const CASES = [
// SGR sequences
- "\u001b[34mBlue text\u001b[m normal text\u001b[31mRed text\u001b[0m",
+ "\u001B[34mBlue text\u001B[m normal text\u001B[31mRed text\u001B[0m",
// Complex SGR with parameters
- "\u001b[1;31mBold Red\u001b[0m \u001b[48;5;240mGray bg\u001b[0m",
+ "\u001B[1;31mBold Red\u001B[0m \u001B[48;5;240mGray bg\u001B[0m",
// Character set selection
- "Text\u001b(BMore\u001b(0Graphics\u001b(B",
+ "Text\u001B(BMore\u001B(0Graphics\u001B(B",
// Complex case
- "\u001b[34m[D 251201 15:32:24 cell_runner:695]\u001b(B\u001b[m Running post_execution hooks in context\n\u001b[34m[D 251201 15:32:24 hooks_post_execution:65]\u001b(B\u001b[m Acquiring graph lock to update cell import workspace\n\u001b[34m[D 251201 15:32:24 hooks_post_execution:67]\u001b(B\u001b[m Acquired graph lock to update import workspace.\n",
+ "\u001B[34m[D 251201 15:32:24 cell_runner:695]\u001B(B\u001B[m Running post_execution hooks in context\n\u001B[34m[D 251201 15:32:24 hooks_post_execution:65]\u001B(B\u001B[m Acquiring graph lock to update cell import workspace\n\u001B[34m[D 251201 15:32:24 hooks_post_execution:67]\u001B(B\u001B[m Acquired graph lock to update import workspace.\n",
];
test.each(CASES)("preserves ANSI color codes", (input) => {
@@ -554,12 +554,12 @@ describe("AnsiReducer color preservation", () => {
// Test that color codes work alongside cursor movements
// Note: when cursor moves up, lines below are discarded (tqdm behavior)
const result = reducer.reduce(
- "Line1\n\u001b[31mRed\u001b[0m\u001b[1A\u001b[32mGreen\u001b[0m",
+ "Line1\n\u001B[31mRed\u001B[0m\u001B[1A\u001B[32mGreen\u001B[0m",
);
// After moving up from row 1 to row 0, row 1 is discarded
// Green is written at the end of row 0
expect(result).toMatchInlineSnapshot(
- `"Line1 \u001b[32mGreen\u001b[0m"`,
+ `"Line1 \u001B[32mGreen\u001B[0m"`,
);
});
});
diff --git a/frontend/src/core/state/__mocks__/mocks.ts b/frontend/src/core/state/__mocks__/mocks.ts
index 43c8b8f223..b209c4bf86 100644
--- a/frontend/src/core/state/__mocks__/mocks.ts
+++ b/frontend/src/core/state/__mocks__/mocks.ts
@@ -1,3 +1,4 @@
+/* Copyright 2024 Marimo. All rights reserved. */
import type { Observable } from "../observable";
export function createMockObservable<T>(
diff --git a/frontend/src/hooks/__tests__/useDuplicateShortcuts.test.ts b/frontend/src/hooks/__tests__/useDuplicateShortcuts.test.ts
new file mode 100644
index 0000000000..07f5892b53
--- /dev/null
+++ b/frontend/src/hooks/__tests__/useDuplicateShortcuts.test.ts
@@ -0,0 +1,449 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import { describe, expect, it } from "vitest";
+import {
+ type Hotkey,
+ type HotkeyAction,
+ HotkeyProvider,
+} from "@/core/hotkeys/hotkeys";
+import {
+ findDuplicateShortcuts,
+ normalizeShortcutKey,
+} from "../useDuplicateShortcuts";
+
+/**
+ * Helper to create a minimal hotkey configuration for testing.
+ */
+function createHotkeys(
+ keys: Partial<Record<HotkeyAction, Hotkey>>,
+): Record<HotkeyAction, Hotkey> {
+ return new Proxy(keys as Record<HotkeyAction, Hotkey>, {
+ // biome-ignore lint: ok to have three arguments here (It's a web API)
+ get(target, p, receiver) {
+ const key = Reflect.get(target, p, receiver);
+ if (key === "undefined") {
+ throw new Error("Missing required hotkey.");
+ }
+ return key;
+ },
+ });
+}
+
+describe("normalizeShortcutKey", () => {
+ it("should convert to lowercase", () => {
+ expect(normalizeShortcutKey("Ctrl-Shift-A")).toBe("ctrl-shift-a");
+ expect(normalizeShortcutKey("MOD-ENTER")).toBe("mod-enter");
+ });
+
+ it("should replace + with -", () => {
+ expect(normalizeShortcutKey("Ctrl+Shift+A")).toBe("ctrl-shift-a");
+ expect(normalizeShortcutKey("Cmd+Enter")).toBe("cmd-enter");
+ });
+
+ it("should trim whitespace", () => {
+ expect(normalizeShortcutKey(" Ctrl-A ")).toBe("ctrl-a");
+ expect(normalizeShortcutKey(" Mod-Enter ")).toBe("mod-enter");
+ });
+
+ it("should handle mixed separators", () => {
+ expect(normalizeShortcutKey("Ctrl+Shift-A")).toBe("ctrl-shift-a");
+ expect(normalizeShortcutKey("Mod-Alt+K")).toBe("mod-alt-k");
+ });
+});
+
+describe("findDuplicateShortcuts", () => {
+ it("should detect no duplicates when all shortcuts are unique", () => {
+ const hotkeys = createHotkeys({
+ "cell.run": {
+ name: "Run cell",
+ group: "Running Cells",
+ key: "Mod-Enter",
+ },
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: "Mod-b",
+ },
+ "cell.delete": {
+ name: "Delete cell",
+ group: "Editing",
+ key: "Shift-Backspace",
+ },
+ });
+
+ const provider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ const result = findDuplicateShortcuts(provider);
+
+ expect(result.duplicates).toHaveLength(0);
+ expect(result.hasDuplicate("cell.run")).toBe(false);
+ expect(result.hasDuplicate("cell.format")).toBe(false);
+ expect(result.hasDuplicate("cell.delete")).toBe(false);
+ });
+
+ it("should detect duplicates when two actions share the same key", () => {
+ const hotkeys = createHotkeys({
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: "Mod-b",
+ },
+ "markdown.bold": {
+ name: "Bold",
+ group: "Markdown",
+ key: "Mod-b",
+ },
+ "cell.run": {
+ name: "Run cell",
+ group: "Running Cells",
+ key: "Mod-Enter",
+ },
+ });
+
+ const provider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ const result = findDuplicateShortcuts(provider);
+
+ expect(result.duplicates).toHaveLength(1);
+ expect(result.duplicates[0].key).toBe("cmd-b");
+ expect(result.duplicates[0].actions).toHaveLength(2);
+
+ expect(result.hasDuplicate("cell.format")).toBe(true);
+ expect(result.hasDuplicate("markdown.bold")).toBe(true);
+ expect(result.hasDuplicate("cell.run")).toBe(false);
+ });
+
+ it("should detect multiple duplicate groups", () => {
+ const hotkeys = createHotkeys({
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: "Mod-b",
+ },
+ "markdown.bold": {
+ name: "Bold",
+ group: "Markdown",
+ key: "Mod-b",
+ },
+ "cell.run": {
+ name: "Run cell",
+ group: "Running Cells",
+ key: "Mod-Enter",
+ },
+ "cell.complete": {
+ name: "Code completion",
+ group: "Editing",
+ key: "Ctrl-Space",
+ },
+ "cell.signatureHelp": {
+ name: "Signature help",
+ group: "Editing",
+ key: "Mod-Enter",
+ },
+ });
+
+ const provider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ const result = findDuplicateShortcuts(provider);
+
+ expect(result.duplicates).toHaveLength(2);
+
+ // Check that both duplicate groups are detected
+ const duplicateKeys = result.duplicates.map((d) => d.key).sort();
+ expect(duplicateKeys).toEqual(["cmd-b", "cmd-enter"]);
+
+ expect(result.hasDuplicate("cell.format")).toBe(true);
+ expect(result.hasDuplicate("markdown.bold")).toBe(true);
+ expect(result.hasDuplicate("cell.run")).toBe(true);
+ expect(result.hasDuplicate("cell.signatureHelp")).toBe(true);
+ expect(result.hasDuplicate("cell.complete")).toBe(false);
+ });
+
+ it("should handle three or more actions with the same key", () => {
+ const hotkeys = createHotkeys({
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: "Mod-k",
+ },
+ "markdown.link": {
+ name: "Convert to Link",
+ group: "Markdown",
+ key: "Mod-k",
+ },
+ "global.commandPalette": {
+ name: "Show command palette",
+ group: "Other",
+ key: "Mod-k",
+ },
+ });
+
+ const provider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ const result = findDuplicateShortcuts(provider);
+
+ expect(result.duplicates).toHaveLength(1);
+ expect(result.duplicates[0].actions).toHaveLength(3);
+ expect(result.duplicates[0].key).toBe("cmd-k");
+ });
+
+ it("should ignore empty or unset shortcuts", () => {
+ const hotkeys = createHotkeys({
+ "cell.run": {
+ name: "Run cell",
+ group: "Running Cells",
+ key: "Mod-Enter",
+ },
+ "global.runAll": {
+ name: "Re-run all cells",
+ group: "Running Cells",
+ key: "",
+ },
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: "Mod-b",
+ },
+ });
+
+ const provider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ const result = findDuplicateShortcuts(provider);
+
+ expect(result.duplicates).toHaveLength(0);
+ expect(result.hasDuplicate("global.runAll")).toBe(false);
+ });
+
+ it("should normalize keys for comparison (case and separator insensitive)", () => {
+ const hotkeys = createHotkeys({
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: "Cmd-B",
+ },
+ "markdown.bold": {
+ name: "Bold",
+ group: "Markdown",
+ key: "cmd+b",
+ },
+ });
+
+ const provider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ const result = findDuplicateShortcuts(provider);
+
+ expect(result.duplicates).toHaveLength(1);
+ expect(result.duplicates[0].actions).toHaveLength(2);
+ });
+
+ it("getDuplicatesFor should return other actions with the same key", () => {
+ const hotkeys = createHotkeys({
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: "Mod-b",
+ },
+ "markdown.bold": {
+ name: "Bold",
+ group: "Markdown",
+ key: "Mod-b",
+ },
+ "markdown.italic": {
+ name: "Italic",
+ group: "Markdown",
+ key: "Mod-i",
+ },
+ });
+
+ const provider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ const result = findDuplicateShortcuts(provider);
+
+ const duplicatesForFormat = result.getDuplicatesFor("cell.format");
+ expect(duplicatesForFormat).toEqual(["markdown.bold"]);
+
+ const duplicatesForBold = result.getDuplicatesFor("markdown.bold");
+ expect(duplicatesForBold).toEqual(["cell.format"]);
+
+ const duplicatesForItalic = result.getDuplicatesFor("markdown.italic");
+ expect(duplicatesForItalic).toEqual([]);
+ });
+
+ it("getDuplicatesFor should return all other actions when three or more share a key", () => {
+ const hotkeys = createHotkeys({
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: "Mod-k",
+ },
+ "markdown.link": {
+ name: "Convert to Link",
+ group: "Markdown",
+ key: "Mod-k",
+ },
+ "global.commandPalette": {
+ name: "Show command palette",
+ group: "Other",
+ key: "Mod-k",
+ },
+ });
+
+ const provider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ const result = findDuplicateShortcuts(provider);
+
+ const duplicatesForFormat = result.getDuplicatesFor("cell.format");
+ expect(duplicatesForFormat).toHaveLength(2);
+ expect(duplicatesForFormat).toContain("markdown.link");
+ expect(duplicatesForFormat).toContain("global.commandPalette");
+ });
+
+ it("should respect platform-specific key overrides", () => {
+ const hotkeys = createHotkeys({
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: {
+ main: "Mod-Shift-F",
+ mac: "Cmd-Option-F",
+ windows: "Ctrl-Alt-F",
+ },
+ },
+ "markdown.bold": {
+ name: "Bold",
+ group: "Markdown",
+ key: "Cmd-Option-F", // Duplicate on Mac only
+ },
+ });
+
+ const macProvider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ const macResult = findDuplicateShortcuts(macProvider);
+ expect(macResult.duplicates).toHaveLength(1);
+ expect(macResult.hasDuplicate("cell.format")).toBe(true);
+ expect(macResult.hasDuplicate("markdown.bold")).toBe(true);
+
+ const windowsProvider = new HotkeyProvider(hotkeys, {
+ platform: "windows",
+ });
+ const windowsResult = findDuplicateShortcuts(windowsProvider);
+ expect(windowsResult.duplicates).toHaveLength(0);
+ expect(windowsResult.hasDuplicate("cell.format")).toBe(false);
+ expect(windowsResult.hasDuplicate("markdown.bold")).toBe(false);
+ });
+
+ describe("ignoreGroup parameter", () => {
+ it("should ignore duplicates from the specified group", () => {
+ const hotkeys = createHotkeys({
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: "Mod-b",
+ },
+ "markdown.bold": {
+ name: "Bold",
+ group: "Markdown",
+ key: "Mod-b",
+ },
+ "cell.run": {
+ name: "Run cell",
+ group: "Running Cells",
+ key: "Mod-Enter",
+ },
+ });
+
+ const provider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ const result = findDuplicateShortcuts(provider, "Markdown");
+
+ // markdown.bold should be ignored, so no duplicates should be found
+ expect(result.duplicates).toHaveLength(0);
+ expect(result.hasDuplicate("cell.format")).toBe(false);
+ expect(result.hasDuplicate("markdown.bold")).toBe(false);
+ });
+
+ it("should still detect duplicates outside the ignored group", () => {
+ const hotkeys = createHotkeys({
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: "Mod-b",
+ },
+ "markdown.bold": {
+ name: "Bold",
+ group: "Markdown",
+ key: "Mod-b",
+ },
+ "cell.run": {
+ name: "Run cell",
+ group: "Running Cells",
+ key: "Mod-Enter",
+ },
+ "cell.complete": {
+ name: "Code completion",
+ group: "Editing",
+ key: "Mod-Enter",
+ },
+ });
+
+ const provider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ const result = findDuplicateShortcuts(provider, "Markdown");
+
+ // markdown.bold is ignored, but cell.run and cell.complete should still be detected
+ expect(result.duplicates).toHaveLength(1);
+ expect(result.duplicates[0].key).toBe("cmd-enter");
+ expect(result.hasDuplicate("cell.run")).toBe(true);
+ expect(result.hasDuplicate("cell.complete")).toBe(true);
+ expect(result.hasDuplicate("markdown.bold")).toBe(false);
+ expect(result.hasDuplicate("cell.format")).toBe(false);
+ });
+
+ it("should handle ignoring a group that doesn't exist", () => {
+ const hotkeys = createHotkeys({
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: "Mod-b",
+ },
+ "markdown.bold": {
+ name: "Bold",
+ group: "Markdown",
+ key: "Mod-b",
+ },
+ });
+
+ const provider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ const result = findDuplicateShortcuts(provider, "NonExistent" as any);
+
+ // Should still work normally and detect the duplicate
+ expect(result.duplicates).toHaveLength(1);
+ expect(result.hasDuplicate("cell.format")).toBe(true);
+ expect(result.hasDuplicate("markdown.bold")).toBe(true);
+ });
+
+ it("should ignore multiple actions from the same group", () => {
+ const hotkeys = createHotkeys({
+ "cell.format": {
+ name: "Format cell",
+ group: "Editing",
+ key: "Mod-b",
+ },
+ "markdown.bold": {
+ name: "Bold",
+ group: "Markdown",
+ key: "Mod-b",
+ },
+ "markdown.italic": {
+ name: "Italic",
+ group: "Markdown",
+ key: "Mod-i",
+ },
+ "cell.hideCode": {
+ name: "Hide cell code",
+ group: "Editing",
+ key: "Mod-i",
+ },
+ });
+
+ const provider = new HotkeyProvider(hotkeys, { platform: "mac" });
+ const result = findDuplicateShortcuts(provider, "Markdown");
+
+ // Both markdown actions should be ignored
+ expect(result.duplicates).toHaveLength(0);
+ expect(result.hasDuplicate("cell.format")).toBe(false);
+ expect(result.hasDuplicate("cell.hideCode")).toBe(false);
+ expect(result.hasDuplicate("markdown.bold")).toBe(false);
+ expect(result.hasDuplicate("markdown.italic")).toBe(false);
+ });
+ });
+});
diff --git a/frontend/src/hooks/useDuplicateShortcuts.ts b/frontend/src/hooks/useDuplicateShortcuts.ts
new file mode 100644
index 0000000000..768fa5f7b7
--- /dev/null
+++ b/frontend/src/hooks/useDuplicateShortcuts.ts
@@ -0,0 +1,145 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { useMemo } from "react";
+import type {
+ HotkeyAction,
+ HotkeyGroup,
+ HotkeyProvider,
+} from "@/core/hotkeys/hotkeys";
+
+export interface DuplicateGroup {
+ key: string;
+ actions: { action: HotkeyAction; name: string }[];
+}
+
+export interface DuplicateShortcutsResult {
+ /** All groups of duplicate shortcuts */
+ duplicates: DuplicateGroup[];
+ /** Check if a specific action has duplicate shortcuts */
+ hasDuplicate: (action: HotkeyAction) => boolean;
+ /** Get all actions that share the same shortcut as the given action */
+ getDuplicatesFor: (action: HotkeyAction) => HotkeyAction[];
+}
+
+/**
+ * Normalizes a keyboard shortcut key for comparison.
+ * - Converts to lowercase
+ * - Replaces + with - for consistent comparison
+ * - Trims whitespace
+ */
+export function normalizeShortcutKey(key: string): string {
+ return key.toLowerCase().replaceAll("+", "-").trim();
+}
+
+/**
+ * Detects duplicate keyboard shortcuts in a hotkey provider.
+ * Returns information about which shortcuts are duplicated and provides utilities
+ * to check if specific actions have duplicates.
+ *
+ * This is a pure function that can be tested independently of React.
+ *
+ * @param hotkeys - The hotkey provider to check for duplicates
+ * @param ignoreGroup - Optional group to exclude from duplicate detection (e.g., "Markdown")
+ */
+export function findDuplicateShortcuts(
+ hotkeys: HotkeyProvider,
+ ignoreGroup?: HotkeyGroup,
+): DuplicateShortcutsResult {
+ // Get all groups to check for ignored actions
+ const groups = hotkeys.getHotkeyGroups();
+ const ignoredActions = ignoreGroup
+ ? new Set(groups[ignoreGroup] || [])
+ : new Set();
+
+ // Group actions by their key binding
+ const keyMap = new Map<string, { action: HotkeyAction; name: string }[]>();
+
+ for (const action of hotkeys.iterate()) {
+ // Skip actions in ignored groups
+ if (ignoredActions.has(action)) {
+ continue;
+ }
+
+ const hotkey = hotkeys.getHotkey(action);
+
+ // Skip empty keys (not set)
+ if (!hotkey.key || hotkey.key.trim() === "") {
+ continue;
+ }
+
+ const normalizedKey = normalizeShortcutKey(hotkey.key);
+
+ if (!keyMap.has(normalizedKey)) {
+ keyMap.set(normalizedKey, []);
+ }
+
+ const existing = keyMap.get(normalizedKey);
+ if (existing) {
+ existing.push({
+ action,
+ name: hotkey.name,
+ });
+ }
+ }
+
+ // Filter to only groups with duplicates (more than one action per key)
+ const duplicates: DuplicateGroup[] = [];
+ const duplicateActionSet = new Set<HotkeyAction>();
+
+ for (const [key, actions] of keyMap.entries()) {
+ if (actions.length > 1) {
+ duplicates.push({ key, actions });
+ for (const { action } of actions) {
+ duplicateActionSet.add(action);
+ }
+ }
+ }
+
+ // Helper to check if an action has duplicates
+ const hasDuplicate = (action: HotkeyAction): boolean => {
+ return duplicateActionSet.has(action);
+ };
+
+ // Helper to get all duplicates for a specific action
+ const getDuplicatesFor = (action: HotkeyAction): HotkeyAction[] => {
+ const hotkey = hotkeys.getHotkey(action);
+ if (!hotkey.key || hotkey.key.trim() === "") {
+ return [];
+ }
+
+ const normalizedKey = normalizeShortcutKey(hotkey.key);
+
+ const group = duplicates.find((d) => d.key === normalizedKey);
+ if (!group || group.actions.length <= 1) {
+ return [];
+ }
+
+ return group.actions
+ .filter((a) => a.action !== action)
+ .map((a) => a.action);
+ };
+
+ return {
+ duplicates,
+ hasDuplicate,
+ getDuplicatesFor,
+ };
+}
+
+/**
+ * Hook to detect duplicate keyboard shortcuts.
+ * Returns information about which shortcuts are duplicated and provides utilities
+ * to check if specific actions have duplicates.
+ *
+ * @param hotkeys - The hotkey provider to check for duplicates
+ * @param ignoreGroup - Optional group to exclude from duplicate detection (e.g., "Markdown")
+ */
+export function useDuplicateShortcuts(
+ hotkeys: HotkeyProvider,
+ ignoreGroup?: HotkeyGroup,
+): DuplicateShortcutsResult {
+ return useMemo(
+ () => findDuplicateShortcuts(hotkeys, ignoreGroup),
+ [hotkeys, ignoreGroup],
+ );
+}
diff --git a/frontend/src/plugins/impl/NumberPlugin.tsx b/frontend/src/plugins/impl/NumberPlugin.tsx
index b31bc339bd..964a486daa 100644
--- a/frontend/src/plugins/impl/NumberPlugin.tsx
+++ b/frontend/src/plugins/impl/NumberPlugin.tsx
@@ -79,7 +79,7 @@ const NumberComponent = (props: NumberComponentProps): JSX.Element => {
// This needs to be `?? NaN` since `?? undefined` makes uncontrolled component
// and can lead to leaving the old value in forms (https://github.com/marimo-team/marimo/issues/7352)
// We out NaNs later
- value={value ?? NaN}
+ value={value ?? Number.NaN}
step={props.step}
onChange={handleChange}
id={id}
diff --git a/frontend/src/plugins/impl/__tests__/NumberPlugin.test.tsx b/frontend/src/plugins/impl/__tests__/NumberPlugin.test.tsx
index f5a1ba4b45..c1c3080a56 100644
--- a/frontend/src/plugins/impl/__tests__/NumberPlugin.test.tsx
+++ b/frontend/src/plugins/impl/__tests__/NumberPlugin.test.tsx
@@ -113,7 +113,7 @@ describe("NumberPlugin", () => {
z.infer<(typeof plugin)["validator"]>
> = {
host,
- value: NaN,
+ value: Number.NaN,
setValue,
data: {
start: 0,
diff --git a/frontend/src/utils/__tests__/json-parser.test.ts b/frontend/src/utils/__tests__/json-parser.test.ts
index 87b4ae1e62..5b98162230 100644
--- a/frontend/src/utils/__tests__/json-parser.test.ts
+++ b/frontend/src/utils/__tests__/json-parser.test.ts
@@ -113,7 +113,7 @@ it("can convert json to tsv with fr-FR locale", () => {
const locale = "fr-FR";
// Handles floats with fr-FR locale (uses , as decimal separator)
- expect(jsonToTSV([{ a: 3.14, b: 2.12345 }], locale)).toEqual(
+ expect(jsonToTSV([{ a: 3.14, b: 2.123_45 }], locale)).toEqual(
"a\tb\n3,14\t2,12345",
);
});