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

Commit b63a21e

Browse files
authored
Merge pull request #266 from TimNN/hijack-single-quote
Hijack single quote (and update ace again)
2 parents a8344ce + d20f6d6 commit b63a21e

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

static/web.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
<!-- web.css and web.js need cache busting; increment the numbers when you make changes -->
1111
<link rel="stylesheet" href="web.css?5">
1212
<link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"/>
13-
<script src="https://cdn.jsdelivr.net/ace/1.2.3/min/ace.js"
14-
integrity="sha512-lp7lBcKlkYx0jlja+DwHOHRk8LPaxvP8MuTlxAFgOpUYNib+59G1IwsxLBIFYwrkxYaBmn/oF9TWtDSEY5Ph+w=="
13+
<script src="https://cdn.jsdelivr.net/ace/1.2.6/min/ace.js"
14+
integrity="sha512-Y/M43OHLyvSuqF2JuQU9RIerW7fH7i/h/7AokQPIXhoYiHIxKXYDCWcuyRSXiVCGGtnfYoIBklTZ4/t26Lrr/A=="
1515
crossorigin="anonymous"
1616
charset="utf-8"></script>
17-
<script src="https://cdn.jsdelivr.net/ace/1.2.3/min/ext-themelist.js"
18-
integrity="sha512-F7PRyMnRbqZEM6kS5YAvxekuwessnK2eIJ3hnxTqpD+5e8lz5K6aa4pH0puO2Gip1CYgCHjvdgXS5+64lsAXog=="
17+
<script src="https://cdn.jsdelivr.net/ace/1.2.6/min/ext-themelist.js"
18+
integrity="sha512-ZdrBduTZxDAOqGKxKVuFtIVU0wD82q0HXnUEj8/KBHZ9DbQuuAjfMZ7yyBnh3vzkOtMQex9jj8EuypRD+KGNSA=="
1919
crossorigin="anonymous"
2020
charset="utf-8"></script>
21-
<script src="web.js?12"></script>
21+
<script src="web.js?13"></script>
2222
</head>
2323
<body>
2424
<form id="control">

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)