LaravelTest
160 строк · 6.7 Кб
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) {12var ie_lt8 = /MSIE \d/.test(navigator.userAgent) &&13(document.documentMode == null || document.documentMode < 8);14
15var Pos = CodeMirror.Pos;16
17var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<", "<": ">>", ">": "<<"};18
19function bracketRegex(config) {20return config && config.bracketRegex || /[(){}[\]]/21}22
23function findMatchingBracket(cm, where, config) {24var line = cm.getLineHandle(where.line), pos = where.ch - 1;25var afterCursor = config && config.afterCursor26if (afterCursor == null)27afterCursor = /(^| )cm-fat-cursor($| )/.test(cm.getWrapperElement().className)28var re = bracketRegex(config)29
30// A cursor is defined as between two characters, but in in vim command mode31// (i.e. not insert mode), the cursor is visually represented as a32// highlighted box on top of the 2nd character. Otherwise, we allow matches33// from before or after the cursor.34var match = (!afterCursor && pos >= 0 && re.test(line.text.charAt(pos)) && matching[line.text.charAt(pos)]) ||35re.test(line.text.charAt(pos + 1)) && matching[line.text.charAt(++pos)];36if (!match) return null;37var dir = match.charAt(1) == ">" ? 1 : -1;38if (config && config.strict && (dir > 0) != (pos == where.ch)) return null;39var style = cm.getTokenTypeAt(Pos(where.line, pos + 1));40
41var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style, config);42if (found == null) return null;43return {from: Pos(where.line, pos), to: found && found.pos,44match: found && found.ch == match.charAt(0), forward: dir > 0};45}46
47// bracketRegex is used to specify which type of bracket to scan48// should be a regexp, e.g. /[[\]]/49//50// Note: If "where" is on an open bracket, then this bracket is ignored.51//52// Returns false when no bracket was found, null when it reached53// maxScanLines and gave up54function scanForBracket(cm, where, dir, style, config) {55var maxScanLen = (config && config.maxScanLineLength) || 10000;56var maxScanLines = (config && config.maxScanLines) || 1000;57
58var stack = [];59var re = bracketRegex(config)60var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1)61: Math.max(cm.firstLine() - 1, where.line - maxScanLines);62for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) {63var line = cm.getLine(lineNo);64if (!line) continue;65var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1;66if (line.length > maxScanLen) continue;67if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0);68for (; pos != end; pos += dir) {69var ch = line.charAt(pos);70if (re.test(ch) && (style === undefined ||71(cm.getTokenTypeAt(Pos(lineNo, pos + 1)) || "") == (style || ""))) {72var match = matching[ch];73if (match && (match.charAt(1) == ">") == (dir > 0)) stack.push(ch);74else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch};75else stack.pop();76}77}78}79return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false : null;80}81
82function matchBrackets(cm, autoclear, config) {83// Disable brace matching in long lines, since it'll cause hugely slow updates84var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000,85highlightNonMatching = config && config.highlightNonMatching;86var marks = [], ranges = cm.listSelections();87for (var i = 0; i < ranges.length; i++) {88var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, config);89if (match && (match.match || highlightNonMatching !== false) && cm.getLine(match.from.line).length <= maxHighlightLen) {90var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";91marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style}));92if (match.to && cm.getLine(match.to.line).length <= maxHighlightLen)93marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style}));94}95}96
97if (marks.length) {98// Kludge to work around the IE bug from issue #1193, where text99// input stops going to the textarea whenever this fires.100if (ie_lt8 && cm.state.focused) cm.focus();101
102var clear = function() {103cm.operation(function() {104for (var i = 0; i < marks.length; i++) marks[i].clear();105});106};107if (autoclear) setTimeout(clear, 800);108else return clear;109}110}111
112function doMatchBrackets(cm) {113cm.operation(function() {114if (cm.state.matchBrackets.currentlyHighlighted) {115cm.state.matchBrackets.currentlyHighlighted();116cm.state.matchBrackets.currentlyHighlighted = null;117}118cm.state.matchBrackets.currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets);119});120}121
122function clearHighlighted(cm) {123if (cm.state.matchBrackets && cm.state.matchBrackets.currentlyHighlighted) {124cm.state.matchBrackets.currentlyHighlighted();125cm.state.matchBrackets.currentlyHighlighted = null;126}127}128
129CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {130if (old && old != CodeMirror.Init) {131cm.off("cursorActivity", doMatchBrackets);132cm.off("focus", doMatchBrackets)133cm.off("blur", clearHighlighted)134clearHighlighted(cm);135}136if (val) {137cm.state.matchBrackets = typeof val == "object" ? val : {};138cm.on("cursorActivity", doMatchBrackets);139cm.on("focus", doMatchBrackets)140cm.on("blur", clearHighlighted)141}142});143
144CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});145CodeMirror.defineExtension("findMatchingBracket", function(pos, config, oldConfig){146// Backwards-compatibility kludge147if (oldConfig || typeof config == "boolean") {148if (!oldConfig) {149config = config ? {strict: true} : null150} else {151oldConfig.strict = config152config = oldConfig153}154}155return findMatchingBracket(this, pos, config)156});157CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){158return scanForBracket(this, pos, dir, style, config);159});160});161