1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/sourceeditor/codemirror/matchbrackets.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 +(function(mod) { 1.5 + if (typeof exports == "object" && typeof module == "object") // CommonJS 1.6 + mod(require("../../lib/codemirror")); 1.7 + else if (typeof define == "function" && define.amd) // AMD 1.8 + define(["../../lib/codemirror"], mod); 1.9 + else // Plain browser env 1.10 + mod(CodeMirror); 1.11 +})(function(CodeMirror) { 1.12 + var ie_lt8 = /MSIE \d/.test(navigator.userAgent) && 1.13 + (document.documentMode == null || document.documentMode < 8); 1.14 + 1.15 + var Pos = CodeMirror.Pos; 1.16 + 1.17 + var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"}; 1.18 + 1.19 + function findMatchingBracket(cm, where, strict, config) { 1.20 + var line = cm.getLineHandle(where.line), pos = where.ch - 1; 1.21 + var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)]; 1.22 + if (!match) return null; 1.23 + var dir = match.charAt(1) == ">" ? 1 : -1; 1.24 + if (strict && (dir > 0) != (pos == where.ch)) return null; 1.25 + var style = cm.getTokenTypeAt(Pos(where.line, pos + 1)); 1.26 + 1.27 + var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style || null, config); 1.28 + return {from: Pos(where.line, pos), to: found && found.pos, 1.29 + match: found && found.ch == match.charAt(0), forward: dir > 0}; 1.30 + } 1.31 + 1.32 + function scanForBracket(cm, where, dir, style, config) { 1.33 + var maxScanLen = (config && config.maxScanLineLength) || 10000; 1.34 + var maxScanLines = (config && config.maxScanLines) || 500; 1.35 + 1.36 + var stack = [], re = /[(){}[\]]/; 1.37 + var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1) 1.38 + : Math.max(cm.firstLine() - 1, where.line - maxScanLines); 1.39 + for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) { 1.40 + var line = cm.getLine(lineNo); 1.41 + if (!line) continue; 1.42 + var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1; 1.43 + if (line.length > maxScanLen) continue; 1.44 + if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0); 1.45 + for (; pos != end; pos += dir) { 1.46 + var ch = line.charAt(pos); 1.47 + if (re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style)) { 1.48 + var match = matching[ch]; 1.49 + if ((match.charAt(1) == ">") == (dir > 0)) stack.push(ch); 1.50 + else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch}; 1.51 + else stack.pop(); 1.52 + } 1.53 + } 1.54 + } 1.55 + } 1.56 + 1.57 + function matchBrackets(cm, autoclear, config) { 1.58 + // Disable brace matching in long lines, since it'll cause hugely slow updates 1.59 + var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000; 1.60 + var marks = [], ranges = cm.listSelections(); 1.61 + for (var i = 0; i < ranges.length; i++) { 1.62 + var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, false, config); 1.63 + if (match && cm.getLine(match.from.line).length <= maxHighlightLen && 1.64 + match.to && cm.getLine(match.to.line).length <= maxHighlightLen) { 1.65 + var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket"; 1.66 + marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style})); 1.67 + if (match.to) 1.68 + marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style})); 1.69 + } 1.70 + } 1.71 + 1.72 + if (marks.length) { 1.73 + // Kludge to work around the IE bug from issue #1193, where text 1.74 + // input stops going to the textare whever this fires. 1.75 + if (ie_lt8 && cm.state.focused) cm.display.input.focus(); 1.76 + 1.77 + var clear = function() { 1.78 + cm.operation(function() { 1.79 + for (var i = 0; i < marks.length; i++) marks[i].clear(); 1.80 + }); 1.81 + }; 1.82 + if (autoclear) setTimeout(clear, 800); 1.83 + else return clear; 1.84 + } 1.85 + } 1.86 + 1.87 + var currentlyHighlighted = null; 1.88 + function doMatchBrackets(cm) { 1.89 + cm.operation(function() { 1.90 + if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;} 1.91 + currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets); 1.92 + }); 1.93 + } 1.94 + 1.95 + CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) { 1.96 + if (old && old != CodeMirror.Init) 1.97 + cm.off("cursorActivity", doMatchBrackets); 1.98 + if (val) { 1.99 + cm.state.matchBrackets = typeof val == "object" ? val : {}; 1.100 + cm.on("cursorActivity", doMatchBrackets); 1.101 + } 1.102 + }); 1.103 + 1.104 + CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);}); 1.105 + CodeMirror.defineExtension("findMatchingBracket", function(pos, strict){ 1.106 + return findMatchingBracket(this, pos, strict); 1.107 + }); 1.108 + CodeMirror.defineExtension("scanForBracket", function(pos, dir, style){ 1.109 + return scanForBracket(this, pos, dir, style); 1.110 + }); 1.111 +});