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

add vim mode to the editor #59

Open
wants to merge 1 commit into
base: 2024
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ build/web/js/require.js: node_modules/requirejs/require.js
build/web/js/codemirror.js: $(CM)/lib/codemirror.js
cp $< $@

build/web/js/codemirror-vim.js: $(CM)/keymap/vim.js
cp $< $@

build/web/js/codemirror-matchbrackets.js: $(CM)/addon/edit/matchbrackets.js
cp $< $@

build/web/js/rulers.js: $(CM)/addon/display/rulers.js
cp $< $@

Expand Down Expand Up @@ -218,6 +224,8 @@ MISC_JS = build/web/js/q.js \
build/web/js/url.js \
build/web/js/require.js \
build/web/js/codemirror.js \
build/web/js/codemirror-vim.js \
build/web/js/codemirror-matchbrackets.js \
build/web/js/rulers.js \
build/web/js/mark-selection.js \
build/web/js/pyret-mode.js \
Expand All @@ -244,11 +252,13 @@ MISC_JS = build/web/js/q.js \
build/web/js/lifecycle.js \
build/web/js/jszip.js \
build/web/js/FileSaver.js


EDITOR_MISC_JS = build/web/js/q.js \
build/web/js/loader.js \
build/web/js/codemirror.js \
build/web/js/codemirror-vim.js \
build/web/js/codemirror-matchbrackets.js \
build/web/js/rulers.js \
build/web/js/scrollpastend.js \
build/web/js/foldcode.js \
Expand Down
9 changes: 9 additions & 0 deletions src/web/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<body class="default">
<script>
var themeOnLoad = localSettings.getItem('theme') || 'default';
var keybindOnLoad = localSettings.getItem('keybind') || 'default';
document.body.classList.remove("default");
document.body.classList.add(themeOnLoad);
var params = {};
Expand Down Expand Up @@ -66,6 +67,7 @@
}
window.addEventListener('load', function() {
document.getElementById('theme-select').value = themeOnLoad;
document.getElementById('keybind-select').value = keybindOnLoad;
}, { once: true });
</script>
<main>
Expand Down Expand Up @@ -207,6 +209,13 @@ <h2 id="menutitle" class="screenreader-only">Navigation Controls</h2>
<option value="high-contrast-dark">High Contrast Dark</option>
</select>
</li>
<li id="keybinds" role="presentation" style="white-space: nowrap;">
<label for="keybind-select">Key bindings:</label>
<select id="keybind-select">
<option value="default">Default</option>
<option value="vim">Vim</option>
</select>
</li>
</ul>
</li>

Expand Down
15 changes: 10 additions & 5 deletions src/web/js/beforePyret.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,23 @@ $(function() {
const mac = CodeMirror.keyMap.default === CodeMirror.keyMap.macDefault;
const modifier = mac ? "Cmd" : "Ctrl";

var cmOptions = {
extraKeys: CodeMirror.normalizeKeyMap({
var defaultKeyMap = CodeMirror.normalizeKeyMap({
"Shift-Enter": function(cm) { runFun(sourceAPI.get_loaded("definitions://").contents); },
"Shift-Ctrl-Enter": function(cm) { runFun(sourceAPI.get_loaded("definitions://")); },
"Tab": "indentAuto",
"Ctrl-I": reindentAllLines,
"Esc Left": "goBackwardSexp",
"Alt-Left": "goBackwardSexp",
"Esc Right": "goForwardSexp",
"Alt-Right": "goForwardSexp",
"Ctrl-Left": "goBackwardToken",
"Ctrl-Right": "goForwardToken",
[`${modifier}-/`]: "toggleComment",
}),
});
CPO.noVimKeyMap = CodeMirror.normalizeKeyMap({
"Esc Left": "goBackwardSexp",
"Esc Right": "goForwardSexp",
});
var cmOptions = {
extraKeys: defaultKeyMap,
indentUnit: 2,
tabSize: 2,
viewportMargin: Infinity,
Expand All @@ -311,6 +314,8 @@ $(function() {
cmOptions = merge(cmOptions, options.cmOptions || {});

var CM = CodeMirror.fromTextArea(textarea[0], cmOptions);
// we do this separately so we can more easily add and remove it for vim mode
CM.addKeyMap(CPO.noVimKeyMap);

function firstLineIsNamespace() {
const firstline = CM.getLine(0);
Expand Down
30 changes: 30 additions & 0 deletions src/web/js/cpo-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,36 @@
applyTheme(newTheme);
});

var curKeybind = document.getElementById("keybind-select").value;
var keybindSelect = $("#keybind-select");

function applyKeybind(keybinding) {
var cm = CPO.editor.cm;
cm.state.keyMaps = [];
if (keybinding !== 'vim') {
cm.addKeyMap(CPO.noVimKeyMap, true);
}
curKeybind = keybinding;
cm.setOption('keyMap', curKeybind);
}

if (localSettings.getItem('keybind') !== null) {
applyKeybind(localSettings.getItem('keybind'));
} else {
localSettings.setItem('keybind', curKeybind);
}

$("#keybinds").change(function(e) {
var value = e.target.value;
applyKeybind(value);

localSettings.setItem("keybind", curKeybind);
});

localSettings.change("keybind", function(_, newKeybinds) {
applyKeybind(newKeybinds);
});

$('.notificationArea').click(function() {$('.notificationArea span').fadeOut(1000);});

editor.cm.on('beforeChange', function(instance, changeObj){textHandlers.autoCorrect(instance, changeObj, editor.cm);});
Expand Down