OLD | NEW |
| 1 // CodeMirror, copyright (c) by Marijn Haverbeke and others |
| 2 // Distributed under an MIT license: http://codemirror.net/LICENSE |
| 3 |
1 (function(mod) { | 4 (function(mod) { |
2 if (typeof exports == "object" && typeof module == "object") // CommonJS | 5 if (typeof exports == "object" && typeof module == "object") // CommonJS |
3 mod(require("../../lib/codemirror")); | 6 mod(require("../../lib/codemirror")); |
4 else if (typeof define == "function" && define.amd) // AMD | 7 else if (typeof define == "function" && define.amd) // AMD |
5 define(["../../lib/codemirror"], mod); | 8 define(["../../lib/codemirror"], mod); |
6 else // Plain browser env | 9 else // Plain browser env |
7 mod(CodeMirror); | 10 mod(CodeMirror); |
8 })(function(CodeMirror) { | 11 })(function(CodeMirror) { |
9 var ie_lt8 = /MSIE \d/.test(navigator.userAgent) && | 12 var ie_lt8 = /MSIE \d/.test(navigator.userAgent) && |
10 (document.documentMode == null || document.documentMode < 8); | 13 (document.documentMode == null || document.documentMode < 8); |
11 | 14 |
12 var Pos = CodeMirror.Pos; | 15 var Pos = CodeMirror.Pos; |
13 | 16 |
14 var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{
<"}; | 17 var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{
<"}; |
15 | 18 |
16 function findMatchingBracket(cm, where, strict, config) { | 19 function findMatchingBracket(cm, where, strict, config) { |
17 var line = cm.getLineHandle(where.line), pos = where.ch - 1; | 20 var line = cm.getLineHandle(where.line), pos = where.ch - 1; |
18 var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.t
ext.charAt(++pos)]; | 21 var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.t
ext.charAt(++pos)]; |
19 if (!match) return null; | 22 if (!match) return null; |
20 var dir = match.charAt(1) == ">" ? 1 : -1; | 23 var dir = match.charAt(1) == ">" ? 1 : -1; |
21 if (strict && (dir > 0) != (pos == where.ch)) return null; | 24 if (strict && (dir > 0) != (pos == where.ch)) return null; |
22 var style = cm.getTokenTypeAt(Pos(where.line, pos + 1)); | 25 var style = cm.getTokenTypeAt(Pos(where.line, pos + 1)); |
23 | 26 |
24 var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir
, style || null, config); | 27 var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir
, style || null, config); |
| 28 if (found == null) return null; |
25 return {from: Pos(where.line, pos), to: found && found.pos, | 29 return {from: Pos(where.line, pos), to: found && found.pos, |
26 match: found && found.ch == match.charAt(0), forward: dir > 0}; | 30 match: found && found.ch == match.charAt(0), forward: dir > 0}; |
27 } | 31 } |
28 | 32 |
29 // bracketRegex is used to specify which type of bracket to scan | 33 // bracketRegex is used to specify which type of bracket to scan |
30 // should be a regexp, e.g. /[[\]]/ | 34 // should be a regexp, e.g. /[[\]]/ |
31 // | 35 // |
32 // Note: If "where" is on an open bracket, then this bracket is ignored. | 36 // Note: If "where" is on an open bracket, then this bracket is ignored. |
| 37 // |
| 38 // Returns false when no bracket was found, null when it reached |
| 39 // maxScanLines and gave up |
33 function scanForBracket(cm, where, dir, style, config) { | 40 function scanForBracket(cm, where, dir, style, config) { |
34 var maxScanLen = (config && config.maxScanLineLength) || 10000; | 41 var maxScanLen = (config && config.maxScanLineLength) || 10000; |
35 var maxScanLines = (config && config.maxScanLines) || 500; | 42 var maxScanLines = (config && config.maxScanLines) || 1000; |
36 | 43 |
37 var stack = []; | 44 var stack = []; |
38 var re = config && config.bracketRegex ? config.bracketRegex : /[(){}[\]]/; | 45 var re = config && config.bracketRegex ? config.bracketRegex : /[(){}[\]]/; |
39 var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() +
1) | 46 var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() +
1) |
40 : Math.max(cm.firstLine() - 1, where.line - maxScanLin
es); | 47 : Math.max(cm.firstLine() - 1, where.line - maxScanLin
es); |
41 for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) { | 48 for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) { |
42 var line = cm.getLine(lineNo); | 49 var line = cm.getLine(lineNo); |
43 if (!line) continue; | 50 if (!line) continue; |
44 var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1; | 51 var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1; |
45 if (line.length > maxScanLen) continue; | 52 if (line.length > maxScanLen) continue; |
46 if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0); | 53 if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0); |
47 for (; pos != end; pos += dir) { | 54 for (; pos != end; pos += dir) { |
48 var ch = line.charAt(pos); | 55 var ch = line.charAt(pos); |
49 if (re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNo,
pos + 1)) == style)) { | 56 if (re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNo,
pos + 1)) == style)) { |
50 var match = matching[ch]; | 57 var match = matching[ch]; |
51 if ((match.charAt(1) == ">") == (dir > 0)) stack.push(ch); | 58 if ((match.charAt(1) == ">") == (dir > 0)) stack.push(ch); |
52 else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch}; | 59 else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch}; |
53 else stack.pop(); | 60 else stack.pop(); |
54 } | 61 } |
55 } | 62 } |
56 } | 63 } |
| 64 return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false :
null; |
57 } | 65 } |
58 | 66 |
59 function matchBrackets(cm, autoclear, config) { | 67 function matchBrackets(cm, autoclear, config) { |
60 // Disable brace matching in long lines, since it'll cause hugely slow updat
es | 68 // Disable brace matching in long lines, since it'll cause hugely slow updat
es |
61 var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000; | 69 var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000; |
62 var marks = [], ranges = cm.listSelections(); | 70 var marks = [], ranges = cm.listSelections(); |
63 for (var i = 0; i < ranges.length; i++) { | 71 for (var i = 0; i < ranges.length; i++) { |
64 var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, f
alse, config); | 72 var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, f
alse, config); |
65 if (match && cm.getLine(match.from.line).length <= maxHighlightLen && | 73 if (match && cm.getLine(match.from.line).length <= maxHighlightLen) { |
66 match.to && cm.getLine(match.to.line).length <= maxHighlightLen) { | |
67 var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-non
matchingbracket"; | 74 var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-non
matchingbracket"; |
68 marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch +
1), {className: style})); | 75 marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch +
1), {className: style})); |
69 if (match.to) | 76 if (match.to && cm.getLine(match.to.line).length <= maxHighlightLen) |
70 marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1),
{className: style})); | 77 marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1),
{className: style})); |
71 } | 78 } |
72 } | 79 } |
73 | 80 |
74 if (marks.length) { | 81 if (marks.length) { |
75 // Kludge to work around the IE bug from issue #1193, where text | 82 // Kludge to work around the IE bug from issue #1193, where text |
76 // input stops going to the textare whever this fires. | 83 // input stops going to the textare whever this fires. |
77 if (ie_lt8 && cm.state.focused) cm.display.input.focus(); | 84 if (ie_lt8 && cm.state.focused) cm.display.input.focus(); |
78 | 85 |
79 var clear = function() { | 86 var clear = function() { |
(...skipping 24 matching lines...) Expand all Loading... |
104 }); | 111 }); |
105 | 112 |
106 CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, tr
ue);}); | 113 CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, tr
ue);}); |
107 CodeMirror.defineExtension("findMatchingBracket", function(pos, strict, config
){ | 114 CodeMirror.defineExtension("findMatchingBracket", function(pos, strict, config
){ |
108 return findMatchingBracket(this, pos, strict, config); | 115 return findMatchingBracket(this, pos, strict, config); |
109 }); | 116 }); |
110 CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config)
{ | 117 CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config)
{ |
111 return scanForBracket(this, pos, dir, style, config); | 118 return scanForBracket(this, pos, dir, style, config); |
112 }); | 119 }); |
113 }); | 120 }); |
OLD | NEW |