]>
iEval git - gruntmaster-page.git/blob - js/95-matchbrackets.js
2 var ie_lt8
= /MSIE \d/.test(navigator
.userAgent
) &&
3 (document
.documentMode
== null || document
.documentMode
< 8);
5 var Pos
= CodeMirror
.Pos
;
7 var matching
= {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"};
8 function findMatchingBracket(cm
, where
, strict
) {
9 var state
= cm
.state
.matchBrackets
;
10 var maxScanLen
= (state
&& state
.maxScanLineLength
) || 10000;
11 var maxScanLines
= (state
&& state
.maxScanLines
) || 100;
13 var cur
= where
|| cm
.getCursor(), line
= cm
.getLineHandle(cur
.line
), pos
= cur
.ch
- 1;
14 var match
= (pos
>= 0 && matching
[line
.text
.charAt(pos
)]) || matching
[line
.text
.charAt(++pos
)];
15 if (!match
) return null;
16 var forward
= match
.charAt(1) == ">", d
= forward
? 1 : -1;
17 if (strict
&& forward
!= (pos
== cur
.ch
)) return null;
18 var style
= cm
.getTokenTypeAt(Pos(cur
.line
, pos
+ 1));
20 var stack
= [line
.text
.charAt(pos
)], re
= /[(){}[\]]/;
21 function scan(line
, lineNo
, start
) {
22 if (!line
.text
) return;
23 var pos
= forward
? 0 : line
.text
.length
- 1, end
= forward
? line
.text
.length
: -1;
24 if (line
.text
.length
> maxScanLen
) return null;
25 if (start
!= null) pos
= start
+ d
;
26 for (; pos
!= end
; pos
+= d
) {
27 var ch
= line
.text
.charAt(pos
);
28 if (re
.test(ch
) && cm
.getTokenTypeAt(Pos(lineNo
, pos
+ 1)) == style
) {
29 var match
= matching
[ch
];
30 if (match
.charAt(1) == ">" == forward
) stack
.push(ch
);
31 else if (stack
.pop() != match
.charAt(0)) return {pos
: pos
, match
: false};
32 else if (!stack
.length
) return {pos
: pos
, match
: true};
36 for (var i
= cur
.line
, found
, e
= forward
? Math
.min(i
+ maxScanLines
, cm
.lineCount()) : Math
.max(-1, i
- maxScanLines
); i
!= e
; i
+=d
) {
37 if (i
== cur
.line
) found
= scan(line
, i
, pos
);
38 else found
= scan(cm
.getLineHandle(i
), i
);
41 return {from: Pos(cur
.line
, pos
), to
: found
&& Pos(i
, found
.pos
),
42 match
: found
&& found
.match
, forward
: forward
};
45 function matchBrackets(cm
, autoclear
) {
46 // Disable brace matching in long lines, since it'll cause hugely slow updates
47 var maxHighlightLen
= cm
.state
.matchBrackets
.maxHighlightLineLength
|| 1000;
48 var found
= findMatchingBracket(cm
);
49 if (!found
|| cm
.getLine(found
.from.line
).length
> maxHighlightLen
||
50 found
.to
&& cm
.getLine(found
.to
.line
).length
> maxHighlightLen
)
53 var style
= found
.match
? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
54 var one
= cm
.markText(found
.from, Pos(found
.from.line
, found
.from.ch
+ 1), {className
: style
});
55 var two
= found
.to
&& cm
.markText(found
.to
, Pos(found
.to
.line
, found
.to
.ch
+ 1), {className
: style
});
56 // Kludge to work around the IE bug from issue #1193, where text
57 // input stops going to the textarea whenever this fires.
58 if (ie_lt8
&& cm
.state
.focused
) cm
.display
.input
.focus();
59 var clear = function() {
60 cm
.operation(function() { one
.clear(); two
&& two
.clear(); });
62 if (autoclear
) setTimeout(clear
, 800);
66 var currentlyHighlighted
= null;
67 function doMatchBrackets(cm
) {
68 cm
.operation(function() {
69 if (currentlyHighlighted
) {currentlyHighlighted(); currentlyHighlighted
= null;}
70 if (!cm
.somethingSelected()) currentlyHighlighted
= matchBrackets(cm
, false);
74 CodeMirror
.defineOption("matchBrackets", false, function(cm
, val
, old
) {
75 if (old
&& old
!= CodeMirror
.Init
)
76 cm
.off("cursorActivity", doMatchBrackets
);
78 cm
.state
.matchBrackets
= typeof val
== "object" ? val
: {};
79 cm
.on("cursorActivity", doMatchBrackets
);
83 CodeMirror
.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
84 CodeMirror
.defineExtension("findMatchingBracket", function(pos
, strict
){
85 return findMatchingBracket(this, pos
, strict
);
This page took 0.064584 seconds and 5 git commands to generate.