LaravelTest
119 строк · 4.4 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14function bracketFolding(pairs) {15return function(cm, start) {16var line = start.line, lineText = cm.getLine(line);17
18function findOpening(pair) {19var tokenType;20for (var at = start.ch, pass = 0;;) {21var found = at <= 0 ? -1 : lineText.lastIndexOf(pair[0], at - 1);22if (found == -1) {23if (pass == 1) break;24pass = 1;25at = lineText.length;26continue;27}28if (pass == 1 && found < start.ch) break;29tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1));30if (!/^(comment|string)/.test(tokenType)) return {ch: found + 1, tokenType: tokenType, pair: pair};31at = found - 1;32}33}34
35function findRange(found) {36var count = 1, lastLine = cm.lastLine(), end, startCh = found.ch, endCh37outer: for (var i = line; i <= lastLine; ++i) {38var text = cm.getLine(i), pos = i == line ? startCh : 0;39for (;;) {40var nextOpen = text.indexOf(found.pair[0], pos), nextClose = text.indexOf(found.pair[1], pos);41if (nextOpen < 0) nextOpen = text.length;42if (nextClose < 0) nextClose = text.length;43pos = Math.min(nextOpen, nextClose);44if (pos == text.length) break;45if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == found.tokenType) {46if (pos == nextOpen) ++count;47else if (!--count) { end = i; endCh = pos; break outer; }48}49++pos;50}51}52
53if (end == null || line == end) return null54return {from: CodeMirror.Pos(line, startCh),55to: CodeMirror.Pos(end, endCh)};56}57
58var found = []59for (var i = 0; i < pairs.length; i++) {60var open = findOpening(pairs[i])61if (open) found.push(open)62}63found.sort(function(a, b) { return a.ch - b.ch })64for (var i = 0; i < found.length; i++) {65var range = findRange(found[i])66if (range) return range67}68return null69}70}
71
72CodeMirror.registerHelper("fold", "brace", bracketFolding([["{", "}"], ["[", "]"]]));73
74CodeMirror.registerHelper("fold", "brace-paren", bracketFolding([["{", "}"], ["[", "]"], ["(", ")"]]));75
76CodeMirror.registerHelper("fold", "import", function(cm, start) {77function hasImport(line) {78if (line < cm.firstLine() || line > cm.lastLine()) return null;79var start = cm.getTokenAt(CodeMirror.Pos(line, 1));80if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1));81if (start.type != "keyword" || start.string != "import") return null;82// Now find closing semicolon, return its position83for (var i = line, e = Math.min(cm.lastLine(), line + 10); i <= e; ++i) {84var text = cm.getLine(i), semi = text.indexOf(";");85if (semi != -1) return {startCh: start.end, end: CodeMirror.Pos(i, semi)};86}87}88
89var startLine = start.line, has = hasImport(startLine), prev;90if (!has || hasImport(startLine - 1) || ((prev = hasImport(startLine - 2)) && prev.end.line == startLine - 1))91return null;92for (var end = has.end;;) {93var next = hasImport(end.line + 1);94if (next == null) break;95end = next.end;96}97return {from: cm.clipPos(CodeMirror.Pos(startLine, has.startCh + 1)), to: end};98});99
100CodeMirror.registerHelper("fold", "include", function(cm, start) {101function hasInclude(line) {102if (line < cm.firstLine() || line > cm.lastLine()) return null;103var start = cm.getTokenAt(CodeMirror.Pos(line, 1));104if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1));105if (start.type == "meta" && start.string.slice(0, 8) == "#include") return start.start + 8;106}107
108var startLine = start.line, has = hasInclude(startLine);109if (has == null || hasInclude(startLine - 1) != null) return null;110for (var end = startLine;;) {111var next = hasInclude(end + 1);112if (next == null) break;113++end;114}115return {from: CodeMirror.Pos(startLine, has + 1),116to: cm.clipPos(CodeMirror.Pos(end))};117});118
119});120