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