Skip to content

Commit aaba815

Browse files
committed
[brace-fold addon] Fix confusion when there are both braces and brackets on a line
Closes #6847
1 parent b7bae95 commit aaba815

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

addon/fold/brace-fold.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
CodeMirror.registerHelper("fold", "brace", function(cm, start) {
1515
var line = start.line, lineText = cm.getLine(line);
16-
var tokenType;
1716

1817
function findOpening(openCh) {
18+
var tokenType;
1919
for (var at = start.ch, pass = 0;;) {
2020
var found = at <= 0 ? -1 : lineText.lastIndexOf(openCh, at - 1);
2121
if (found == -1) {
@@ -26,40 +26,42 @@ CodeMirror.registerHelper("fold", "brace", function(cm, start) {
2626
}
2727
if (pass == 1 && found < start.ch) break;
2828
tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1));
29-
if (!/^(comment|string)/.test(tokenType)) return found + 1;
29+
if (!/^(comment|string)/.test(tokenType)) return {ch: found + 1, tokenType: tokenType};
3030
at = found - 1;
3131
}
3232
}
3333

34-
var startBrace = findOpening("{"), startBracket = findOpening("[")
35-
var startToken, endToken, startCh
36-
if (startBrace != null && (startBracket == null || startBracket > startBrace)) {
37-
startCh = startBrace; startToken = "{"; endToken = "}"
38-
} else if (startBracket != null) {
39-
startCh = startBracket; startToken = "["; endToken = "]"
40-
} else {
41-
return
42-
}
43-
44-
var count = 1, lastLine = cm.lastLine(), end, endCh;
45-
outer: for (var i = line; i <= lastLine; ++i) {
46-
var text = cm.getLine(i), pos = i == line ? startCh : 0;
47-
for (;;) {
48-
var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos);
49-
if (nextOpen < 0) nextOpen = text.length;
50-
if (nextClose < 0) nextClose = text.length;
51-
pos = Math.min(nextOpen, nextClose);
52-
if (pos == text.length) break;
53-
if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == tokenType) {
54-
if (pos == nextOpen) ++count;
55-
else if (!--count) { end = i; endCh = pos; break outer; }
34+
function findRange(startToken, endToken, found) {
35+
var count = 1, lastLine = cm.lastLine(), end, startCh = found.ch, endCh
36+
outer: for (var i = line; i <= lastLine; ++i) {
37+
var text = cm.getLine(i), pos = i == line ? startCh : 0;
38+
for (;;) {
39+
var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos);
40+
if (nextOpen < 0) nextOpen = text.length;
41+
if (nextClose < 0) nextClose = text.length;
42+
pos = Math.min(nextOpen, nextClose);
43+
if (pos == text.length) break;
44+
if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == found.tokenType) {
45+
if (pos == nextOpen) ++count;
46+
else if (!--count) { end = i; endCh = pos; break outer; }
47+
}
48+
++pos;
5649
}
57-
++pos;
5850
}
51+
52+
if (end == null || line == end) return null
53+
return {from: CodeMirror.Pos(line, startCh),
54+
to: CodeMirror.Pos(end, endCh)};
55+
}
56+
57+
var startBrace = findOpening("{"), startBracket = findOpening("[")
58+
if (startBrace && (!startBracket || startBracket.ch > startBrace.ch)) {
59+
return findRange("{", "}", startBrace) || (startBracket && findRange("[", "]", startBracket))
60+
} else if (startBracket) {
61+
return findRange("[", "]", startBracket) || (startBrace && findRange("{", "}", startBrace))
62+
} else {
63+
return null
5964
}
60-
if (end == null || line == end) return;
61-
return {from: CodeMirror.Pos(line, startCh),
62-
to: CodeMirror.Pos(end, endCh)};
6365
});
6466

6567
CodeMirror.registerHelper("fold", "import", function(cm, start) {

0 commit comments

Comments
 (0)