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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: configurable toggle-line-comment (#4728)
Myles Scolnickcea63765c7a5
diff --git a/frontend/src/components/app-config/user-config-form.tsx b/frontend/src/components/app-config/user-config-form.tsxindex 42635ec302..4e8ecf9b61 100644--- a/frontend/src/components/app-config/user-config-form.tsx+++ b/frontend/src/components/app-config/user-config-form.tsx@@ -1268,37 +1268,6 @@ export const UserConfigForm: React.FC = () => {notebook to take effect.</p>- {!isWasm() && (- <FormField- control={form.control}- name="experimental.lsp"- render={({ field }) => (- <div className="flex flex-col gap-y-1">- <FormItem className={formItemClasses}>- <FormLabel className="font-normal">- LSP (Language Server Protocol)- </FormLabel>- <FormControl>- <Checkbox- data-testid="inline-ai-checkbox"- checked={field.value === true}- onCheckedChange={field.onChange}- />- </FormControl>- </FormItem>- <FormDescription>- Enable experimental LSP support. You will need to have- <Kbd className="inline">marimo[lsp]</Kbd> installed in- your environment to use this. See{" "}- <ExternalLink href="https://docs.marimo.io/guides/editor_features/language_server/">- docs- </ExternalLink>{" "}- for more info.- </FormDescription>- </div>- )}- />- )}<FormFieldcontrol={form.control}name="experimental.inline_ai_tooltip"diff --git a/frontend/src/core/codemirror/cm.ts b/frontend/src/core/codemirror/cm.tsindex 44f725c72a..46a0d8fe76 100644--- a/frontend/src/core/codemirror/cm.ts+++ b/frontend/src/core/codemirror/cm.ts@@ -106,7 +106,7 @@ export const setupCodeMirror = (opts: CodeMirrorSetupOpts): Extension[] => {return [// Editor keymaps (vim or defaults) based on user config- keymapBundle(keymapConfig),+ keymapBundle(keymapConfig, hotkeys),dndBundle(),pasteBundle(),jupyterHelpExtension(),diff --git a/frontend/src/core/codemirror/keymaps/__tests__/keymaps.test.ts b/frontend/src/core/codemirror/keymaps/__tests__/keymaps.test.tsnew file mode 100644index 0000000000..7e48c9e79e--- /dev/null+++ b/frontend/src/core/codemirror/keymaps/__tests__/keymaps.test.ts@@ -0,0 +1,42 @@+/* Copyright 2024 Marimo. All rights reserved. */+import { describe, it, expect } from "vitest";+import { defaultKeymap as originalDefaultKeymap } from "@codemirror/commands";+import { visibleForTesting } from "../keymaps";+import { HotkeyProvider } from "@/core/hotkeys/hotkeys";++const { defaultKeymap, defaultVimKeymap, overrideKeymap, OVERRIDDEN_COMMANDS } =+ visibleForTesting;++describe("keymaps", () => {+ it("should filter out overridden commands from default keymap", () => {+ // Get the defaultKeymap function result+ const filteredKeymap = defaultKeymap();++ // Original keymap should have more entries than the filtered one+ expect(originalDefaultKeymap.length).toBeGreaterThan(filteredKeymap.length);++ // The difference should be equal to the size of OVERRIDDEN_COMMANDS+ expect(originalDefaultKeymap.length - filteredKeymap.length).toBe(+ OVERRIDDEN_COMMANDS.size,+ );++ // Verify none of the overridden commands are in the filtered keymap+ for (const binding of filteredKeymap) {+ expect(OVERRIDDEN_COMMANDS.has(binding.run)).toBe(false);+ }+ });++ it("defaultVimKeymap should remove conflicting keys", () => {+ const vimKeymap = defaultVimKeymap();+ expect(vimKeymap.length).toBeLessThan(defaultKeymap().length);+ });++ it("overrideKeymap should have the same size as OVERRIDDEN_COMMANDS", () => {+ const keys = overrideKeymap(HotkeyProvider.create());+ expect(keys.length).toBe(OVERRIDDEN_COMMANDS.size);++ for (const command of OVERRIDDEN_COMMANDS) {+ expect(keys.some((k) => k.run === command)).toBe(true);+ }+ });+});diff --git a/frontend/src/core/codemirror/keymaps/keymaps.ts b/frontend/src/core/codemirror/keymaps/keymaps.tsindex b90b25dbd5..37806ddba6 100644--- a/frontend/src/core/codemirror/keymaps/keymaps.ts+++ b/frontend/src/core/codemirror/keymaps/keymaps.ts@@ -1,21 +1,36 @@/* Copyright 2024 Marimo. All rights reserved. */import type { KeymapConfig } from "@/core/config/config-schema";import { logNever } from "@/utils/assertNever";-import { defaultKeymap, insertNewlineAndIndent } from "@codemirror/commands";+import {+ defaultKeymap as originalDefaultKeymap,+ insertNewlineAndIndent,+ toggleBlockComment,+ toggleComment,+} from "@codemirror/commands";import { type Extension, Prec } from "@codemirror/state";-import { type EditorView, keymap } from "@codemirror/view";+import {+ type Command,+ type EditorView,+ type KeyBinding,+ keymap,+} from "@codemirror/view";import { getCM, vim } from "@replit/codemirror-vim";import { vimKeymapExtension } from "./vim";import { once } from "@/utils/once";import { cellActionsState } from "../cells/state";+import type { HotkeyProvider } from "@/core/hotkeys/hotkeys";export const KEYMAP_PRESETS = ["default", "vim"] as const;-export function keymapBundle(config: KeymapConfig): Extension[] {+export function keymapBundle(+ config: KeymapConfig,+ hotkeys: HotkeyProvider,+): Extension[] {switch (config.preset) {case "default":return [- keymap.of(defaultKeymap),+ keymap.of(defaultKeymap()),+ keymap.of(overrideKeymap(hotkeys)),// Should be the last thing to close when Escape is pressedPrec.low(keymap.of([@@ -32,8 +47,8 @@ export function keymapBundle(config: KeymapConfig): Extension[] {];case "vim":return [- // Filter out Enter from defaultKeymapkeymap.of(defaultVimKeymap()),+ keymap.of(overrideKeymap(hotkeys)),// Add Enter back for insert mode onlykeymap.of([{@@ -73,6 +88,33 @@ export function keymapBundle(config: KeymapConfig): Extension[] {}}+// Override commands from the default keymap+// https://github.com/codemirror/commands/blob/6.8.1/src/commands.ts#L1043+// We can add more from the default keymap if needed over time,+// but there are currently 50+ commands in the default keymap+const OVERRIDDEN_COMMANDS = new Set<Command | undefined>([+ toggleComment,+ toggleBlockComment,+]);++const defaultKeymap = once(() => {+ // Filter out commands that are in the overridden set+ return originalDefaultKeymap.filter((k) => !OVERRIDDEN_COMMANDS.has(k.run));+});++const overrideKeymap = (keymap: HotkeyProvider): readonly KeyBinding[] => {+ return [+ {+ key: keymap.getHotkey("cell.toggleComment").key,+ run: toggleComment,+ },+ {+ key: keymap.getHotkey("cell.toggleBlockComment").key,+ run: toggleBlockComment,+ },+ ];+};+const defaultVimKeymap = once(() => {const toRemove = new Set(["Enter", "Ctrl-v"]);// Remove conflicting keys from the keymap@@ -80,7 +122,7 @@ const defaultVimKeymap = once(() => {// - it should just go to the next line// Ctrl-v goes to the bottom of the cell// - should enter blockwise visual mode- return defaultKeymap.filter(+ return defaultKeymap().filter((k) => !toRemove.has(k.key || k.mac || k.linux || k.win || ""),);});@@ -127,3 +169,10 @@ function doubleCharacterListener(},]);}++export const visibleForTesting = {+ defaultKeymap,+ defaultVimKeymap,+ overrideKeymap,+ OVERRIDDEN_COMMANDS,+};diff --git a/frontend/src/core/hotkeys/hotkeys.ts b/frontend/src/core/hotkeys/hotkeys.tsindex e073b4fa28..9b284c4b0e 100644--- a/frontend/src/core/hotkeys/hotkeys.ts+++ b/frontend/src/core/hotkeys/hotkeys.ts@@ -221,11 +221,17 @@ const DEFAULT_HOT_KEY = {group: "Editing",key: "Mod-Shift-'",},- "cell.toggleLineComment": {- name: "Toggle line comment",+ "cell.toggleComment": {+ name: "Toggle comment",group: "Editing",+ // https://github.com/codemirror/commands/blob/6.8.1/src/commands.ts#L1067key: "Mod-/",- editable: false,+ },+ "cell.toggleBlockComment": {+ name: "Toggle block comment",+ group: "Editing",+ // https://github.com/codemirror/commands/blob/6.8.1/src/commands.ts#L1068+ key: "Alt-A",},"cell.renameSymbol": {name: "Rename symbol",diff --git a/marimo/_config/manager.py b/marimo/_config/manager.pyindex bf9a90bfca..5bfc035842 100644--- a/marimo/_config/manager.py+++ b/marimo/_config/manager.py@@ -92,12 +92,6 @@ class MarimoConfigReader:@propertydef language_servers(self) -> LanguageServersConfig:- # LSP is behind an experimental flag,- # if it's not enabled, return an empty config- lsp_enabled = self.experimental.get("lsp", False)- if not lsp_enabled:- return {}-if "language_servers" in self._config:return self._config["language_servers"]return {}