1 |
|
2 |
|
3 |
|
4 | (function(mod) {
|
5 | if (typeof exports == "object" && typeof module == "object")
|
6 | mod(require("../../lib/codemirror"));
|
7 | else if (typeof define == "function" && define.amd)
|
8 | define(["../../lib/codemirror"], mod);
|
9 | else
|
10 | mod(CodeMirror);
|
11 | })(function(CodeMirror) {
|
12 | "use strict";
|
13 |
|
14 | function lineIndent(cm, lineNo) {
|
15 | var text = cm.getLine(lineNo)
|
16 | var spaceTo = text.search(/\S/)
|
17 | if (spaceTo == -1 || /\bcomment\b/.test(cm.getTokenTypeAt(CodeMirror.Pos(lineNo, spaceTo + 1))))
|
18 | return -1
|
19 | return CodeMirror.countColumn(text, null, cm.getOption("tabSize"))
|
20 | }
|
21 |
|
22 | CodeMirror.registerHelper("fold", "indent", function(cm, start) {
|
23 | var myIndent = lineIndent(cm, start.line)
|
24 | if (myIndent < 0) return
|
25 | var lastLineInFold = null
|
26 |
|
27 |
|
28 |
|
29 | for (var i = start.line + 1, end = cm.lastLine(); i <= end; ++i) {
|
30 | var indent = lineIndent(cm, i)
|
31 | if (indent == -1) {
|
32 | } else if (indent > myIndent) {
|
33 |
|
34 | lastLineInFold = i;
|
35 | } else {
|
36 |
|
37 |
|
38 |
|
39 | break;
|
40 | }
|
41 | }
|
42 | if (lastLineInFold) return {
|
43 | from: CodeMirror.Pos(start.line, cm.getLine(start.line).length),
|
44 | to: CodeMirror.Pos(lastLineInFold, cm.getLine(lastLineInFold).length)
|
45 | };
|
46 | });
|
47 |
|
48 | });
|