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.

improvement: sync vim mode (#1807)

Myles Scolnick0e737a8579cd
diff --git a/frontend/src/core/codemirror/keymaps/keymaps.ts b/frontend/src/core/codemirror/keymaps/keymaps.ts
index 3d989b9f51..1c5dfdddc1 100644
--- a/frontend/src/core/codemirror/keymaps/keymaps.ts
+++ b/frontend/src/core/codemirror/keymaps/keymaps.ts
@@ -1,9 +1,9 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { KeymapConfig } from "@/core/config/config-schema";
+import type { KeymapConfig } from "@/core/config/config-schema";
import { logNever } from "@/utils/assertNever";
import { defaultKeymap } from "@codemirror/commands";
-import { Extension, Prec } from "@codemirror/state";
-import { EditorView, keymap } from "@codemirror/view";
+import { type Extension, Prec } from "@codemirror/state";
+import { type EditorView, keymap } from "@codemirror/view";
import { vim, Vim } from "@replit/codemirror-vim";
import { vimKeymapExtension } from "./vim";
@@ -38,7 +38,6 @@ export function keymapBundle(
callbacks.deleteCell();
});
return [
- vimKeymapExtension(callbacks),
// delete the cell on double press of "d", if the cell is empty
Prec.highest(
doubleCharacterListener(
@@ -54,6 +53,8 @@ export function keymapBundle(
),
),
vim({ status: false }),
+ // Needs to come after the vim extension
+ vimKeymapExtension(callbacks),
keymap.of(defaultKeymap),
];
default:
diff --git a/frontend/src/core/codemirror/keymaps/vim.ts b/frontend/src/core/codemirror/keymaps/vim.ts
index e0fd9a0d27..45b36d712d 100644
--- a/frontend/src/core/codemirror/keymaps/vim.ts
+++ b/frontend/src/core/codemirror/keymaps/vim.ts
@@ -1,16 +1,20 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { keymap } from "@codemirror/view";
+import { getCM, Vim } from "@replit/codemirror-vim";
+import { type EditorView, keymap, ViewPlugin } from "@codemirror/view";
import {
isAtEndOfEditor,
isAtStartOfEditor,
isInVimNormalMode,
} from "../utils";
+import { invariant } from "@/utils/invariant";
+import type { Extension } from "@codemirror/state";
+import { Logger } from "@/utils/Logger";
export function vimKeymapExtension(callbacks: {
focusUp: () => void;
focusDown: () => void;
-}) {
+}): Extension[] {
return [
keymap.of([
{
@@ -36,5 +40,115 @@ export function vimKeymapExtension(callbacks: {
},
},
]),
+ ViewPlugin.define((view) => {
+ CodeMirrorVimSync.INSTANCES.addInstance(view);
+ return {
+ destroy() {
+ CodeMirrorVimSync.INSTANCES.removeInstance(view);
+ },
+ };
+ }),
];
}
+
+class CodeMirrorVimSync {
+ private instances = new Set<EditorView>();
+ private isBroadcasting = false;
+
+ public static INSTANCES: CodeMirrorVimSync = new CodeMirrorVimSync();
+
+ private constructor() {
+ // noop
+ }
+
+ addInstance(instance: EditorView) {
+ this.instances.add(instance);
+
+ const cm = getCM(instance);
+ if (!cm) {
+ Logger.warn(
+ "Expected CodeMirror instance to have CodeMirror instance state",
+ );
+ return;
+ }
+
+ // Create an event listener for Vim mode changes
+ // When it changes, we broadcast it to all other CodeMirror instances
+ cm.on("vim-mode-change", (e: { mode: string }) => {
+ if (this.isBroadcasting) {
+ return;
+ }
+ invariant("mode" in e, 'Expected event to have a "mode" property');
+ const mode = e.mode;
+ this.isBroadcasting = true;
+ this.broadcastModeChange(instance, mode);
+ this.isBroadcasting = false;
+ });
+ }
+
+ removeInstance(instance: EditorView) {
+ this.instances.delete(instance);
+ }
+
+ broadcastModeChange(originInstance: EditorView, mode: string) {
+ invariant(
+ "exitInsertMode" in Vim,
+ "Vim does not have an exitInsertMode method",
+ );
+ invariant(
+ "exitVisualMode" in Vim,
+ "Vim does not have an exitVisualMode method",
+ );
+
+ for (const instance of this.instances) {
+ if (instance !== originInstance) {
+ const cm = getCM(instance);
+ if (!cm) {
+ Logger.warn(
+ "Expected CodeMirror instance to have CodeMirror instance state",
+ );
+ continue;
+ }
+
+ // HACK: setSelections will steal focus from the current editor
+ // so we remove it and set it back afterwards
+ const prevSetSelections = cm.setSelections.bind(cm);
+ cm.setSelections = () => {
+ // noop
+ return [];
+ };
+
+ const vim = cm.state.vim;
+ if (!vim) {
+ Logger.warn("Expected CodeMirror instance to have Vim state");
+ continue;
+ }
+
+ switch (mode) {
+ case "normal":
+ // Only exit insert mode if we're in it
+ if (vim.insertMode) {
+ Vim.exitInsertMode(cm, true);
+ }
+ // Only exit visual mode if we're in it
+ if (vim.visualMode) {
+ Vim.exitVisualMode(cm, true);
+ }
+ break;
+ case "insert":
+ // Only enter insert mode if we're not already in it
+ if (!vim.insertMode) {
+ Vim.handleKey(cm, "i", "");
+ }
+ break;
+ case "visual":
+ // We don't switch to visual mode across instances
+ break;
+ }
+
+ // HACK: restore selection
+ cm.setSelections = prevSetSelections;
+ }
+ }
+ }
+}
diff --git a/frontend/src/css/common.css b/frontend/src/css/common.css
index d9aa976a00..e8e17ad867 100644
--- a/frontend/src/css/common.css
+++ b/frontend/src/css/common.css
@@ -8,14 +8,12 @@
}
.hover-action:hover,
-.hover-action:focus,
-.hover-action:focus-within {
+.hover-action:focus {
display: inline-flex !important;
}
.hover-actions-parent:hover .hover-action,
-.hover-actions-parent:focus .hover-action,
-.hover-actions-parent:focus-within .hover-action {
+.hover-actions-parent:focus .hover-action {
display: inline-flex !important;
}