Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/edit/matchbrackets.js
1294 views
(function() {1var ie_lt8 = /MSIE \d/.test(navigator.userAgent) &&2(document.documentMode == null || document.documentMode < 8);34var Pos = CodeMirror.Pos;56var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"};7function findMatchingBracket(cm, where, strict) {8var state = cm.state.matchBrackets;9var maxScanLen = (state && state.maxScanLineLength) || 10000;10var maxScanLines = (state && state.maxScanLines) || 100;1112var cur = where || cm.getCursor(), line = cm.getLineHandle(cur.line), pos = cur.ch - 1;13var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)];14if (!match) return null;15var forward = match.charAt(1) == ">", d = forward ? 1 : -1;16if (strict && forward != (pos == cur.ch)) return null;17var style = cm.getTokenTypeAt(Pos(cur.line, pos + 1));1819var stack = [line.text.charAt(pos)], re = /[(){}[\]]/;20function scan(line, lineNo, start) {21if (!line.text) return;22var pos = forward ? 0 : line.text.length - 1, end = forward ? line.text.length : -1;23if (line.text.length > maxScanLen) return null;24if (start != null) pos = start + d;25for (; pos != end; pos += d) {26var ch = line.text.charAt(pos);27if (re.test(ch) && cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style) {28var match = matching[ch];29if (match.charAt(1) == ">" == forward) stack.push(ch);30else if (stack.pop() != match.charAt(0)) return {pos: pos, match: false};31else if (!stack.length) return {pos: pos, match: true};32}33}34}35for (var i = cur.line, found, e = forward ? Math.min(i + maxScanLines, cm.lineCount()) : Math.max(-1, i - maxScanLines); i != e; i+=d) {36if (i == cur.line) found = scan(line, i, pos);37else found = scan(cm.getLineHandle(i), i);38if (found) break;39}40return {from: Pos(cur.line, pos), to: found && Pos(i, found.pos),41match: found && found.match, forward: forward};42}4344function matchBrackets(cm, autoclear) {45// Disable brace matching in long lines, since it'll cause hugely slow updates46var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000;47var found = findMatchingBracket(cm);48if (!found || cm.getLine(found.from.line).length > maxHighlightLen ||49found.to && cm.getLine(found.to.line).length > maxHighlightLen)50return;5152var style = found.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";53var one = cm.markText(found.from, Pos(found.from.line, found.from.ch + 1), {className: style});54var two = found.to && cm.markText(found.to, Pos(found.to.line, found.to.ch + 1), {className: style});55// Kludge to work around the IE bug from issue #1193, where text56// input stops going to the textarea whenever this fires.57if (ie_lt8 && cm.state.focused) cm.display.input.focus();58var clear = function() {59cm.operation(function() { one.clear(); two && two.clear(); });60};61if (autoclear) setTimeout(clear, 800);62else return clear;63}6465var currentlyHighlighted = null;66function doMatchBrackets(cm) {67cm.operation(function() {68if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;}69if (!cm.somethingSelected()) currentlyHighlighted = matchBrackets(cm, false);70});71}7273CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {74if (old && old != CodeMirror.Init)75cm.off("cursorActivity", doMatchBrackets);76if (val) {77cm.state.matchBrackets = typeof val == "object" ? val : {};78cm.on("cursorActivity", doMatchBrackets);79}80});8182CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});83CodeMirror.defineExtension("findMatchingBracket", function(pos, strict){84return findMatchingBracket(this, pos, strict);85});86})();878889