| 1 |
(function() { |
| 2 |
var ie_lt8 = /MSIE \d/.test(navigator.userAgent) && |
| 3 |
(document.documentMode == null || document.documentMode < 8); |
| 4 |
|
| 5 |
var Pos = CodeMirror.Pos; |
| 6 |
|
| 7 |
var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"}; |
| 8 |
function findMatchingBracket(cm, where, strict) { |
| 9 |
var state = cm.state.matchBrackets; |
| 10 |
var maxScanLen = (state && state.maxScanLineLength) || 10000; |
| 11 |
|
| 12 |
var cur = where || cm.getCursor(), line = cm.getLineHandle(cur.line), pos = cur.ch - 1; |
| 13 |
var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)]; |
| 14 |
if (!match) return null; |
| 15 |
var forward = match.charAt(1) == ">", d = forward ? 1 : -1; |
| 16 |
if (strict && forward != (pos == cur.ch)) return null; |
| 17 |
var style = cm.getTokenTypeAt(Pos(cur.line, pos + 1)); |
| 18 |
|
| 19 |
var stack = [line.text.charAt(pos)], re = /[(){}[\]]/; |
| 20 |
function scan(line, lineNo, start) { |
| 21 |
if (!line.text) return; |
| 22 |
var pos = forward ? 0 : line.text.length - 1, end = forward ? line.text.length : -1; |
| 23 |
if (line.text.length > maxScanLen) return null; |
| 24 |
if (start != null) pos = start + d; |
| 25 |
for (; pos != end; pos += d) { |
| 26 |
var ch = line.text.charAt(pos); |
| 27 |
if (re.test(ch) && cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style) { |
| 28 |
var match = matching[ch]; |
| 29 |
if (match.charAt(1) == ">" == forward) stack.push(ch); |
| 30 |
else if (stack.pop() != match.charAt(0)) return {pos: pos, match: false}; |
| 31 |
else if (!stack.length) return {pos: pos, match: true}; |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
for (var i = cur.line, found, e = forward ? Math.min(i + 100, cm.lineCount()) : Math.max(-1, i - 100); i != e; i+=d) { |
| 36 |
if (i == cur.line) found = scan(line, i, pos); |
| 37 |
else found = scan(cm.getLineHandle(i), i); |
| 38 |
if (found) break; |
| 39 |
} |
| 40 |
return {from: Pos(cur.line, pos), to: found && Pos(i, found.pos), |
| 41 |
match: found && found.match, forward: forward}; |
| 42 |
} |
| 43 |
|
| 44 |
function matchBrackets(cm, autoclear) { |
| 45 |
// Disable brace matching in long lines, since it'll cause hugely slow updates |
| 46 |
var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000; |
| 47 |
var found = findMatchingBracket(cm); |
| 48 |
if (!found || cm.getLine(found.from.line).length > maxHighlightLen || |
| 49 |
found.to && cm.getLine(found.to.line).length > maxHighlightLen) |
| 50 |
return; |
| 51 |
|
| 52 |
var style = found.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket"; |
| 53 |
var one = cm.markText(found.from, Pos(found.from.line, found.from.ch + 1), {className: style}); |
| 54 |
var 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 text |
| 56 |
// input stops going to the textare whever this fires. |
| 57 |
if (ie_lt8 && cm.state.focused) cm.display.input.focus(); |
| 58 |
var clear = function() { |
| 59 |
cm.operation(function() { one.clear(); two && two.clear(); }); |
| 60 |
}; |
| 61 |
if (autoclear) setTimeout(clear, 800); |
| 62 |
else return clear; |
| 63 |
} |
| 64 |
|
| 65 |
var currentlyHighlighted = null; |
| 66 |
function doMatchBrackets(cm) { |
| 67 |
cm.operation(function() { |
| 68 |
if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;} |
| 69 |
if (!cm.somethingSelected()) currentlyHighlighted = matchBrackets(cm, false); |
| 70 |
}); |
| 71 |
} |
| 72 |
|
| 73 |
CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) { |
| 74 |
if (old && old != CodeMirror.Init) |
| 75 |
cm.off("cursorActivity", doMatchBrackets); |
| 76 |
if (val) { |
| 77 |
cm.state.matchBrackets = typeof val == "object" ? val : {}; |
| 78 |
cm.on("cursorActivity", doMatchBrackets); |
| 79 |
} |
| 80 |
}); |
| 81 |
|
| 82 |
CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);}); |
| 83 |
CodeMirror.defineExtension("findMatchingBracket", function(pos, strict){ |
| 84 |
return findMatchingBracket(this, pos, strict); |
| 85 |
}); |
| 86 |
})(); |
| 87 |
|