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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/sourceeditor/codemirror/fold/comment-fold.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,42 @@
     1.4 +CodeMirror.registerGlobalHelper("fold", "comment", function(mode) {
     1.5 +  return mode.blockCommentStart && mode.blockCommentEnd;
     1.6 +}, function(cm, start) {
     1.7 +  var mode = cm.getModeAt(start), startToken = mode.blockCommentStart, endToken = mode.blockCommentEnd;
     1.8 +  if (!startToken || !endToken) return;
     1.9 +  var line = start.line, lineText = cm.getLine(line);
    1.10 +
    1.11 +  var startCh;
    1.12 +  for (var at = start.ch, pass = 0;;) {
    1.13 +    var found = at <= 0 ? -1 : lineText.lastIndexOf(startToken, at - 1);
    1.14 +    if (found == -1) {
    1.15 +      if (pass == 1) return;
    1.16 +      pass = 1;
    1.17 +      at = lineText.length;
    1.18 +      continue;
    1.19 +    }
    1.20 +    if (pass == 1 && found < start.ch) return;
    1.21 +    if (/comment/.test(cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1)))) {
    1.22 +      startCh = found + startToken.length;
    1.23 +      break;
    1.24 +    }
    1.25 +    at = found - 1;
    1.26 +  }
    1.27 +
    1.28 +  var depth = 1, lastLine = cm.lastLine(), end, endCh;
    1.29 +  outer: for (var i = line; i <= lastLine; ++i) {
    1.30 +    var text = cm.getLine(i), pos = i == line ? startCh : 0;
    1.31 +    for (;;) {
    1.32 +      var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos);
    1.33 +      if (nextOpen < 0) nextOpen = text.length;
    1.34 +      if (nextClose < 0) nextClose = text.length;
    1.35 +      pos = Math.min(nextOpen, nextClose);
    1.36 +      if (pos == text.length) break;
    1.37 +      if (pos == nextOpen) ++depth;
    1.38 +      else if (!--depth) { end = i; endCh = pos; break outer; }
    1.39 +      ++pos;
    1.40 +    }
    1.41 +  }
    1.42 +  if (end == null || line == end && endCh == startCh) return;
    1.43 +  return {from: CodeMirror.Pos(line, startCh),
    1.44 +          to: CodeMirror.Pos(end, endCh)};
    1.45 +});

mercurial