Add vim keys to the solve page's editor
Test and publish the site / test (push) Successful in 51s
Test and publish the site / publish (push) Successful in 1m16s

A Vim checkbox in the toolbar turns on @replit/codemirror-vim, off by
default and remembered in this browser like the split. A status line
under the editor shows the mode and the command being typed, and :w
saves as Save does. Run, Submit and Save keep their Ctrl keys in every
mode, since the page's keymap is at the highest precedence.

The vim extension sits in a Compartment, so turning it on or off keeps
the text and its history; the states kept for the other languages are
brought up to date when they are swapped back in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-20 14:00:29 +01:00
co-authored by Claude Opus 5.5
parent 20d2e05798
commit 2ea9980d58
5 changed files with 93 additions and 1 deletions
+34 -1
View File
@@ -17,10 +17,15 @@
*
* Completions come from basicSetup's autocompletion with sources of our own
* (completions.js), since lang-cpp has none.
*
* Vim keys (@replit/codemirror-vim) sit in a Compartment, unlike the
* language: turning them on or off keeps the text and its history. The
* states put aside for the other languages are brought up to date when
* they are swapped back in.
*/
import { onBeforeUnmount, onMounted, ref, watch } from "vue";
import { basicSetup } from "codemirror";
import { EditorState, Prec } from "@codemirror/state";
import { Compartment, EditorState, Prec } from "@codemirror/state";
import { EditorView, keymap } from "@codemirror/view";
import { indentWithTab } from "@codemirror/commands";
import { HighlightStyle, indentUnit, syntaxHighlighting } from "@codemirror/language";
@@ -28,6 +33,7 @@ import { lintGutter, setDiagnostics } from "@codemirror/lint";
import { cpp } from "@codemirror/lang-cpp";
import { python } from "@codemirror/lang-python";
import { tags as t } from "@lezer/highlight";
import { CodeMirror, vim } from "@replit/codemirror-vim";
import { cppCompletions, pythonCompletions } from "@/completions";
const props = defineProps({
@@ -36,6 +42,7 @@ const props = defineProps({
// The server's: 1-based line/col, endLine/endCol optional.
diagnostics: { type: Array, default: () => [] },
label: { type: String, default: "Code" },
vim: { type: Boolean, default: false },
});
const emit = defineEmits(["update:modelValue", "run", "submit", "save"]);
@@ -107,10 +114,27 @@ const theme = EditorView.theme(
".cm-completionIcon": { opacity: "0.55" },
".cm-tooltip.cm-completionInfo": { fontFamily: "var(--font-mono)", fontSize: "0.8rem", padding: "4px 8px" },
".cm-snippetField": { backgroundColor: "rgba(0, 0, 0, 0.05)" },
// The vim extension's own theme is at the highest precedence, so these
// win on specificity (the cursor layer) instead.
".cm-vimCursorLayer .cm-fat-cursor": { background: "#9ec5f4", color: "var(--color-ink)" },
"&:not(.cm-focused) .cm-vimCursorLayer .cm-fat-cursor": { background: "none", outline: "1px solid #5598e7" },
".cm-panels-bottom": { borderTop: "1px solid var(--color-line-soft)" },
".cm-vim-panel": { fontFamily: "var(--font-mono)", fontSize: "0.8rem", padding: "2px 12px" },
".cm-vim-panel input": { fontFamily: "inherit", fontSize: "inherit", color: "var(--color-ink)" },
},
{ dark: false },
);
// The status line shows the mode and the command being typed; vim must
// come before basicSetup's keymaps, which then work in insert mode.
const vimKeys = new Compartment();
const vimExtension = (on) => (on ? vim({ status: true }) : []);
// :w is vim's save, which calls CodeMirror.commands.save if there is one.
// It is global, so it finds the editor's own handler through the view.
const savers = new WeakMap();
CodeMirror.commands.save = (cm) => savers.get(cm.cm6)?.();
// The page also listens for these on the window, for when the editor is not
// focused; stopping them here keeps one press from running twice, and keeps
// basicSetup's Mod-Enter (insert a blank line) out of the way.
@@ -131,6 +155,7 @@ function newState(doc, lang) {
doc,
extensions: [
pageKeys,
vimKeys.of(vimExtension(props.vim)),
basicSetup,
keymap.of([indentWithTab]),
(LANGS[lang] || (() => []))(),
@@ -174,6 +199,7 @@ function pushDiagnostics() {
onMounted(() => {
view = new EditorView({ state: newState(props.modelValue, props.lang), parent: host.value });
savers.set(view, () => emit("save"));
states.set(props.lang, view.state);
});
onBeforeUnmount(() => view?.destroy());
@@ -190,6 +216,7 @@ watch(
states.set(oldLang, view.state);
const kept = states.get(lang);
view.setState(kept && kept.doc.toString() === value ? kept : newState(value, lang));
syncVim();
pushDiagnostics();
} else if (value !== view.state.doc.toString()) {
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: value } });
@@ -198,6 +225,12 @@ watch(
);
watch(() => props.diagnostics, () => view && pushDiagnostics());
function syncVim() {
const on = vimKeys.get(view.state).length > 0;
if (on !== props.vim) view.dispatch({ effects: vimKeys.reconfigure(vimExtension(props.vim)) });
}
watch(() => props.vim, () => view && syncVim());
// Puts the cursor at the start of a line's code and shows it.
function goto(line) {
if (!view) return;