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.

Correct command mode handling for vim vs non-vim modes (#6092)

Trevor Manz8145fd0b546d
diff --git a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
index 8b2affef1d..7b9421baff 100644
--- a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
+++ b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
@@ -12,7 +12,11 @@ import { MockRequestClient } from "@/__mocks__/requests";
import { aiCompletionCellAtom } from "@/core/ai/state";
import type { CellActions } from "@/core/cells/cells";
import { notebookAtom } from "@/core/cells/cells";
-import { configOverridesAtom, userConfigAtom } from "@/core/config/config";
+import {
+ configOverridesAtom,
+ platformAtom,
+ userConfigAtom,
+} from "@/core/config/config";
import { requestClientAtom } from "@/core/network/requests";
import { store } from "@/core/state/jotai";
import type { CellActionsDropdownHandle } from "../../cell/cell-actions";
@@ -1584,22 +1588,12 @@ describe("useCellNavigationProps", () => {
});
});
-// Mock isPlatformWindows for testing
-const mockIsPlatformWindows = vi.hoisted(() => vi.fn());
-vi.mock("@/core/hotkeys/shortcuts", async (importOriginal) => ({
- ...(await importOriginal()),
- isPlatformWindows: mockIsPlatformWindows,
-}));
-
describe("useCellEditorNavigationProps", () => {
beforeEach(() => {
vi.clearAllMocks();
// Reset config overrides
store.set(configOverridesAtom, {});
-
- // Reset platform mock to default (non-Windows)
- mockIsPlatformWindows.mockReturnValue(false);
});
describe("keyboard shortcuts", () => {
@@ -1705,19 +1699,18 @@ describe("useCellEditorNavigationProps", () => {
},
});
});
-
- describe("non-Windows platforms", () => {
+ describe("Mac", () => {
beforeEach(() => {
- mockIsPlatformWindows.mockReturnValue(false);
+ store.set(platformAtom, "mac");
});
- it("should focus cell when Ctrl+Escape is pressed in vim mode", () => {
+ it("should focus cell when Cmd+Escape is pressed in vim mode", () => {
const mockEditorView = { current: null };
const { result } = renderWithProvider(() =>
useCellEditorNavigationProps(mockCellId, mockEditorView),
);
- const mockEvent = Mocks.keyboardEvent({ key: "Escape", ctrlKey: true });
+ const mockEvent = Mocks.keyboardEvent({ key: "Escape", metaKey: true });
act(() => {
result.current.onKeyDown?.(mockEvent);
@@ -1726,30 +1719,20 @@ describe("useCellEditorNavigationProps", () => {
expect(focusCell).toHaveBeenCalledWith(mockCellId);
expect(mockEvent.continuePropagation).not.toHaveBeenCalled();
});
+ });
- it("should focus cell when Cmd+Escape (metaKey) is pressed in vim mode", () => {
- const mockEditorView = { current: null };
- const { result } = renderWithProvider(() =>
- useCellEditorNavigationProps(mockCellId, mockEditorView),
- );
-
- const mockEvent = Mocks.keyboardEvent({ key: "Escape", metaKey: true });
-
- act(() => {
- result.current.onKeyDown?.(mockEvent);
- });
-
- expect(focusCell).toHaveBeenCalledWith(mockCellId);
- expect(mockEvent.continuePropagation).not.toHaveBeenCalled();
+ describe("Linux", () => {
+ beforeEach(() => {
+ store.set(platformAtom, "linux");
});
- it("should focus cell when Escape (without Ctrl) is pressed in vim mode", () => {
+ it("should focus cell when Ctrl+Escape is pressed in vim mode", () => {
const mockEditorView = { current: null };
const { result } = renderWithProvider(() =>
useCellEditorNavigationProps(mockCellId, mockEditorView),
);
- const mockEvent = Mocks.keyboardEvent({ key: "Escape" });
+ const mockEvent = Mocks.keyboardEvent({ key: "Escape", ctrlKey: true });
act(() => {
result.current.onKeyDown?.(mockEvent);
@@ -1760,9 +1743,9 @@ describe("useCellEditorNavigationProps", () => {
});
});
- describe("Windows platform", () => {
+ describe("Windows", () => {
beforeEach(() => {
- mockIsPlatformWindows.mockReturnValue(true);
+ store.set(platformAtom, "windows");
});
it("should focus cell when Shift+Escape is pressed in vim mode on Windows", () => {
@@ -1783,38 +1766,6 @@ describe("useCellEditorNavigationProps", () => {
expect(focusCell).toHaveBeenCalledWith(mockCellId);
expect(mockEvent.continuePropagation).not.toHaveBeenCalled();
});
-
- it("should focus cell when Ctrl+Escape is pressed in vim mode on Windows", () => {
- const mockEditorView = { current: null };
- const { result } = renderWithProvider(() =>
- useCellEditorNavigationProps(mockCellId, mockEditorView),
- );
-
- const mockEvent = Mocks.keyboardEvent({ key: "Escape", ctrlKey: true });
-
- act(() => {
- result.current.onKeyDown?.(mockEvent);
- });
-
- expect(focusCell).toHaveBeenCalledWith(mockCellId);
- expect(mockEvent.continuePropagation).not.toHaveBeenCalled();
- });
-
- it("should focus cell when Escape (without Shift) is pressed in vim mode on Windows", () => {
- const mockEditorView = { current: null };
- const { result } = renderWithProvider(() =>
- useCellEditorNavigationProps(mockCellId, mockEditorView),
- );
-
- const mockEvent = Mocks.keyboardEvent({ key: "Escape" });
-
- act(() => {
- result.current.onKeyDown?.(mockEvent);
- });
-
- expect(focusCell).toHaveBeenCalledWith(mockCellId);
- expect(mockEvent.continuePropagation).not.toHaveBeenCalled();
- });
});
});
});
diff --git a/frontend/src/components/editor/navigation/navigation.ts b/frontend/src/components/editor/navigation/navigation.ts
index e9447d32bd..f10e148d44 100644
--- a/frontend/src/components/editor/navigation/navigation.ts
+++ b/frontend/src/components/editor/navigation/navigation.ts
@@ -655,12 +655,16 @@ export function useCellEditorNavigationProps(
const { keyboardProps } = useKeyboard({
onKeyDown: (evt) => {
- // For vim mode, use configurable shortcut
- if (keymapPreset === "vim" && vimCommandModeShortcut(evt)) {
- handleEscape();
- } else if (evt.key === "Escape") {
+ if (keymapPreset === "vim") {
+ // For vim mode, use configurable shortcut
+ if (vimCommandModeShortcut(evt)) {
+ handleEscape();
+ }
+ } else {
// For non-vim mode, regular Escape exits to command mode
- handleEscape();
+ if (evt.key === "Escape") {
+ handleEscape();
+ }
}
evt.continuePropagation();
},
diff --git a/frontend/src/core/config/config.ts b/frontend/src/core/config/config.ts
index 54af4eddeb..dc47f6c3ce 100644
--- a/frontend/src/core/config/config.ts
+++ b/frontend/src/core/config/config.ts
@@ -2,6 +2,7 @@
import { atom, useAtom, useAtomValue, useSetAtom } from "jotai";
import { merge } from "lodash-es";
import { OverridingHotkeyProvider } from "../hotkeys/hotkeys";
+import { type Platform, resolvePlatform } from "../hotkeys/shortcuts";
import { store } from "../state/jotai";
import {
type AppConfig,
@@ -31,9 +32,12 @@ export const hotkeyOverridesAtom = atom((get) => {
return get(resolvedMarimoConfigAtom).keymap.overrides ?? {};
});
+export const platformAtom = atom<Platform>(resolvePlatform());
+
export const hotkeysAtom = atom((get) => {
const overrides = get(hotkeyOverridesAtom);
- return new OverridingHotkeyProvider(overrides);
+ const platform = get(platformAtom);
+ return new OverridingHotkeyProvider(overrides, { platform });
});
export const autoSaveConfigAtom = atom((get) => {
diff --git a/frontend/src/core/hotkeys/hotkeys.ts b/frontend/src/core/hotkeys/hotkeys.ts
index 0be4cdcc61..23151814a6 100644
--- a/frontend/src/core/hotkeys/hotkeys.ts
+++ b/frontend/src/core/hotkeys/hotkeys.ts
@@ -1,5 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { isPlatformMac, isPlatformWindows } from "@/core/hotkeys/shortcuts";
+import { type Platform, resolvePlatform } from "@/core/hotkeys/shortcuts";
import { Objects } from "@/utils/objects";
export const NOT_SET: unique symbol = Symbol("NOT_SET");
@@ -35,7 +35,6 @@ interface ResolvedHotkey {
}
type ModKey = "Cmd" | "Ctrl";
-type Platform = "mac" | "windows" | "linux";
export type HotkeyGroup =
| "Running Cells"
@@ -508,8 +507,9 @@ export class OverridingHotkeyProvider extends HotkeyProvider {
private readonly overrides: Partial<
Record<HotkeyAction, string | undefined>
>,
+ options: HotkeyProviderOptions = {},
) {
- super(DEFAULT_HOT_KEY);
+ super(DEFAULT_HOT_KEY, options);
}
override getHotkey(action: HotkeyAction): ResolvedHotkey {
@@ -521,13 +521,3 @@ export class OverridingHotkeyProvider extends HotkeyProvider {
};
}
}
-
-function resolvePlatform(): Platform {
- if (isPlatformMac()) {
- return "mac";
- }
- if (isPlatformWindows()) {
- return "windows";
- }
- return "linux";
-}
diff --git a/frontend/src/core/hotkeys/shortcuts.ts b/frontend/src/core/hotkeys/shortcuts.ts
index e320a4daad..3d842617e6 100644
--- a/frontend/src/core/hotkeys/shortcuts.ts
+++ b/frontend/src/core/hotkeys/shortcuts.ts
@@ -132,3 +132,15 @@ export function parseShortcut(
const keys = shortcut.split(separator).map(normalizeKey);
return (e: IKeyboardEvent) => areKeysPressed(keys, e);
}
+
+export type Platform = "mac" | "windows" | "linux";
+
+export function resolvePlatform(): Platform {
+ if (isPlatformMac()) {
+ return "mac";
+ }
+ if (isPlatformWindows()) {
+ return "windows";
+ }
+ return "linux";
+}