Skip to content

Commit

Permalink
fix: vim keymap precedence (fix enter in insert mode) (#3395)
Browse files Browse the repository at this point in the history
This is a better and more correct solution
  • Loading branch information
mscolnick authored Jan 10, 2025
1 parent 8c53055 commit 9c07b3e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ exports[`snapshot all duplicate keymaps > vim keymaps 2`] = `
"shift": "deleteCharBackward",
},
],
"Enter": [
{
"key": "Enter",
"run": "acceptCompletion",
},
{
"key": "Enter",
"run": "<no name>",
"shift": "<no name>",
},
],
"Escape": [
{
"key": "Escape",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/core/codemirror/__tests__/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe("snapshot all duplicate keymaps", () => {
);
// Total duplicates:
// if this changes, please make sure to validate they are not conflicting
expect(Object.values(duplicates).flat().length).toMatchInlineSnapshot("17");
expect(Object.values(duplicates).flat().length).toMatchInlineSnapshot("19");
expect(duplicates).toMatchSnapshot();
});
});
Expand Down
23 changes: 4 additions & 19 deletions frontend/src/core/codemirror/keymaps/keymaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { type Extension, Prec } from "@codemirror/state";
import { type EditorView, keymap } from "@codemirror/view";
import { vim } from "@replit/codemirror-vim";
import { vimKeymapExtension } from "./vim";
import { once } from "@/utils/once";

export const KEYMAP_PRESETS = ["default", "vim"] as const;

Expand Down Expand Up @@ -35,8 +34,9 @@ export function keymapBundle(
];
case "vim":
return [
keymap.of(defaultKeymap),
// delete the cell on double press of "d", if the cell is empty
Prec.highest(
Prec.high(
doubleCharacterListener(
"d",
(view) => view.state.doc.toString() === "",
Expand All @@ -49,31 +49,16 @@ export function keymapBundle(
},
),
),
keymap.of(defaultVimKeymap()),
vim({ status: false }),
Prec.highest(vim({ status: false })),
// Needs to come after the vim extension
vimKeymapExtension(callbacks),
Prec.highest(vimKeymapExtension(callbacks)),
];
default:
logNever(config.preset);
return [];
}
}

const defaultVimKeymap = once(() => {
const toRemove = new Set(["Enter", "Ctrl-v", "ArrowLeft", "ArrowRight"]);
// Remove conflicting keys from the keymap
// Enter (<CR>) adds a new line
// - it should just go to the next line
// Ctrl-v goes to the bottom of the cell
// - should enter blockwise visual mode
// ArrowLeft/ArrowRight exit blockwise visual mode
// - should keep blockwise, but continue with cursor movement
return defaultKeymap.filter(
(k) => !toRemove.has(k.key || k.mac || k.linux || k.win || ""),
);
});

/**
* Listen for a double keypress of a character and call a callback.
*/
Expand Down

0 comments on commit 9c07b3e

Please sign in to comment.