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.

fix escape logic for selections and multiple cursors in a cell (#7434)

Shahmir Varqha525ea7300bd1
diff --git a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
index 7b9421baff..37872e57da 100644
--- a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
+++ b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
@@ -53,6 +53,20 @@ vi.mock("../focus-utils", () => ({
focusCell: vi.fn(),
}));
+// Mock simplifySelection from @codemirror/commands
+const mockSimplifySelection = vi.fn();
+vi.mock("@codemirror/commands", () => ({
+ simplifySelection: (...args: unknown[]) => mockSimplifySelection(...args),
+}));
+
+// Mock @codemirror/autocomplete
+const mockCompletionStatus = vi.fn();
+const mockCloseCompletion = vi.fn();
+vi.mock("@codemirror/autocomplete", () => ({
+ completionStatus: (...args: unknown[]) => mockCompletionStatus(...args),
+ closeCompletion: (...args: unknown[]) => mockCloseCompletion(...args),
+}));
+
// Get mocked functions
const mockUseCellActions = vi.mocked(
await import("@/core/cells/cells"),
@@ -1594,6 +1608,11 @@ describe("useCellEditorNavigationProps", () => {
// Reset config overrides
store.set(configOverridesAtom, {});
+
+ // Default: simplifySelection returns false (nothing to simplify)
+ mockSimplifySelection.mockReturnValue(false);
+ // Default: no autocomplete popup
+ mockCompletionStatus.mockReturnValue(null);
});
describe("keyboard shortcuts", () => {
@@ -1614,13 +1633,15 @@ describe("useCellEditorNavigationProps", () => {
});
it("should clear text selection when Escape is pressed with selection", () => {
- const mockDispatch = vi.fn();
+ // Return true (simplified something)
+ mockSimplifySelection.mockReturnValue(true);
+
const mockEditorView = {
current: {
state: {
selection: { main: { from: 5, to: 10 } },
},
- dispatch: mockDispatch,
+ dispatch: vi.fn(),
} as unknown as EditorView,
};
const { result } = renderWithProvider(() =>
@@ -1633,30 +1654,72 @@ describe("useCellEditorNavigationProps", () => {
result.current.onKeyDown?.(mockEvent);
});
- const mockCall = mockDispatch.mock.calls[0][0];
- expect(mockCall.selection.ranges[0].anchor).toBe(5);
- expect(mockCall.selection.ranges[0].head).toBe(5);
+ expect(mockSimplifySelection).toHaveBeenCalledWith(
+ mockEditorView.current,
+ );
+ // Should not exit to command mode when simplifySelection returns true
expect(focusCell).not.toHaveBeenCalled();
});
+ it("should collapse multiple cursors on first Escape, then exit to command mode on second Escape", () => {
+ const mockEditorView = {
+ current: {
+ state: {
+ selection: {
+ main: { from: 10, to: 10, empty: true },
+ ranges: [
+ { from: 5, to: 5, empty: true },
+ { from: 10, to: 10, empty: true },
+ { from: 15, to: 15, empty: true },
+ ],
+ },
+ },
+ dispatch: vi.fn(),
+ } as unknown as EditorView,
+ };
+ const { result } = renderWithProvider(() =>
+ useCellEditorNavigationProps(mockCellId, mockEditorView),
+ );
+
+ // First Escape: simplifySelection returns true (collapsed multiple cursors)
+ mockSimplifySelection.mockReturnValue(true);
+ const firstEscape = Mocks.keyboardEvent({ key: "Escape" });
+
+ act(() => {
+ result.current.onKeyDown?.(firstEscape);
+ });
+
+ expect(mockSimplifySelection).toHaveBeenCalledWith(
+ mockEditorView.current,
+ );
+ // Should NOT exit to command mode - first Escape simplifies cursors
+ expect(focusCell).not.toHaveBeenCalled();
+
+ // Second Escape: nothing left to simplify
+ mockSimplifySelection.mockReturnValue(false);
+ const secondEscape = Mocks.keyboardEvent({ key: "Escape" });
+
+ act(() => {
+ result.current.onKeyDown?.(secondEscape);
+ });
+
+ // Exit to command mode
+ expect(focusCell).toHaveBeenCalledWith(mockCellId);
+ });
+
it("should close autocomplete popup when Escape is pressed with popup active", () => {
+ mockSimplifySelection.mockReturnValue(false); // Mock: nothing to simplify
+ mockCompletionStatus.mockReturnValue("active"); // Mock: popup is open
+
const mockEditorView = {
current: {
state: {
selection: { main: { from: 5, to: 5, empty: true } },
- field: vi.fn().mockReturnValue({ active: [{ state: 1 }] }), // Mock active completion
},
dispatch: vi.fn(),
} as unknown as EditorView,
};
- // Mock the closeCompletion function
- const originalCloseCompletion = vi.hoisted(() => vi.fn());
- vi.mock("@codemirror/autocomplete", () => ({
- completionStatus: vi.fn().mockReturnValue("active"),
- closeCompletion: originalCloseCompletion,
- }));
-
const { result } = renderWithProvider(() =>
useCellEditorNavigationProps(mockCellId, mockEditorView),
);
@@ -1667,9 +1730,7 @@ describe("useCellEditorNavigationProps", () => {
result.current.onKeyDown?.(mockEvent);
});
- expect(originalCloseCompletion).toHaveBeenCalledWith(
- mockEditorView.current,
- );
+ expect(mockCloseCompletion).toHaveBeenCalledWith(mockEditorView.current);
expect(focusCell).not.toHaveBeenCalled();
});
diff --git a/frontend/src/components/editor/navigation/navigation.ts b/frontend/src/components/editor/navigation/navigation.ts
index 4f8e909fec..4541cbd833 100644
--- a/frontend/src/components/editor/navigation/navigation.ts
+++ b/frontend/src/components/editor/navigation/navigation.ts
@@ -1,7 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { closeCompletion, completionStatus } from "@codemirror/autocomplete";
-import { EditorSelection } from "@codemirror/state";
+import { simplifySelection } from "@codemirror/commands";
import type { EditorView } from "@codemirror/view";
import { useAtomValue, useSetAtom, useStore } from "jotai";
import { useMemo } from "react";
@@ -635,12 +635,8 @@ export function useCellEditorNavigationProps(
const view = editorView.current;
const state = view.state;
- const hasTextSelection = !state.selection.main.empty;
-
- if (hasTextSelection) {
- view.dispatch({
- selection: EditorSelection.single(state.selection.main.from), // Cursor to the start of the selection
- });
+ const wasSimplified = simplifySelection(view);
+ if (wasSimplified) {
return;
}