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(command-mode): add shortcut to create a SQL cell below (#10701)

Aniket Saha6dca18001055
diff --git a/docs/guides/editor_features/overview.md b/docs/guides/editor_features/overview.md
index c4a5b0686f..59aa0a195e 100644
--- a/docs/guides/editor_features/overview.md
+++ b/docs/guides/editor_features/overview.md
@@ -53,6 +53,7 @@ editing their contents.
- `Shift+↓`/`Shift+↑` - multi-select cells
- `Enter` - edit selected cell
- `a`/`b` - new cell above/below
+- `q` - new SQL cell below
- `c`/`v` - copy/paste cells
- `s` - save notebook
- `Shift+Enter` - run cell and move to next
diff --git a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
index 10f7b3d92e..2d4e2a4f92 100644
--- a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
+++ b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
@@ -12,6 +12,7 @@ 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 { LanguageAdapters } from "@/core/codemirror/language/LanguageAdapters";
import { signatureHintField } from "@/core/codemirror/completion/signature-hint";
import {
configOverridesAtom,
@@ -20,6 +21,8 @@ import {
} from "@/core/config/config";
import { requestClientAtom } from "@/core/network/requests";
import { store } from "@/core/state/jotai";
+import { variablesAtom } from "@/core/variables/state";
+import type { Variables } from "@/core/variables/types";
import type { CellActionsDropdownHandle } from "../../cell/cell-actions";
import {
useCellEditorNavigationProps,
@@ -421,6 +424,33 @@ describe("useCellNavigationProps", () => {
});
});
+ it("should add an import before and a SQL cell after when 'q' is pressed", () => {
+ store.set(variablesAtom, {} as Variables);
+
+ const { result } = renderWithProvider(() =>
+ useCellNavigationProps(mockCellId, options),
+ );
+
+ act(() => {
+ result.current.onKeyDown?.(Mocks.keyboardEvent({ key: "q" }));
+ });
+
+ expect(mockCellActions.createNewCell).toHaveBeenNthCalledWith(
+ 1,
+ expect.objectContaining({
+ cellId: mockCellId,
+ before: true,
+ code: "import marimo as mo",
+ }),
+ );
+ expect(mockCellActions.createNewCell).toHaveBeenNthCalledWith(2, {
+ cellId: mockCellId,
+ before: false,
+ autoFocus: true,
+ code: LanguageAdapters.sql.defaultCode,
+ });
+ });
+
it("should move to top cell when Cmd+ArrowUp is pressed (or Ctrl)", () => {
const { result } = renderWithProvider(() =>
useCellNavigationProps(mockCellId, options),
diff --git a/frontend/src/components/editor/navigation/navigation.ts b/frontend/src/components/editor/navigation/navigation.ts
index cb3a2065a1..e6e8984884 100644
--- a/frontend/src/components/editor/navigation/navigation.ts
+++ b/frontend/src/components/editor/navigation/navigation.ts
@@ -12,6 +12,7 @@ import { useMemo } from "react";
import { mergeProps, useFocusWithin, useKeyboard } from "react-aria";
import { DATA_FOR_CELL_ID } from "@/components/data-table/cell-utils";
import { aiCompletionCellAtom } from "@/core/ai/state";
+import { maybeAddMarimoImport } from "@/core/cells/add-missing-import";
import { cellIdsAtom, notebookAtom, useCellActions } from "@/core/cells/cells";
import { useCellFocusActions } from "@/core/cells/focus";
import type { CellId } from "@/core/cells/ids";
@@ -26,6 +27,7 @@ import {
closeSignatureHint,
signatureHintField,
} from "@/core/codemirror/completion/signature-hint";
+import { LanguageAdapters } from "@/core/codemirror/language/LanguageAdapters";
import {
hotkeysAtom,
isAiFeatureEnabled,
@@ -551,6 +553,24 @@ export function useCellNavigationProps(
actions.createNewCell({ cellId, before: false, autoFocus: true });
return true;
},
+ "command.createSqlCellAfter": (cellId) => {
+ if (Events.hasModifier(evt)) {
+ return false;
+ }
+ maybeAddMarimoImport({
+ autoInstantiate: true,
+ createNewCell: actions.createNewCell,
+ fromCellId: cellId,
+ before: true,
+ });
+ actions.createNewCell({
+ cellId,
+ before: false,
+ autoFocus: true,
+ code: LanguageAdapters.sql.defaultCode,
+ });
+ return true;
+ },
"cell.delete": () => {
// Only handle if destructive_delete is enabled
if (!userConfig.keymap.destructive_delete) {
diff --git a/frontend/src/core/cells/__tests__/add-missing-import.test.ts b/frontend/src/core/cells/__tests__/add-missing-import.test.ts
index eab252b320..f6b4431cef 100644
--- a/frontend/src/core/cells/__tests__/add-missing-import.test.ts
+++ b/frontend/src/core/cells/__tests__/add-missing-import.test.ts
@@ -120,4 +120,24 @@ describe("maybeAddMissingImport", () => {
expect(result).toBeNull();
}
});
+
+ it("should create a marimo import before the source cell when requested", () => {
+ const createNewCell = vi.fn();
+
+ const result = maybeAddMarimoImport({
+ autoInstantiate: false,
+ createNewCell,
+ fromCellId: Cell2,
+ before: true,
+ });
+
+ expect(result).not.toBeNull();
+ expect(createNewCell).toHaveBeenCalledWith(
+ expect.objectContaining({
+ cellId: Cell2,
+ before: true,
+ code: "import marimo as mo",
+ }),
+ );
+ });
});
diff --git a/frontend/src/core/hotkeys/hotkeys.ts b/frontend/src/core/hotkeys/hotkeys.ts
index 77b0168ad1..25002b7995 100644
--- a/frontend/src/core/hotkeys/hotkeys.ts
+++ b/frontend/src/core/hotkeys/hotkeys.ts
@@ -458,6 +458,11 @@ const DEFAULT_HOT_KEY = {
group: "Command",
key: "b",
},
+ "command.createSqlCellAfter": {
+ name: "Create a SQL cell after current cell",
+ group: "Command",
+ key: "q",
+ },
"command.copyCell": {
name: "Copy cell",
group: "Command",