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