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
8 })(function(CodeMirror
) {
11 CodeMirror
.registerHelper("fold", "brace", function(cm
, start
) {
12 var line
= start
.line
, lineText
= cm
.getLine(line
);
13 var startCh
, tokenType
;
15 function findOpening(openCh
) {
16 for (var at
= start
.ch
, pass
= 0;;) {
17 var found
= at
<= 0 ? -1 : lineText
.lastIndexOf(openCh
, at
- 1);
24 if (pass
== 1 && found
< start
.ch
) break;
25 tokenType
= cm
.getTokenTypeAt(CodeMirror
.Pos(line
, found
+ 1));
26 if (!/^(comment|string)/.test(tokenType
)) return found
+ 1;
31 var startToken
= "{", endToken
= "}", startCh
= findOpening("{");
32 if (startCh
== null) {
33 startToken
= "[", endToken
= "]";
34 startCh
= findOpening("[");
37 if (startCh
== null) return;
38 var count
= 1, lastLine
= cm
.lastLine(), end
, endCh
;
39 outer
: for (var i
= line
; i
<= lastLine
; ++i
) {
40 var text
= cm
.getLine(i
), pos
= i
== line
? startCh
: 0;
42 var nextOpen
= text
.indexOf(startToken
, pos
), nextClose
= text
.indexOf(endToken
, pos
);
43 if (nextOpen
< 0) nextOpen
= text
.length
;
44 if (nextClose
< 0) nextClose
= text
.length
;
45 pos
= Math
.min(nextOpen
, nextClose
);
46 if (pos
== text
.length
) break;
47 if (cm
.getTokenTypeAt(CodeMirror
.Pos(i
, pos
+ 1)) == tokenType
) {
48 if (pos
== nextOpen
) ++count
;
49 else if (!--count
) { end
= i
; endCh
= pos
; break outer
; }
54 if (end
== null || line
== end
&& endCh
== startCh
) return;
55 return {from: CodeMirror
.Pos(line
, startCh
),
56 to
: CodeMirror
.Pos(end
, endCh
)};
59 CodeMirror
.registerHelper("fold", "import", function(cm
, start
) {
60 function hasImport(line
) {
61 if (line
< cm
.firstLine() || line
> cm
.lastLine()) return null;
62 var start
= cm
.getTokenAt(CodeMirror
.Pos(line
, 1));
63 if (!/\S/.test(start
.string
)) start
= cm
.getTokenAt(CodeMirror
.Pos(line
, start
.end
+ 1));
64 if (start
.type
!= "keyword" || start
.string
!= "import") return null;
65 // Now find closing semicolon, return its position
66 for (var i
= line
, e
= Math
.min(cm
.lastLine(), line
+ 10); i
<= e
; ++i
) {
67 var text
= cm
.getLine(i
), semi
= text
.indexOf(";");
68 if (semi
!= -1) return {startCh
: start
.end
, end
: CodeMirror
.Pos(i
, semi
)};
72 var start
= start
.line
, has
= hasImport(start
), prev
;
73 if (!has
|| hasImport(start
- 1) || ((prev
= hasImport(start
- 2)) && prev
.end
.line
== start
- 1))
75 for (var end
= has
.end
;;) {
76 var next
= hasImport(end
.line
+ 1);
77 if (next
== null) break;
80 return {from: cm
.clipPos(CodeMirror
.Pos(start
, has
.startCh
+ 1)), to
: end
};
83 CodeMirror
.registerHelper("fold", "include", function(cm
, start
) {
84 function hasInclude(line
) {
85 if (line
< cm
.firstLine() || line
> cm
.lastLine()) return null;
86 var start
= cm
.getTokenAt(CodeMirror
.Pos(line
, 1));
87 if (!/\S/.test(start
.string
)) start
= cm
.getTokenAt(CodeMirror
.Pos(line
, start
.end
+ 1));
88 if (start
.type
== "meta" && start
.string
.slice(0, 8) == "#include") return start
.start
+ 8;
91 var start
= start
.line
, has
= hasInclude(start
);
92 if (has
== null || hasInclude(start
- 1) != null) return null;
93 for (var end
= start
;;) {
94 var next
= hasInclude(end
+ 1);
95 if (next
== null) break;
98 return {from: CodeMirror
.Pos(start
, has
+ 1),
99 to
: cm
.clipPos(CodeMirror
.Pos(end
))};