Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vim keymap precedence (fix enter in insert mode) #3395

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading