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.

Clear selection or autocomplete popup first before exiting to command mode (#5849)

Shahmir Varqhaffa831390d6b
diff --git a/frontend/src/components/editor/cell/code/cell-editor.tsx b/frontend/src/components/editor/cell/code/cell-editor.tsx
index 093144f04a..85ef4a49a5 100644
--- a/frontend/src/components/editor/cell/code/cell-editor.tsx
+++ b/frontend/src/components/editor/cell/code/cell-editor.tsx
@@ -386,7 +386,7 @@ const CellEditorInternal = ({
};
}, [editorViewRef]);
- const navigationProps = useCellEditorNavigationProps(cellId);
+ const navigationProps = useCellEditorNavigationProps(cellId, editorViewRef);
// Completely hide the editor & icons if it's markdown and hidden. If there is output, we show.
const showHideButton =
diff --git a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
index 5c8015cb79..2eedf2cdca 100644
--- a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
+++ b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
@@ -1305,11 +1305,21 @@ describe("useCellNavigationProps", () => {
// Column 1: cellId2 (0-80), cellId4 (80-160), cellId5 (160-240)
mockGetElementById.mockImplementation((id) => {
const idToCellId = id.replace("cell-", "");
- if (idToCellId === cellId1) return createMockElement(0, 100);
- if (idToCellId === cellId2) return createMockElement(0, 80);
- if (idToCellId === cellId3) return createMockElement(100, 100);
- if (idToCellId === cellId4) return createMockElement(80, 80);
- if (idToCellId === cellId5) return createMockElement(160, 80);
+ if (idToCellId === cellId1) {
+ return createMockElement(0, 100);
+ }
+ if (idToCellId === cellId2) {
+ return createMockElement(0, 80);
+ }
+ if (idToCellId === cellId3) {
+ return createMockElement(100, 100);
+ }
+ if (idToCellId === cellId4) {
+ return createMockElement(80, 80);
+ }
+ if (idToCellId === cellId5) {
+ return createMockElement(160, 80);
+ }
return null;
});
@@ -1580,8 +1590,9 @@ describe("useCellEditorNavigationProps", () => {
describe("keyboard shortcuts", () => {
it("should focus cell when Escape is pressed", () => {
+ const mockEditorView = { current: null };
const { result } = renderWithProvider(() =>
- useCellEditorNavigationProps(mockCellId),
+ useCellEditorNavigationProps(mockCellId, mockEditorView),
);
const mockEvent = Mocks.keyboardEvent({ key: "Escape" });
@@ -1594,9 +1605,70 @@ describe("useCellEditorNavigationProps", () => {
expect(mockEvent.continuePropagation).not.toHaveBeenCalled();
});
+ it("should clear text selection when Escape is pressed with selection", () => {
+ const mockDispatch = vi.fn();
+ const mockEditorView = {
+ current: {
+ state: {
+ selection: { main: { from: 5, to: 10 } },
+ },
+ dispatch: mockDispatch,
+ } as unknown as EditorView,
+ };
+ const { result } = renderWithProvider(() =>
+ useCellEditorNavigationProps(mockCellId, mockEditorView),
+ );
+
+ const mockEvent = Mocks.keyboardEvent({ key: "Escape" });
+
+ act(() => {
+ 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(focusCell).not.toHaveBeenCalled();
+ });
+
+ it("should close autocomplete popup when Escape is pressed with popup active", () => {
+ const mockEditorView = {
+ current: {
+ state: {
+ selection: { main: { from: 5, to: 5 } },
+ 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),
+ );
+
+ const mockEvent = Mocks.keyboardEvent({ key: "Escape" });
+
+ act(() => {
+ result.current.onKeyDown?.(mockEvent);
+ });
+
+ expect(originalCloseCompletion).toHaveBeenCalledWith(
+ mockEditorView.current,
+ );
+ expect(focusCell).not.toHaveBeenCalled();
+ });
+
it("should continue propagation for other keys", () => {
+ const mockEditorView = { current: null };
const { result } = renderWithProvider(() =>
- useCellEditorNavigationProps(mockCellId),
+ useCellEditorNavigationProps(mockCellId, mockEditorView),
);
const mockEvent = Mocks.keyboardEvent({ key: "Enter" });
@@ -1621,8 +1693,9 @@ describe("useCellEditorNavigationProps", () => {
});
it("should focus cell when Ctrl+Escape is pressed in vim mode", () => {
+ const mockEditorView = { current: null };
const { result } = renderWithProvider(() =>
- useCellEditorNavigationProps(mockCellId),
+ useCellEditorNavigationProps(mockCellId, mockEditorView),
);
const mockEvent = Mocks.keyboardEvent({ key: "Escape", ctrlKey: true });
@@ -1636,8 +1709,9 @@ describe("useCellEditorNavigationProps", () => {
});
it("should focus cell when Cmd+Escape (metaKey) is pressed in vim mode", () => {
+ const mockEditorView = { current: null };
const { result } = renderWithProvider(() =>
- useCellEditorNavigationProps(mockCellId),
+ useCellEditorNavigationProps(mockCellId, mockEditorView),
);
const mockEvent = Mocks.keyboardEvent({ key: "Escape", metaKey: true });
@@ -1651,8 +1725,9 @@ describe("useCellEditorNavigationProps", () => {
});
it("should not focus cell when Escape (without Ctrl) is pressed in vim mode", () => {
+ const mockEditorView = { current: null };
const { result } = renderWithProvider(() =>
- useCellEditorNavigationProps(mockCellId),
+ useCellEditorNavigationProps(mockCellId, mockEditorView),
);
const mockEvent = Mocks.keyboardEvent({ key: "Escape" });
diff --git a/frontend/src/components/editor/navigation/navigation.ts b/frontend/src/components/editor/navigation/navigation.ts
index c98dc72b86..8057937d8a 100644
--- a/frontend/src/components/editor/navigation/navigation.ts
+++ b/frontend/src/components/editor/navigation/navigation.ts
@@ -1,5 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
+import { closeCompletion, completionStatus } from "@codemirror/autocomplete";
+import { EditorSelection } from "@codemirror/state";
import type { EditorView } from "@codemirror/view";
import { useAtomValue, useSetAtom, useStore } from "jotai";
import { mergeProps, useFocusWithin, useKeyboard } from "react-aria";
@@ -600,23 +602,61 @@ export function useCellNavigationProps(
*
* Handles both keyboard and mouse navigation.
*/
-export function useCellEditorNavigationProps(cellId: CellId) {
+export function useCellEditorNavigationProps(
+ cellId: CellId,
+ editorView: React.RefObject<EditorView | null>,
+) {
const setTemporarilyShownCode = useSetAtom(temporarilyShownCodeAtom);
const keymapPreset = useAtomValue(keymapPresetAtom);
+ const exitToCommandMode = () => {
+ setTemporarilyShownCode(false);
+ focusCell(cellId);
+ };
+
+ const handleEscape = () => {
+ // If there is a text selection or autocomplete popup in the editor, we clear those and return.
+ // Subsequent 'Escapes' will exit to command mode.
+
+ if (!editorView.current) {
+ // If no editor, we can exit to command mode immediately
+ exitToCommandMode();
+ return;
+ }
+
+ const view = editorView.current;
+ const state = view.state;
+
+ const hasTextSelection =
+ state.selection.main.from !== state.selection.main.to;
+
+ if (hasTextSelection) {
+ view.dispatch({
+ selection: EditorSelection.single(state.selection.main.from), // Cursor to the start of the selection
+ });
+ return;
+ }
+
+ const hasAutocompletePopup = completionStatus(state) !== null;
+ if (hasAutocompletePopup) {
+ closeCompletion(view);
+ return;
+ }
+
+ exitToCommandMode();
+ };
+
const { keyboardProps } = useKeyboard({
onKeyDown: (evt) => {
// For vim mode, require Ctrl+Escape (or Cmd+Escape on Mac) to exit to command mode
if (keymapPreset === "vim") {
if (evt.key === "Escape" && (evt.ctrlKey || evt.metaKey)) {
- setTemporarilyShownCode(false);
- focusCell(cellId);
+ handleEscape();
}
} else {
// For non-vim mode, regular Escape exits to command mode
if (evt.key === "Escape") {
- setTemporarilyShownCode(false);
- focusCell(cellId);
+ handleEscape();
}
}