openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

brace-fold.min.js (3939B)


      1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
      2 // Distributed under an MIT license: http://codemirror.net/LICENSE
      3 
      4 (function(mod) {
      5   if (typeof exports == "object" && typeof module == "object") // CommonJS
      6     mod(require("../../lib/codemirror"));
      7   else if (typeof define == "function" && define.amd) // AMD
      8     define(["../../lib/codemirror"], mod);
      9   else // Plain browser env
     10     mod(CodeMirror);
     11 })(function(CodeMirror) {
     12 "use strict";
     13 
     14 CodeMirror.registerHelper("fold", "brace", function(cm, start) {
     15   var line = start.line, lineText = cm.getLine(line);
     16   var tokenType;
     17 
     18   function findOpening(openCh) {
     19     for (var at = start.ch, pass = 0;;) {
     20       var found = at <= 0 ? -1 : lineText.lastIndexOf(openCh, at - 1);
     21       if (found == -1) {
     22         if (pass == 1) break;
     23         pass = 1;
     24         at = lineText.length;
     25         continue;
     26       }
     27       if (pass == 1 && found < start.ch) break;
     28       tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1));
     29       if (!/^(comment|string)/.test(tokenType)) return found + 1;
     30       at = found - 1;
     31     }
     32   }
     33 
     34   var startToken = "{", endToken = "}", startCh = findOpening("{");
     35   if (startCh == null) {
     36     startToken = "[", endToken = "]";
     37     startCh = findOpening("[");
     38   }
     39 
     40   if (startCh == null) return;
     41   var count = 1, lastLine = cm.lastLine(), end, endCh;
     42   outer: for (var i = line; i <= lastLine; ++i) {
     43     var text = cm.getLine(i), pos = i == line ? startCh : 0;
     44     for (;;) {
     45       var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos);
     46       if (nextOpen < 0) nextOpen = text.length;
     47       if (nextClose < 0) nextClose = text.length;
     48       pos = Math.min(nextOpen, nextClose);
     49       if (pos == text.length) break;
     50       if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == tokenType) {
     51         if (pos == nextOpen) ++count;
     52         else if (!--count) { end = i; endCh = pos; break outer; }
     53       }
     54       ++pos;
     55     }
     56   }
     57   if (end == null || line == end && endCh == startCh) return;
     58   return {from: CodeMirror.Pos(line, startCh),
     59           to: CodeMirror.Pos(end, endCh)};
     60 });
     61 
     62 CodeMirror.registerHelper("fold", "import", function(cm, start) {
     63   function hasImport(line) {
     64     if (line < cm.firstLine() || line > cm.lastLine()) return null;
     65     var start = cm.getTokenAt(CodeMirror.Pos(line, 1));
     66     if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1));
     67     if (start.type != "keyword" || start.string != "import") return null;
     68     // Now find closing semicolon, return its position
     69     for (var i = line, e = Math.min(cm.lastLine(), line + 10); i <= e; ++i) {
     70       var text = cm.getLine(i), semi = text.indexOf(";");
     71       if (semi != -1) return {startCh: start.end, end: CodeMirror.Pos(i, semi)};
     72     }
     73   }
     74 
     75   var startLine = start.line, has = hasImport(startLine), prev;
     76   if (!has || hasImport(startLine - 1) || ((prev = hasImport(startLine - 2)) && prev.end.line == startLine - 1))
     77     return null;
     78   for (var end = has.end;;) {
     79     var next = hasImport(end.line + 1);
     80     if (next == null) break;
     81     end = next.end;
     82   }
     83   return {from: cm.clipPos(CodeMirror.Pos(startLine, has.startCh + 1)), to: end};
     84 });
     85 
     86 CodeMirror.registerHelper("fold", "include", function(cm, start) {
     87   function hasInclude(line) {
     88     if (line < cm.firstLine() || line > cm.lastLine()) return null;
     89     var start = cm.getTokenAt(CodeMirror.Pos(line, 1));
     90     if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1));
     91     if (start.type == "meta" && start.string.slice(0, 8) == "#include") return start.start + 8;
     92   }
     93 
     94   var startLine = start.line, has = hasInclude(startLine);
     95   if (has == null || hasInclude(startLine - 1) != null) return null;
     96   for (var end = startLine;;) {
     97     var next = hasInclude(end + 1);
     98     if (next == null) break;
     99     ++end;
    100   }
    101   return {from: CodeMirror.Pos(startLine, has + 1),
    102           to: cm.clipPos(CodeMirror.Pos(end))};
    103 });
    104 
    105 });