Skip to content

Commit

Permalink
improvement: add vim go-to-definition (gd) (marimo-team#1862)
Browse files Browse the repository at this point in the history
* improvement: add vim go-to-definition (gd)

* lint
  • Loading branch information
mscolnick committed Jul 23, 2024
1 parent bbe642e commit 33766c9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
9 changes: 9 additions & 0 deletions docs/guides/editor_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ A non-exhausted list of settings:
- Autoreloading/Hot-reloading
- Outputs above or below code cells

## Vim Support

marimo supports vim keybindings. Enable them in the settings menu.

**Additional bindings/features:**

- `gd` - go to definition
- `dd` - when a cell is empty, delete it

## Send feedback

The question mark icon in the panels tray (lower left of the editor) opens a
Expand Down
6 changes: 1 addition & 5 deletions frontend/src/core/codemirror/keymaps/keymaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { logNever } from "@/utils/assertNever";
import { defaultKeymap } from "@codemirror/commands";
import { type Extension, Prec } from "@codemirror/state";
import { type EditorView, keymap } from "@codemirror/view";
import { vim, Vim } from "@replit/codemirror-vim";
import { vim } from "@replit/codemirror-vim";
import { vimKeymapExtension } from "./vim";

export const KEYMAP_PRESETS = ["default", "vim"] as const;
Expand Down Expand Up @@ -33,10 +33,6 @@ export function keymapBundle(
]),
];
case "vim":
// Delete a cell with :dcell
Vim.defineEx("dcell", "dcell", () => {
callbacks.deleteCell();
});
return [
// delete the cell on double press of "d", if the cell is empty
Prec.highest(
Expand Down
15 changes: 14 additions & 1 deletion frontend/src/core/codemirror/keymaps/vim.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Copyright 2024 Marimo. All rights reserved. */

import { getCM, Vim } from "@replit/codemirror-vim";
import { CodeMirror, getCM, Vim } from "@replit/codemirror-vim";
import { type EditorView, keymap, ViewPlugin } from "@codemirror/view";
import {
isAtEndOfEditor,
Expand All @@ -10,11 +10,15 @@ import {
import { invariant } from "@/utils/invariant";
import type { Extension } from "@codemirror/state";
import { Logger } from "@/utils/Logger";
import { goToDefinitionAtCursorPosition } from "../go-to-definition/utils";
import { once } from "@/utils/once";

export function vimKeymapExtension(callbacks: {
focusUp: () => void;
focusDown: () => void;
}): Extension[] {
addCustomVimCommandsOnce();

return [
keymap.of([
{
Expand Down Expand Up @@ -51,6 +55,15 @@ export function vimKeymapExtension(callbacks: {
];
}

const addCustomVimCommandsOnce = once(() => {
// Go to definition
Vim.defineAction("goToDefinition", (cm: CodeMirror) => {
const view = cm.cm6;
return goToDefinitionAtCursorPosition(view);
});
Vim.mapCommand("gd", "action", "goToDefinition", {}, { context: "normal" });
});

class CodeMirrorVimSync {
private instances = new Set<EditorView>();
private isBroadcasting = false;
Expand Down

0 comments on commit 33766c9

Please sign in to comment.