Skip to content

add support for vim in the editor #551

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: horizon
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
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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 @@ -209,6 +215,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 Down Expand Up @@ -236,6 +244,8 @@ MISC_JS = build/web/js/q.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 @@ -29,6 +29,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 @@ -64,6 +65,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 @@ -205,6 +207,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
16 changes: 11 additions & 5 deletions src/web/js/beforePyret.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,24 @@ $(function() {
const mac = CodeMirror.keyMap.default === CodeMirror.keyMap.macDefault;
const modifier = mac ? "Cmd" : "Ctrl";

var cmOptions = {
extraKeys: CodeMirror.normalizeKeyMap({
defaultKeyMap = CodeMirror.normalizeKeyMap({
"Shift-Enter": function(cm) { runFun(cm.getValue()); },
"Shift-Ctrl-Enter": function(cm) { runFun(cm.getValue()); },
"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({
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know if there is a better place to store the non-vim specific keymap than in CPO :)

"Esc Left": "goBackwardSexp",
"Esc Right": "goForwardSexp",
});

var cmOptions = {
extraKeys: defaultKeyMap,
indentUnit: 2,
tabSize: 2,
viewportMargin: Infinity,
Expand All @@ -221,6 +225,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 @@ -498,6 +498,36 @@
localSettings.change("theme", function(_, newTheme) {
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);});

Expand Down