|
1 CodeMirror.registerHelper("fold", "brace", function(cm, start) { |
|
2 var line = start.line, lineText = cm.getLine(line); |
|
3 var startCh, tokenType; |
|
4 |
|
5 function findOpening(openCh) { |
|
6 for (var at = start.ch, pass = 0;;) { |
|
7 var found = at <= 0 ? -1 : lineText.lastIndexOf(openCh, at - 1); |
|
8 if (found == -1) { |
|
9 if (pass == 1) break; |
|
10 pass = 1; |
|
11 at = lineText.length; |
|
12 continue; |
|
13 } |
|
14 if (pass == 1 && found < start.ch) break; |
|
15 tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1)); |
|
16 if (!/^(comment|string)/.test(tokenType)) return found + 1; |
|
17 at = found - 1; |
|
18 } |
|
19 } |
|
20 |
|
21 var startToken = "{", endToken = "}", startCh = findOpening("{"); |
|
22 if (startCh == null) { |
|
23 startToken = "[", endToken = "]"; |
|
24 startCh = findOpening("["); |
|
25 } |
|
26 |
|
27 if (startCh == null) return; |
|
28 var count = 1, lastLine = cm.lastLine(), end, endCh; |
|
29 outer: for (var i = line; i <= lastLine; ++i) { |
|
30 var text = cm.getLine(i), pos = i == line ? startCh : 0; |
|
31 for (;;) { |
|
32 var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos); |
|
33 if (nextOpen < 0) nextOpen = text.length; |
|
34 if (nextClose < 0) nextClose = text.length; |
|
35 pos = Math.min(nextOpen, nextClose); |
|
36 if (pos == text.length) break; |
|
37 if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == tokenType) { |
|
38 if (pos == nextOpen) ++count; |
|
39 else if (!--count) { end = i; endCh = pos; break outer; } |
|
40 } |
|
41 ++pos; |
|
42 } |
|
43 } |
|
44 if (end == null || line == end && endCh == startCh) return; |
|
45 return {from: CodeMirror.Pos(line, startCh), |
|
46 to: CodeMirror.Pos(end, endCh)}; |
|
47 }); |
|
48 CodeMirror.braceRangeFinder = CodeMirror.fold.brace; // deprecated |
|
49 |
|
50 CodeMirror.registerHelper("fold", "import", function(cm, start) { |
|
51 function hasImport(line) { |
|
52 if (line < cm.firstLine() || line > cm.lastLine()) return null; |
|
53 var start = cm.getTokenAt(CodeMirror.Pos(line, 1)); |
|
54 if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1)); |
|
55 if (start.type != "keyword" || start.string != "import") return null; |
|
56 // Now find closing semicolon, return its position |
|
57 for (var i = line, e = Math.min(cm.lastLine(), line + 10); i <= e; ++i) { |
|
58 var text = cm.getLine(i), semi = text.indexOf(";"); |
|
59 if (semi != -1) return {startCh: start.end, end: CodeMirror.Pos(i, semi)}; |
|
60 } |
|
61 } |
|
62 |
|
63 var start = start.line, has = hasImport(start), prev; |
|
64 if (!has || hasImport(start - 1) || ((prev = hasImport(start - 2)) && prev.end.line == start - 1)) |
|
65 return null; |
|
66 for (var end = has.end;;) { |
|
67 var next = hasImport(end.line + 1); |
|
68 if (next == null) break; |
|
69 end = next.end; |
|
70 } |
|
71 return {from: cm.clipPos(CodeMirror.Pos(start, has.startCh + 1)), to: end}; |
|
72 }); |
|
73 CodeMirror.importRangeFinder = CodeMirror.fold["import"]; // deprecated |
|
74 |
|
75 CodeMirror.registerHelper("fold", "include", function(cm, start) { |
|
76 function hasInclude(line) { |
|
77 if (line < cm.firstLine() || line > cm.lastLine()) return null; |
|
78 var start = cm.getTokenAt(CodeMirror.Pos(line, 1)); |
|
79 if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1)); |
|
80 if (start.type == "meta" && start.string.slice(0, 8) == "#include") return start.start + 8; |
|
81 } |
|
82 |
|
83 var start = start.line, has = hasInclude(start); |
|
84 if (has == null || hasInclude(start - 1) != null) return null; |
|
85 for (var end = start;;) { |
|
86 var next = hasInclude(end + 1); |
|
87 if (next == null) break; |
|
88 ++end; |
|
89 } |
|
90 return {from: CodeMirror.Pos(start, has + 1), |
|
91 to: cm.clipPos(CodeMirror.Pos(end))}; |
|
92 }); |
|
93 CodeMirror.includeRangeFinder = CodeMirror.fold.include; // deprecated |