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.

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

mercurial