michael@0: CodeMirror.registerGlobalHelper("fold", "comment", function(mode) { michael@0: return mode.blockCommentStart && mode.blockCommentEnd; michael@0: }, function(cm, start) { michael@0: var mode = cm.getModeAt(start), startToken = mode.blockCommentStart, endToken = mode.blockCommentEnd; michael@0: if (!startToken || !endToken) return; michael@0: var line = start.line, lineText = cm.getLine(line); michael@0: michael@0: var startCh; michael@0: for (var at = start.ch, pass = 0;;) { michael@0: var found = at <= 0 ? -1 : lineText.lastIndexOf(startToken, at - 1); michael@0: if (found == -1) { michael@0: if (pass == 1) return; michael@0: pass = 1; michael@0: at = lineText.length; michael@0: continue; michael@0: } michael@0: if (pass == 1 && found < start.ch) return; michael@0: if (/comment/.test(cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1)))) { michael@0: startCh = found + startToken.length; michael@0: break; michael@0: } michael@0: at = found - 1; michael@0: } michael@0: michael@0: var depth = 1, lastLine = cm.lastLine(), end, endCh; michael@0: outer: for (var i = line; i <= lastLine; ++i) { michael@0: var text = cm.getLine(i), pos = i == line ? startCh : 0; michael@0: for (;;) { michael@0: var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos); michael@0: if (nextOpen < 0) nextOpen = text.length; michael@0: if (nextClose < 0) nextClose = text.length; michael@0: pos = Math.min(nextOpen, nextClose); michael@0: if (pos == text.length) break; michael@0: if (pos == nextOpen) ++depth; michael@0: else if (!--depth) { end = i; endCh = pos; break outer; } michael@0: ++pos; michael@0: } michael@0: } michael@0: if (end == null || line == end && endCh == startCh) return; michael@0: return {from: CodeMirror.Pos(line, startCh), michael@0: to: CodeMirror.Pos(end, endCh)}; michael@0: });