browser/devtools/sourceeditor/codemirror/fold/brace-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/brace-fold.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +CodeMirror.registerHelper("fold", "brace", function(cm, start) {
     1.5 +  var line = start.line, lineText = cm.getLine(line);
     1.6 +  var startCh, tokenType;
     1.7 +
     1.8 +  function findOpening(openCh) {
     1.9 +    for (var at = start.ch, pass = 0;;) {
    1.10 +      var found = at <= 0 ? -1 : lineText.lastIndexOf(openCh, at - 1);
    1.11 +      if (found == -1) {
    1.12 +        if (pass == 1) break;
    1.13 +        pass = 1;
    1.14 +        at = lineText.length;
    1.15 +        continue;
    1.16 +      }
    1.17 +      if (pass == 1 && found < start.ch) break;
    1.18 +      tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1));
    1.19 +      if (!/^(comment|string)/.test(tokenType)) return found + 1;
    1.20 +      at = found - 1;
    1.21 +    }
    1.22 +  }
    1.23 +
    1.24 +  var startToken = "{", endToken = "}", startCh = findOpening("{");
    1.25 +  if (startCh == null) {
    1.26 +    startToken = "[", endToken = "]";
    1.27 +    startCh = findOpening("[");
    1.28 +  }
    1.29 +
    1.30 +  if (startCh == null) return;
    1.31 +  var count = 1, lastLine = cm.lastLine(), end, endCh;
    1.32 +  outer: for (var i = line; i <= lastLine; ++i) {
    1.33 +    var text = cm.getLine(i), pos = i == line ? startCh : 0;
    1.34 +    for (;;) {
    1.35 +      var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos);
    1.36 +      if (nextOpen < 0) nextOpen = text.length;
    1.37 +      if (nextClose < 0) nextClose = text.length;
    1.38 +      pos = Math.min(nextOpen, nextClose);
    1.39 +      if (pos == text.length) break;
    1.40 +      if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == tokenType) {
    1.41 +        if (pos == nextOpen) ++count;
    1.42 +        else if (!--count) { end = i; endCh = pos; break outer; }
    1.43 +      }
    1.44 +      ++pos;
    1.45 +    }
    1.46 +  }
    1.47 +  if (end == null || line == end && endCh == startCh) return;
    1.48 +  return {from: CodeMirror.Pos(line, startCh),
    1.49 +          to: CodeMirror.Pos(end, endCh)};
    1.50 +});
    1.51 +CodeMirror.braceRangeFinder = CodeMirror.fold.brace; // deprecated
    1.52 +
    1.53 +CodeMirror.registerHelper("fold", "import", function(cm, start) {
    1.54 +  function hasImport(line) {
    1.55 +    if (line < cm.firstLine() || line > cm.lastLine()) return null;
    1.56 +    var start = cm.getTokenAt(CodeMirror.Pos(line, 1));
    1.57 +    if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1));
    1.58 +    if (start.type != "keyword" || start.string != "import") return null;
    1.59 +    // Now find closing semicolon, return its position
    1.60 +    for (var i = line, e = Math.min(cm.lastLine(), line + 10); i <= e; ++i) {
    1.61 +      var text = cm.getLine(i), semi = text.indexOf(";");
    1.62 +      if (semi != -1) return {startCh: start.end, end: CodeMirror.Pos(i, semi)};
    1.63 +    }
    1.64 +  }
    1.65 +
    1.66 +  var start = start.line, has = hasImport(start), prev;
    1.67 +  if (!has || hasImport(start - 1) || ((prev = hasImport(start - 2)) && prev.end.line == start - 1))
    1.68 +    return null;
    1.69 +  for (var end = has.end;;) {
    1.70 +    var next = hasImport(end.line + 1);
    1.71 +    if (next == null) break;
    1.72 +    end = next.end;
    1.73 +  }
    1.74 +  return {from: cm.clipPos(CodeMirror.Pos(start, has.startCh + 1)), to: end};
    1.75 +});
    1.76 +CodeMirror.importRangeFinder = CodeMirror.fold["import"]; // deprecated
    1.77 +
    1.78 +CodeMirror.registerHelper("fold", "include", function(cm, start) {
    1.79 +  function hasInclude(line) {
    1.80 +    if (line < cm.firstLine() || line > cm.lastLine()) return null;
    1.81 +    var start = cm.getTokenAt(CodeMirror.Pos(line, 1));
    1.82 +    if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1));
    1.83 +    if (start.type == "meta" && start.string.slice(0, 8) == "#include") return start.start + 8;
    1.84 +  }
    1.85 +
    1.86 +  var start = start.line, has = hasInclude(start);
    1.87 +  if (has == null || hasInclude(start - 1) != null) return null;
    1.88 +  for (var end = start;;) {
    1.89 +    var next = hasInclude(end + 1);
    1.90 +    if (next == null) break;
    1.91 +    ++end;
    1.92 +  }
    1.93 +  return {from: CodeMirror.Pos(start, has + 1),
    1.94 +          to: cm.clipPos(CodeMirror.Pos(end))};
    1.95 +});
    1.96 +CodeMirror.includeRangeFinder = CodeMirror.fold.include; // deprecated

mercurial