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.

Add Cmd/Alt+Shift+L to toggle between Python and SQL cells (#8517)

Shahmir Varqhad24396cd4c81
diff --git a/frontend/src/components/editor/actions/useCellActionButton.tsx b/frontend/src/components/editor/actions/useCellActionButton.tsx
index b3991d9a13..a8a1efafaa 100644
--- a/frontend/src/components/editor/actions/useCellActionButton.tsx
+++ b/frontend/src/components/editor/actions/useCellActionButton.tsx
@@ -41,7 +41,10 @@ import { hasOnlyOneCellAtom, useCellActions } from "@/core/cells/cells";
import { type CellId, SETUP_CELL_ID } from "@/core/cells/ids";
import type { CellData } from "@/core/cells/types";
import { formatEditorViews } from "@/core/codemirror/format";
-import { toggleToLanguage } from "@/core/codemirror/language/commands";
+import {
+ getCurrentLanguageAdapter,
+ toggleToLanguage,
+} from "@/core/codemirror/language/commands";
import { switchLanguage } from "@/core/codemirror/language/extension";
import { MARKDOWN_INITIAL_HIDE_CODE } from "@/core/codemirror/language/languages/markdown";
import {
@@ -235,35 +238,41 @@ export function useCellActionButtons({ cell, closePopover }: Props) {
},
hidden: isSetupCell,
},
- {
- icon: <DatabaseIcon size={13} strokeWidth={1.5} />,
- label: "Convert to SQL",
- handle: () => {
- const editorView = getEditorView();
- if (!editorView) {
- return;
+ // We need to check this here because the user may have toggled the language
+ getCurrentLanguageAdapter(getEditorView()) === "sql"
+ ? {
+ icon: <PythonIcon />,
+ label: "View as Python",
+ hotkey: "cell.viewAsSQL",
+ handle: () => {
+ const editorView = getEditorView();
+ if (!editorView) {
+ return;
+ }
+ toggleToLanguage(editorView, "python", { force: true });
+ },
+ hidden: isSetupCell,
}
- maybeAddMarimoImport({ autoInstantiate, createNewCell: createCell });
- switchLanguage(editorView, {
- language: "sql",
- keepCodeAsIs: false,
- });
- },
- hidden: isSetupCell,
- },
- {
- icon: <PythonIcon />,
- label: "Toggle as Python",
- handle: () => {
- const editorView = getEditorView();
- if (!editorView) {
- return;
- }
- maybeAddMarimoImport({ autoInstantiate, createNewCell: createCell });
- toggleToLanguage(editorView, "python", { force: true });
- },
- hidden: isSetupCell,
- },
+ : {
+ icon: <DatabaseIcon size={13} strokeWidth={1.5} />,
+ label: "Convert to SQL",
+ hotkey: "cell.viewAsSQL",
+ handle: () => {
+ const editorView = getEditorView();
+ if (!editorView) {
+ return;
+ }
+ maybeAddMarimoImport({
+ autoInstantiate,
+ createNewCell: createCell,
+ });
+ switchLanguage(editorView, {
+ language: "sql",
+ keepCodeAsIs: false,
+ });
+ },
+ hidden: isSetupCell,
+ },
],
// Movement
diff --git a/frontend/src/components/editor/cell/code/cell-editor.tsx b/frontend/src/components/editor/cell/code/cell-editor.tsx
index 6f1a2187a8..827bf9d5d7 100644
--- a/frontend/src/components/editor/cell/code/cell-editor.tsx
+++ b/frontend/src/components/editor/cell/code/cell-editor.tsx
@@ -163,6 +163,13 @@ const CellEditorInternal = ({
}
});
+ const afterToggleSQL = useEvent(() => {
+ maybeAddMarimoImport({
+ autoInstantiate,
+ createNewCell: cellActions.createNewCell,
+ });
+ });
+
const aiEnabled = isAiEnabled(userConfig);
const extensions = useMemo(() => {
@@ -173,6 +180,7 @@ const CellEditorInternal = ({
cellActions: {
...cellActions,
afterToggleMarkdown,
+ afterToggleSQL,
onRun: handleRunCell,
deleteCell: handleDelete,
saveNotebook: saveOrNameNotebook,
@@ -267,6 +275,7 @@ const CellEditorInternal = ({
handleRunCell,
setAiCompletionCell,
afterToggleMarkdown,
+ afterToggleSQL,
setLanguageAdapter,
showHiddenCode,
saveOrNameNotebook,
diff --git a/frontend/src/core/codemirror/__tests__/setup.test.ts b/frontend/src/core/codemirror/__tests__/setup.test.ts
index 87d0e153ae..12b712a0de 100644
--- a/frontend/src/core/codemirror/__tests__/setup.test.ts
+++ b/frontend/src/core/codemirror/__tests__/setup.test.ts
@@ -45,6 +45,7 @@ function getOpts() {
onRun: namedFunction("onRun"),
deleteCell: namedFunction("deleteCell"),
afterToggleMarkdown: namedFunction("afterToggleMarkdown"),
+ afterToggleSQL: namedFunction("afterToggleSQL"),
} as unknown as CodemirrorCellActions,
completionConfig: {
activate_on_typing: false,
diff --git a/frontend/src/core/codemirror/cells/state.ts b/frontend/src/core/codemirror/cells/state.ts
index 9c9fcfb0dc..9e9f332de9 100644
--- a/frontend/src/core/codemirror/cells/state.ts
+++ b/frontend/src/core/codemirror/cells/state.ts
@@ -11,6 +11,7 @@ export interface CodemirrorCellActions extends CellActions {
onRun: () => void;
deleteCell: () => void;
afterToggleMarkdown: () => void;
+ afterToggleSQL: () => void;
saveNotebook: () => void;
}
diff --git a/frontend/src/core/codemirror/extensions.ts b/frontend/src/core/codemirror/extensions.ts
index 86fe918c9a..c4eea6f790 100644
--- a/frontend/src/core/codemirror/extensions.ts
+++ b/frontend/src/core/codemirror/extensions.ts
@@ -42,6 +42,26 @@ export function formatKeymapExtension(hotkeys: HotkeyProvider) {
actions.afterToggleMarkdown();
}
+ return response !== false;
+ },
+ },
+ {
+ key: hotkeys.getHotkey("cell.viewAsSQL").key,
+ preventDefault: true,
+ run: (ev) => {
+ const currentLanguage = getCurrentLanguageAdapter(ev);
+ const destinationLanguage =
+ currentLanguage === "sql" ? "python" : "sql";
+
+ const response = toggleToLanguage(ev, destinationLanguage, {
+ force: true,
+ });
+
+ if (response === "sql") {
+ const actions = ev.state.facet(cellActionsState);
+ actions.afterToggleSQL();
+ }
+
return response !== false;
},
},
diff --git a/frontend/src/core/hotkeys/hotkeys.ts b/frontend/src/core/hotkeys/hotkeys.ts
index fb167959d6..9e1c9eb03a 100644
--- a/frontend/src/core/hotkeys/hotkeys.ts
+++ b/frontend/src/core/hotkeys/hotkeys.ts
@@ -142,6 +142,14 @@ const DEFAULT_HOT_KEY = {
group: "Editing",
key: "Mod-Shift-m",
},
+ "cell.viewAsSQL": {
+ name: "Toggle SQL",
+ group: "Editing",
+ key: {
+ windows: "Alt-Shift-l",
+ main: "Mod-Shift-l",
+ },
+ },
"cell.complete": {
name: "Code completion",
group: "Editing",