|
11 | 11 | })(function(CodeMirror) { |
12 | 12 | "use strict"; |
13 | 13 |
|
14 | | -CodeMirror.registerHelper("fold", "brace", function(cm, start) { |
15 | | - var line = start.line, lineText = cm.getLine(line); |
16 | | - var tokenType; |
| 14 | +function bracketFolding(pairs) { |
| 15 | + return function(cm, start) { |
| 16 | + var line = start.line, lineText = cm.getLine(line); |
17 | 17 |
|
18 | | - function findOpening(openCh) { |
19 | | - for (var at = start.ch, pass = 0;;) { |
20 | | - var found = at <= 0 ? -1 : lineText.lastIndexOf(openCh, at - 1); |
21 | | - if (found == -1) { |
22 | | - if (pass == 1) break; |
23 | | - pass = 1; |
24 | | - at = lineText.length; |
25 | | - continue; |
| 18 | + function findOpening(pair) { |
| 19 | + var tokenType; |
| 20 | + for (var at = start.ch, pass = 0;;) { |
| 21 | + var found = at <= 0 ? -1 : lineText.lastIndexOf(pair[0], at - 1); |
| 22 | + if (found == -1) { |
| 23 | + if (pass == 1) break; |
| 24 | + pass = 1; |
| 25 | + at = lineText.length; |
| 26 | + continue; |
| 27 | + } |
| 28 | + if (pass == 1 && found < start.ch) break; |
| 29 | + tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1)); |
| 30 | + if (!/^(comment|string)/.test(tokenType)) return {ch: found + 1, tokenType: tokenType, pair: pair}; |
| 31 | + at = found - 1; |
26 | 32 | } |
27 | | - if (pass == 1 && found < start.ch) break; |
28 | | - tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1)); |
29 | | - if (!/^(comment|string)/.test(tokenType)) return found + 1; |
30 | | - at = found - 1; |
31 | 33 | } |
32 | | - } |
33 | | - |
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 | 34 |
|
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; } |
| 35 | + function findRange(found) { |
| 36 | + var count = 1, lastLine = cm.lastLine(), end, startCh = found.ch, endCh |
| 37 | + outer: for (var i = line; i <= lastLine; ++i) { |
| 38 | + var text = cm.getLine(i), pos = i == line ? startCh : 0; |
| 39 | + for (;;) { |
| 40 | + var nextOpen = text.indexOf(found.pair[0], pos), nextClose = text.indexOf(found.pair[1], pos); |
| 41 | + if (nextOpen < 0) nextOpen = text.length; |
| 42 | + if (nextClose < 0) nextClose = text.length; |
| 43 | + pos = Math.min(nextOpen, nextClose); |
| 44 | + if (pos == text.length) break; |
| 45 | + if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == found.tokenType) { |
| 46 | + if (pos == nextOpen) ++count; |
| 47 | + else if (!--count) { end = i; endCh = pos; break outer; } |
| 48 | + } |
| 49 | + ++pos; |
| 50 | + } |
56 | 51 | } |
57 | | - ++pos; |
| 52 | + |
| 53 | + if (end == null || line == end) return null |
| 54 | + return {from: CodeMirror.Pos(line, startCh), |
| 55 | + to: CodeMirror.Pos(end, endCh)}; |
| 56 | + } |
| 57 | + |
| 58 | + var found = [] |
| 59 | + for (var i = 0; i < pairs.length; i++) { |
| 60 | + var open = findOpening(pairs[i]) |
| 61 | + if (open) found.push(open) |
| 62 | + } |
| 63 | + found.sort(function(a, b) { return a.ch - b.ch }) |
| 64 | + for (var i = 0; i < found.length; i++) { |
| 65 | + var range = findRange(found[i]) |
| 66 | + if (range) return range |
58 | 67 | } |
| 68 | + return null |
59 | 69 | } |
60 | | - if (end == null || line == end) return; |
61 | | - return {from: CodeMirror.Pos(line, startCh), |
62 | | - to: CodeMirror.Pos(end, endCh)}; |
63 | | -}); |
| 70 | +} |
| 71 | + |
| 72 | +CodeMirror.registerHelper("fold", "brace", bracketFolding([["{", "}"], ["[", "]"]])); |
| 73 | + |
| 74 | +CodeMirror.registerHelper("fold", "brace-paren", bracketFolding([["{", "}"], ["[", "]"], ["(", ")"]])); |
64 | 75 |
|
65 | 76 | CodeMirror.registerHelper("fold", "import", function(cm, start) { |
66 | 77 | function hasImport(line) { |
|
0 commit comments