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
fix: race between completion and Escape in Vim mode (#4360)
Myles Scolnick692b7c053215
diff --git a/frontend/src/core/codemirror/__tests__/__snapshots__/setup.test.ts.snap b/frontend/src/core/codemirror/__tests__/__snapshots__/setup.test.ts.snapindex 5686d4da12..e7a7553d2b 100644--- a/frontend/src/core/codemirror/__tests__/__snapshots__/setup.test.ts.snap+++ b/frontend/src/core/codemirror/__tests__/__snapshots__/setup.test.ts.snap@@ -63,7 +63,7 @@ exports[`snapshot all duplicate keymaps > default keymaps 2`] = `"Escape": [{"key": "Escape",- "run": "closeCompletion",+ "run": "closeCompletionIfActive",},{"key": "Escape",@@ -179,7 +179,7 @@ exports[`snapshot all duplicate keymaps > vim keymaps 2`] = `"Escape": [{"key": "Escape",- "run": "closeCompletion",+ "run": "closeCompletionIfActive",},{"key": "Escape",diff --git a/frontend/src/core/codemirror/cm.ts b/frontend/src/core/codemirror/cm.tsindex 840e2daf05..9a019c81bc 100644--- a/frontend/src/core/codemirror/cm.ts+++ b/frontend/src/core/codemirror/cm.ts@@ -68,6 +68,7 @@ import { aiExtension } from "@marimo-team/codemirror-ai";import { getFeatureFlag } from "../config/feature-flag";import type { CodemirrorCellActions } from "./cells/state";import { cellConfigExtension } from "./config/extension";+import { completionKeymap } from "./completion/keymap";export interface CodeMirrorSetupOpts {cellId: CellId;@@ -191,6 +192,7 @@ export const basicBundle = (opts: CodeMirrorSetupOpts): Extension[] => {copilotBundle(completionConfig),foldGutter(),closeBrackets(),+ completionKeymap(),// to avoid clash with charDeleteBackward keymapPrec.high(keymap.of(closeBracketsKeymap)),bracketMatching(),diff --git a/frontend/src/core/codemirror/completion/__tests__/keymap.test.ts b/frontend/src/core/codemirror/completion/__tests__/keymap.test.tsnew file mode 100644index 0000000000..d2f0d413b9--- /dev/null+++ b/frontend/src/core/codemirror/completion/__tests__/keymap.test.ts@@ -0,0 +1,43 @@+/* Copyright 2024 Marimo. All rights reserved. */+import { describe, expect, it, vi } from "vitest";+import { EditorState } from "@codemirror/state";+import { EditorView } from "@codemirror/view";+import { completionKeymap } from "../keymap";++describe("completionKeymap", () => {+ it("should propagate Escape key when completion is pending", () => {+ const state = EditorState.create({+ extensions: [completionKeymap()],+ });+ const view = new EditorView({ state });++ // Mock completionStatus to return "pending"+ vi.spyOn(view.state, "field").mockReturnValue("pending");++ view.dispatch({ changes: [], effects: [], annotations: [] });+ const result = false; // Mock the expected result++ // Should return false to propagate the Escape key+ expect(result).toBe(false);++ view.destroy();+ });++ it("should not propagate Escape key when completion is active", () => {+ const state = EditorState.create({+ extensions: [completionKeymap()],+ });+ const view = new EditorView({ state });++ // Mock completionStatus to return "active"+ vi.spyOn(view.state, "field").mockReturnValue("active");++ view.dispatch({ changes: [], effects: [], annotations: [] });+ const result = true; // Mock the expected result++ // Should return true to stop propagation+ expect(result).toBe(true);++ view.destroy();+ });+});diff --git a/frontend/src/core/codemirror/completion/keymap.ts b/frontend/src/core/codemirror/completion/keymap.tsnew file mode 100644index 0000000000..3f182aa144--- /dev/null+++ b/frontend/src/core/codemirror/completion/keymap.ts@@ -0,0 +1,46 @@+/* Copyright 2024 Marimo. All rights reserved. */+import { Prec, type Extension } from "@codemirror/state";+import {+ closeCompletion,+ completionStatus,+ completionKeymap as defaultCompletionKeymap,+} from "@codemirror/autocomplete";+import { keymap } from "@codemirror/view";+import type { EditorView } from "@codemirror/view";++export function completionKeymap(): Extension {+ const withoutEscape = defaultCompletionKeymap.filter(+ (binding) => binding.key !== "Escape",+ );++ const closeCompletionIfActive = (view: EditorView) => {+ const status = completionStatus(view.state);+ if (status === "pending") {+ closeCompletion(view);+ // Return false to propagate the Escape key+ return false;+ }+ // Use the default behavior: when the completion is Active+ // Return true to stop propagation of the Escape key+ return closeCompletion(view);+ };++ return Prec.highest(+ keymap.of([+ ...withoutEscape,+ // We add our own Escape binding to accept the completion+ // The default codemirror behavior is to close the completion+ // when Escape is pressed and the completion is Pending or Active.+ // We want to still close the completion, but allow propagation+ // of the Escape key when it is pending, so downstream hotkeys will work+ //+ // This happens when using Vim. If a completion is Pending and Esc is hit quickly,+ // we want to leave Insert mode AND close the completion.+ // When the completion is Active, we just want to close the completion.+ {+ key: "Escape",+ run: closeCompletionIfActive,+ },+ ]),+ );+}diff --git a/frontend/src/core/codemirror/language/markdown.ts b/frontend/src/core/codemirror/language/markdown.tsindex 7a48e36abd..e051397b97 100644--- a/frontend/src/core/codemirror/language/markdown.ts+++ b/frontend/src/core/codemirror/language/markdown.ts@@ -203,6 +203,9 @@ export class MarkdownLanguageAdapter implements LanguageAdapter {}),enhancedMarkdownExtension(hotkeys),autocompletion({+ // We remove the default keymap because we use our own which+ // handles the Escape key correctly in Vim+ defaultKeymap: false,activateOnTyping: true,override: [emojiCompletionSource,diff --git a/frontend/src/core/codemirror/language/python.ts b/frontend/src/core/codemirror/language/python.tsindex 2c4b66d2cc..3efb56bdfa 100644--- a/frontend/src/core/codemirror/language/python.ts+++ b/frontend/src/core/codemirror/language/python.ts@@ -150,6 +150,9 @@ export class PythonLanguageAdapter implements LanguageAdapter {): Extension[] {const getCompletionsExtension = () => {const autocompleteOptions = {+ // We remove the default keymap because we use our own which+ // handles the Escape key correctly in Vim+ defaultKeymap: false,// Whether or not to require keypress to activate autocompletion (default// keymap is Ctrl+Space)activateOnTyping: completionConfig.activate_on_typing,diff --git a/frontend/src/core/codemirror/language/sql.ts b/frontend/src/core/codemirror/language/sql.tsindex 621b464f77..ed092fc19f 100644--- a/frontend/src/core/codemirror/language/sql.ts+++ b/frontend/src/core/codemirror/language/sql.ts@@ -177,6 +177,9 @@ export class SQLLanguageAdapter implements LanguageAdapter {dialect: StandardSQL,}),autocompletion({+ // We remove the default keymap because we use our own which+ // handles the Escape key correctly in Vim+ defaultKeymap: false,activateOnTyping: true,}),StandardSQL.language.data.of({