browser/devtools/sourceeditor/codemirror/fold/brace-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.

     1 CodeMirror.registerHelper("fold", "brace", function(cm, start) {
     2   var line = start.line, lineText = cm.getLine(line);
     3   var startCh, tokenType;
     5   function findOpening(openCh) {
     6     for (var at = start.ch, pass = 0;;) {
     7       var found = at <= 0 ? -1 : lineText.lastIndexOf(openCh, at - 1);
     8       if (found == -1) {
     9         if (pass == 1) break;
    10         pass = 1;
    11         at = lineText.length;
    12         continue;
    13       }
    14       if (pass == 1 && found < start.ch) break;
    15       tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1));
    16       if (!/^(comment|string)/.test(tokenType)) return found + 1;
    17       at = found - 1;
    18     }
    19   }
    21   var startToken = "{", endToken = "}", startCh = findOpening("{");
    22   if (startCh == null) {
    23     startToken = "[", endToken = "]";
    24     startCh = findOpening("[");
    25   }
    27   if (startCh == null) return;
    28   var count = 1, lastLine = cm.lastLine(), end, endCh;
    29   outer: for (var i = line; i <= lastLine; ++i) {
    30     var text = cm.getLine(i), pos = i == line ? startCh : 0;
    31     for (;;) {
    32       var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos);
    33       if (nextOpen < 0) nextOpen = text.length;
    34       if (nextClose < 0) nextClose = text.length;
    35       pos = Math.min(nextOpen, nextClose);
    36       if (pos == text.length) break;
    37       if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == tokenType) {
    38         if (pos == nextOpen) ++count;
    39         else if (!--count) { end = i; endCh = pos; break outer; }
    40       }
    41       ++pos;
    42     }
    43   }
    44   if (end == null || line == end && endCh == startCh) return;
    45   return {from: CodeMirror.Pos(line, startCh),
    46           to: CodeMirror.Pos(end, endCh)};
    47 });
    48 CodeMirror.braceRangeFinder = CodeMirror.fold.brace; // deprecated
    50 CodeMirror.registerHelper("fold", "import", function(cm, start) {
    51   function hasImport(line) {
    52     if (line < cm.firstLine() || line > cm.lastLine()) return null;
    53     var start = cm.getTokenAt(CodeMirror.Pos(line, 1));
    54     if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1));
    55     if (start.type != "keyword" || start.string != "import") return null;
    56     // Now find closing semicolon, return its position
    57     for (var i = line, e = Math.min(cm.lastLine(), line + 10); i <= e; ++i) {
    58       var text = cm.getLine(i), semi = text.indexOf(";");
    59       if (semi != -1) return {startCh: start.end, end: CodeMirror.Pos(i, semi)};
    60     }
    61   }
    63   var start = start.line, has = hasImport(start), prev;
    64   if (!has || hasImport(start - 1) || ((prev = hasImport(start - 2)) && prev.end.line == start - 1))
    65     return null;
    66   for (var end = has.end;;) {
    67     var next = hasImport(end.line + 1);
    68     if (next == null) break;
    69     end = next.end;
    70   }
    71   return {from: cm.clipPos(CodeMirror.Pos(start, has.startCh + 1)), to: end};
    72 });
    73 CodeMirror.importRangeFinder = CodeMirror.fold["import"]; // deprecated
    75 CodeMirror.registerHelper("fold", "include", function(cm, start) {
    76   function hasInclude(line) {
    77     if (line < cm.firstLine() || line > cm.lastLine()) return null;
    78     var start = cm.getTokenAt(CodeMirror.Pos(line, 1));
    79     if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1));
    80     if (start.type == "meta" && start.string.slice(0, 8) == "#include") return start.start + 8;
    81   }
    83   var start = start.line, has = hasInclude(start);
    84   if (has == null || hasInclude(start - 1) != null) return null;
    85   for (var end = start;;) {
    86     var next = hasInclude(end + 1);
    87     if (next == null) break;
    88     ++end;
    89   }
    90   return {from: CodeMirror.Pos(start, has + 1),
    91           to: cm.clipPos(CodeMirror.Pos(end))};
    92 });
    93 CodeMirror.includeRangeFinder = CodeMirror.fold.include; // deprecated

mercurial