2 if (typeof exports
== "object" && typeof module
== "object") // CommonJS
3 mod(require("../../lib/codemirror"));
4 else if (typeof define
== "function" && define
.amd
) // AMD
5 define(["../../lib/codemirror"], mod
);
6 else // Plain browser env
8 })(function(CodeMirror
) {
11 CodeMirror
.registerGlobalHelper("fold", "comment", function(mode
) {
12 return mode
.blockCommentStart
&& mode
.blockCommentEnd
;
13 }, function(cm
, start
) {
14 var mode
= cm
.getModeAt(start
), startToken
= mode
.blockCommentStart
, endToken
= mode
.blockCommentEnd
;
15 if (!startToken
|| !endToken
) return;
16 var line
= start
.line
, lineText
= cm
.getLine(line
);
19 for (var at
= start
.ch
, pass
= 0;;) {
20 var found
= at
<= 0 ? -1 : lineText
.lastIndexOf(startToken
, at
- 1);
22 if (pass
== 1) return;
27 if (pass
== 1 && found
< start
.ch
) return;
28 if (/comment/.test(cm
.getTokenTypeAt(CodeMirror
.Pos(line
, found
+ 1)))) {
29 startCh
= found
+ startToken
.length
;
35 var depth
= 1, lastLine
= cm
.lastLine(), end
, endCh
;
36 outer
: for (var i
= line
; i
<= lastLine
; ++i
) {
37 var text
= cm
.getLine(i
), pos
= i
== line
? startCh
: 0;
39 var nextOpen
= text
.indexOf(startToken
, pos
), nextClose
= text
.indexOf(endToken
, pos
);
40 if (nextOpen
< 0) nextOpen
= text
.length
;
41 if (nextClose
< 0) nextClose
= text
.length
;
42 pos
= Math
.min(nextOpen
, nextClose
);
43 if (pos
== text
.length
) break;
44 if (pos
== nextOpen
) ++depth
;
45 else if (!--depth
) { end
= i
; endCh
= pos
; break outer
; }
49 if (end
== null || line
== end
&& endCh
== startCh
) return;
50 return {from: CodeMirror
.Pos(line
, startCh
),
51 to
: CodeMirror
.Pos(end
, endCh
)};