Skip to content
This repository was archived by the owner on Jun 27, 2018. It is now read-only.

Commit f9e2f05

Browse files
committed
workaround: single quote autopairing
1 parent 3762b03 commit f9e2f05

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

static/web.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,42 @@
734734
bindKey: {win: "Ctrl-Enter", mac: "Ctrl-Enter"}
735735
});
736736

737+
// ACE uses the "cstyle" behaviour for all languages by default, which
738+
// gives us nice things like quote and paren autopairing. However this
739+
// also autopairs single quotes, which makes writing lifetimes annoying.
740+
// To avoid having to duplicate the other functionality provided by the
741+
// cstyle behaviour, we work around this situation by hijacking the
742+
// single quote as a hotkey and modifying the document ourselves, which
743+
// does not trigger this behaviour.
744+
editor.commands.addCommand({
745+
name: "rust_no_single_quote_autopairing",
746+
exec: function(editor, line) {
747+
var sess = editor.getSession();
748+
var doc = sess.getDocument();
749+
var selection = sess.getSelection();
750+
var ranges = selection.getAllRanges();
751+
var prev_range = null;
752+
753+
// no selection = zero width range, so we don't need to handle this case specially
754+
// start from the back, so changes to earlier ranges don't invalidate later ranges
755+
for (var i = ranges.length - 1; i >= 0; i--) {
756+
// sanity check: better to do no modification than to do something wrong
757+
// see the compareRange docs:
758+
// https://github.com/ajaxorg/ace/blob/v1.2.6/lib/ace/range.js#L106-L120
759+
if (prev_range && prev_range.compareRange(ranges[i]) != -2) {
760+
console.log("ranges intersect or are not in ascending order, skipping",
761+
ranges[i]);
762+
}
763+
prev_range = ranges[i];
764+
765+
doc.replace(ranges[i], "'");
766+
}
767+
// the selection contents were replaced, so clear the selection
768+
selection.clearSelection();
769+
},
770+
bindKey: {win: "'", mac: "'"},
771+
});
772+
737773
// We’re all pretty much agreed that such an obscure command as transposing
738774
// letters hogging Ctrl-T, normally “open new tab”, is a bad thing.
739775
var transposeletters = editor.commands.commands.transposeletters;

0 commit comments

Comments
 (0)