Add vim keys to the solve page's editor
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:
Generated
+23
@@ -17,6 +17,7 @@
|
||||
"@codemirror/state": "^6.7.6",
|
||||
"@codemirror/view": "^6.43.13",
|
||||
"@lezer/highlight": "^1.2.5",
|
||||
"@replit/codemirror-vim": "^6.4.0",
|
||||
"@vscode/markdown-it-katex": "^1.1.2",
|
||||
"codemirror": "^6.0.2",
|
||||
"dompurify": "^3.4.17",
|
||||
@@ -749,6 +750,28 @@
|
||||
"node": "^22.20 || ^24.12 || >=25"
|
||||
}
|
||||
},
|
||||
"node_modules/@replit/codemirror-vim": {
|
||||
"version": "6.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@replit/codemirror-vim/-/codemirror-vim-6.4.0.tgz",
|
||||
"integrity": "sha512-t9UMDNhkmeAkl0uRbiJVotv97bGD6mf4GyJJoEbrjUqa/Pov0s9eL+4AaI0Xto3OGri17i9qwIpT0JbVlVXt8A==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@replit/codemirror-vim-core": "^0.1.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@codemirror/commands": "6.x.x",
|
||||
"@codemirror/language": "6.x.x",
|
||||
"@codemirror/search": "6.x.x",
|
||||
"@codemirror/state": "6.x.x",
|
||||
"@codemirror/view": "6.x.x"
|
||||
}
|
||||
},
|
||||
"node_modules/@replit/codemirror-vim-core": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@replit/codemirror-vim-core/-/codemirror-vim-core-0.1.0.tgz",
|
||||
"integrity": "sha512-1i6EBKpcNfDKvTmTh6N6g9lL6udD5t+uFNh4JCqozRnVlvUGOps7h/QzS2ne4zcvPUjvApKpmcP7Grc3fNbZiQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@rolldown/pluginutils": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.0.1.tgz",
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
"@codemirror/state": "^6.7.6",
|
||||
"@codemirror/view": "^6.43.13",
|
||||
"@lezer/highlight": "^1.2.5",
|
||||
"@replit/codemirror-vim": "^6.4.0",
|
||||
"@vscode/markdown-it-katex": "^1.1.2",
|
||||
"codemirror": "^6.0.2",
|
||||
"dompurify": "^3.4.17",
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -414,6 +414,29 @@ onUnmounted(() => {
|
||||
flushDraft();
|
||||
});
|
||||
|
||||
// ---- vim keys ----
|
||||
// Off by default, and remembered in this browser like the split.
|
||||
const VIM_KEY = "usaco:solve:vim";
|
||||
const vimMode = ref(readVim());
|
||||
|
||||
function readVim() {
|
||||
try {
|
||||
return localStorage.getItem(VIM_KEY) === "1";
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
watch(vimMode, (on) => {
|
||||
try {
|
||||
if (on) localStorage.setItem(VIM_KEY, "1");
|
||||
else localStorage.removeItem(VIM_KEY);
|
||||
} catch {
|
||||
// As in readDraft.
|
||||
}
|
||||
editor.value?.focus();
|
||||
});
|
||||
|
||||
// ---- the divider between the panes ----
|
||||
// A share of the width, remembered in this browser.
|
||||
const SPLIT_KEY = "usaco:solve:split";
|
||||
@@ -543,6 +566,9 @@ watch(() => props.name, load, { immediate: true });
|
||||
<button class="btn" type="button" :disabled="busy || !dirty" @click="reset">
|
||||
Reset to {{ current.saved ? "saved" : "starter" }}
|
||||
</button>
|
||||
<label class="check" title="Vim keys in the editor (:w saves)">
|
||||
<input v-model="vimMode" type="checkbox" /> Vim
|
||||
</label>
|
||||
<span class="grow"></span>
|
||||
<button
|
||||
class="btn"
|
||||
@@ -601,6 +627,7 @@ watch(() => props.name, load, { immediate: true });
|
||||
:lang="lang"
|
||||
:diagnostics="editorDiagnostics"
|
||||
:label="`${LANG_LABELS[lang] || lang} solution`"
|
||||
:vim="vimMode"
|
||||
@run="run('custom')"
|
||||
@submit="run('tests')"
|
||||
@save="save"
|
||||
@@ -753,6 +780,13 @@ h2 {
|
||||
opacity: 0.5;
|
||||
cursor: default;
|
||||
}
|
||||
.check {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
font-size: 0.9rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
.grow {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user