browser/devtools/sourceeditor/codemirror/fold/comment-fold.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial