browser/devtools/sourceeditor/codemirror/matchbrackets.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 (function(mod) {
michael@0 2 if (typeof exports == "object" && typeof module == "object") // CommonJS
michael@0 3 mod(require("../../lib/codemirror"));
michael@0 4 else if (typeof define == "function" && define.amd) // AMD
michael@0 5 define(["../../lib/codemirror"], mod);
michael@0 6 else // Plain browser env
michael@0 7 mod(CodeMirror);
michael@0 8 })(function(CodeMirror) {
michael@0 9 var ie_lt8 = /MSIE \d/.test(navigator.userAgent) &&
michael@0 10 (document.documentMode == null || document.documentMode < 8);
michael@0 11
michael@0 12 var Pos = CodeMirror.Pos;
michael@0 13
michael@0 14 var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"};
michael@0 15
michael@0 16 function findMatchingBracket(cm, where, strict, config) {
michael@0 17 var line = cm.getLineHandle(where.line), pos = where.ch - 1;
michael@0 18 var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)];
michael@0 19 if (!match) return null;
michael@0 20 var dir = match.charAt(1) == ">" ? 1 : -1;
michael@0 21 if (strict && (dir > 0) != (pos == where.ch)) return null;
michael@0 22 var style = cm.getTokenTypeAt(Pos(where.line, pos + 1));
michael@0 23
michael@0 24 var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style || null, config);
michael@0 25 return {from: Pos(where.line, pos), to: found && found.pos,
michael@0 26 match: found && found.ch == match.charAt(0), forward: dir > 0};
michael@0 27 }
michael@0 28
michael@0 29 function scanForBracket(cm, where, dir, style, config) {
michael@0 30 var maxScanLen = (config && config.maxScanLineLength) || 10000;
michael@0 31 var maxScanLines = (config && config.maxScanLines) || 500;
michael@0 32
michael@0 33 var stack = [], re = /[(){}[\]]/;
michael@0 34 var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1)
michael@0 35 : Math.max(cm.firstLine() - 1, where.line - maxScanLines);
michael@0 36 for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) {
michael@0 37 var line = cm.getLine(lineNo);
michael@0 38 if (!line) continue;
michael@0 39 var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1;
michael@0 40 if (line.length > maxScanLen) continue;
michael@0 41 if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0);
michael@0 42 for (; pos != end; pos += dir) {
michael@0 43 var ch = line.charAt(pos);
michael@0 44 if (re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style)) {
michael@0 45 var match = matching[ch];
michael@0 46 if ((match.charAt(1) == ">") == (dir > 0)) stack.push(ch);
michael@0 47 else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch};
michael@0 48 else stack.pop();
michael@0 49 }
michael@0 50 }
michael@0 51 }
michael@0 52 }
michael@0 53
michael@0 54 function matchBrackets(cm, autoclear, config) {
michael@0 55 // Disable brace matching in long lines, since it'll cause hugely slow updates
michael@0 56 var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000;
michael@0 57 var marks = [], ranges = cm.listSelections();
michael@0 58 for (var i = 0; i < ranges.length; i++) {
michael@0 59 var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, false, config);
michael@0 60 if (match && cm.getLine(match.from.line).length <= maxHighlightLen &&
michael@0 61 match.to && cm.getLine(match.to.line).length <= maxHighlightLen) {
michael@0 62 var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
michael@0 63 marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style}));
michael@0 64 if (match.to)
michael@0 65 marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style}));
michael@0 66 }
michael@0 67 }
michael@0 68
michael@0 69 if (marks.length) {
michael@0 70 // Kludge to work around the IE bug from issue #1193, where text
michael@0 71 // input stops going to the textare whever this fires.
michael@0 72 if (ie_lt8 && cm.state.focused) cm.display.input.focus();
michael@0 73
michael@0 74 var clear = function() {
michael@0 75 cm.operation(function() {
michael@0 76 for (var i = 0; i < marks.length; i++) marks[i].clear();
michael@0 77 });
michael@0 78 };
michael@0 79 if (autoclear) setTimeout(clear, 800);
michael@0 80 else return clear;
michael@0 81 }
michael@0 82 }
michael@0 83
michael@0 84 var currentlyHighlighted = null;
michael@0 85 function doMatchBrackets(cm) {
michael@0 86 cm.operation(function() {
michael@0 87 if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;}
michael@0 88 currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets);
michael@0 89 });
michael@0 90 }
michael@0 91
michael@0 92 CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
michael@0 93 if (old && old != CodeMirror.Init)
michael@0 94 cm.off("cursorActivity", doMatchBrackets);
michael@0 95 if (val) {
michael@0 96 cm.state.matchBrackets = typeof val == "object" ? val : {};
michael@0 97 cm.on("cursorActivity", doMatchBrackets);
michael@0 98 }
michael@0 99 });
michael@0 100
michael@0 101 CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
michael@0 102 CodeMirror.defineExtension("findMatchingBracket", function(pos, strict){
michael@0 103 return findMatchingBracket(this, pos, strict);
michael@0 104 });
michael@0 105 CodeMirror.defineExtension("scanForBracket", function(pos, dir, style){
michael@0 106 return scanForBracket(this, pos, dir, style);
michael@0 107 });
michael@0 108 });

mercurial