|
1 CodeMirror.registerGlobalHelper("fold", "comment", function(mode) { |
|
2 return mode.blockCommentStart && mode.blockCommentEnd; |
|
3 }, function(cm, start) { |
|
4 var mode = cm.getModeAt(start), startToken = mode.blockCommentStart, endToken = mode.blockCommentEnd; |
|
5 if (!startToken || !endToken) return; |
|
6 var line = start.line, lineText = cm.getLine(line); |
|
7 |
|
8 var startCh; |
|
9 for (var at = start.ch, pass = 0;;) { |
|
10 var found = at <= 0 ? -1 : lineText.lastIndexOf(startToken, at - 1); |
|
11 if (found == -1) { |
|
12 if (pass == 1) return; |
|
13 pass = 1; |
|
14 at = lineText.length; |
|
15 continue; |
|
16 } |
|
17 if (pass == 1 && found < start.ch) return; |
|
18 if (/comment/.test(cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1)))) { |
|
19 startCh = found + startToken.length; |
|
20 break; |
|
21 } |
|
22 at = found - 1; |
|
23 } |
|
24 |
|
25 var depth = 1, lastLine = cm.lastLine(), end, endCh; |
|
26 outer: for (var i = line; i <= lastLine; ++i) { |
|
27 var text = cm.getLine(i), pos = i == line ? startCh : 0; |
|
28 for (;;) { |
|
29 var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos); |
|
30 if (nextOpen < 0) nextOpen = text.length; |
|
31 if (nextClose < 0) nextClose = text.length; |
|
32 pos = Math.min(nextOpen, nextClose); |
|
33 if (pos == text.length) break; |
|
34 if (pos == nextOpen) ++depth; |
|
35 else if (!--depth) { end = i; endCh = pos; break outer; } |
|
36 ++pos; |
|
37 } |
|
38 } |
|
39 if (end == null || line == end && endCh == startCh) return; |
|
40 return {from: CodeMirror.Pos(line, startCh), |
|
41 to: CodeMirror.Pos(end, endCh)}; |
|
42 }); |