Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / codemirror / addon / fold / comment-fold.js
1 (function(mod) {
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
7 mod(CodeMirror);
8 })(function(CodeMirror) {
9 "use strict";
10
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);
17
18 var startCh;
19 for (var at = start.ch, pass = 0;;) {
20 var found = at <= 0 ? -1 : lineText.lastIndexOf(startToken, at - 1);
21 if (found == -1) {
22 if (pass == 1) return;
23 pass = 1;
24 at = lineText.length;
25 continue;
26 }
27 if (pass == 1 && found < start.ch) return;
28 if (/comment/.test(cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1)))) {
29 startCh = found + startToken.length;
30 break;
31 }
32 at = found - 1;
33 }
34
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;
38 for (;;) {
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; }
46 ++pos;
47 }
48 }
49 if (end == null || line == end && endCh == startCh) return;
50 return {from: CodeMirror.Pos(line, startCh),
51 to: CodeMirror.Pos(end, endCh)};
52 });
53
54 });