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