1 CodeMirror
.braceRangeFinder = function(cm
, start
) {
2 var line
= start
.line
, lineText
= cm
.getLine(line
);
3 var at
= lineText
.length
, startChar
, tokenType
;
5 var found
= lineText
.lastIndexOf("{", at
);
6 var startToken
= '{', endToken
= '}';
7 if (found
< start
.ch
) {
8 found
= lineText
.lastIndexOf("[", at
);
9 if (found
< start
.ch
) break;
10 startToken
= '['; endToken
= ']';
13 tokenType
= cm
.getTokenAt(CodeMirror
.Pos(line
, found
+ 1)).type
;
14 if (!/^(comment|string)/.test(tokenType
)) { startChar
= found
; break; }
17 if (startChar
== null || lineText
.lastIndexOf(startToken
) > startChar
) return;
18 var count
= 1, lastLine
= cm
.lineCount(), end
, endCh
;
19 outer
: for (var i
= line
+ 1; i
< lastLine
; ++i
) {
20 var text
= cm
.getLine(i
), pos
= 0;
22 var nextOpen
= text
.indexOf(startToken
, pos
), nextClose
= text
.indexOf(endToken
, pos
);
23 if (nextOpen
< 0) nextOpen
= text
.length
;
24 if (nextClose
< 0) nextClose
= text
.length
;
25 pos
= Math
.min(nextOpen
, nextClose
);
26 if (pos
== text
.length
) break;
27 if (cm
.getTokenAt(CodeMirror
.Pos(i
, pos
+ 1)).type
== tokenType
) {
28 if (pos
== nextOpen
) ++count
;
29 else if (!--count
) { end
= i
; endCh
= pos
; break outer
; }
34 if (end
== null || end
== line
+ 1) return;
35 return {from: CodeMirror
.Pos(line
, startChar
+ 1),
36 to
: CodeMirror
.Pos(end
, endCh
)};