Skip to content

Commit 0c5c59c

Browse files
hanzhaodengmarijnh
authored andcommitted
[closebrackets addon] More refined check for when to close a bracket
Improves the current behavior which auto closes every opening bracket. - hardcodes a closeBefore list of characters that if the next character is in the list, opening a bracket should be auto-closed. The list is : ")]}'\":;>" Basically the closing characters in pairs ; : and >. - when opening a bracket, it will be auto-closed if: 1. there is no next character, 2. the next character is a white space character. 3. the next character is in the shouldClose list.
1 parent 17fa1ea commit 0c5c59c

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

addon/edit/closebrackets.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
})(function(CodeMirror) {
1212
var defaults = {
1313
pairs: "()[]{}''\"\"",
14+
closeBefore: ")]}'\":;>",
1415
triples: "",
1516
explode: "[]{}"
1617
};
@@ -109,6 +110,9 @@
109110
var pairs = getOption(conf, "pairs");
110111
var pos = pairs.indexOf(ch);
111112
if (pos == -1) return CodeMirror.Pass;
113+
114+
var closeBefore = getOption(conf,"closeBefore");
115+
112116
var triples = getOption(conf, "triples");
113117

114118
var identical = pairs.charAt(pos + 1) == ch;
@@ -136,7 +140,7 @@
136140
var prev = cur.ch == 0 ? " " : cm.getRange(Pos(cur.line, cur.ch - 1), cur)
137141
if (!CodeMirror.isWordChar(next) && prev != ch && !CodeMirror.isWordChar(prev)) curType = "both";
138142
else return CodeMirror.Pass;
139-
} else if (opening) {
143+
} else if (opening && (next.length === 0 || /\s/.test(next) || closeBefore.indexOf(next) > -1)) {
140144
curType = "both";
141145
} else {
142146
return CodeMirror.Pass;

0 commit comments

Comments
 (0)