-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcm-folding.js
151 lines (131 loc) · 5.38 KB
/
cm-folding.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var CodeMirror = require('codemirror');
/* global CodeMirror */
(function() {
"use strict";
function doFold(cm, pos, options) {
var finder = options && (options.call ? options : options.rangeFinder);
if (!finder) finder = cm.getHelper(pos, "fold");
if (!finder) return;
if (typeof pos == "number") pos = CodeMirror.Pos(pos, 0);
var minSize = options && options.minFoldSize || 0;
function getRange(allowFolded) {
var range = finder(cm, pos);
if (!range || range.to.line - range.from.line < minSize) return null;
var marks = cm.findMarksAt(range.from);
for (var i = 0; i < marks.length; ++i) {
if (marks[i].__isFold) {
if (!allowFolded) return null;
range.cleared = true;
marks[i].clear();
}
}
return range;
}
var range = getRange(true);
if (options && options.scanUp) while (!range && pos.line > cm.firstLine()) {
pos = CodeMirror.Pos(pos.line - 1, 0);
range = getRange(false);
}
if (!range || range.cleared) return;
var myWidget = makeWidget(options);
CodeMirror.on(myWidget, "mousedown", function() { myRange.clear(); });
var myRange = cm.markText(range.from, range.to, {
replacedWith: myWidget,
clearOnEnter: true,
__isFold: true
});
myRange.on("clear", function(from, to) {
CodeMirror.signal(cm, "unfold", cm, from, to);
});
CodeMirror.signal(cm, "fold", cm, range.from, range.to);
}
function makeWidget(options) {
var widget = (options && options.widget) || "\u2194";
if (typeof widget == "string") {
var text = document.createTextNode(widget);
widget = document.createElement("span");
widget.appendChild(text);
widget.className = "CodeMirror-foldmarker";
}
return widget;
}
// Clumsy backwards-compatible interface
CodeMirror.newFoldFunction = function(rangeFinder, widget) {
return function(cm, pos) { doFold(cm, pos, {rangeFinder: rangeFinder, widget: widget}); };
};
// New-style interface
CodeMirror.defineExtension("foldCode", function(pos, options) { doFold(this, pos, options); });
CodeMirror.registerHelper("fold", "combine", function() {
var funcs = Array.prototype.slice.call(arguments, 0);
return function(cm, start) {
for (var i = 0; i < funcs.length; ++i) {
var found = funcs[i](cm, start);
if (found) return found;
}
};
});
})();
var myRangeFinder = function(cm, start) {
var line = start.line, lineText = cm.getLine(line);
//if the line has a comment fold, then do that:
if (lineText.indexOf(";;;fold:") != -1) return tripleCommentRangeFinder(cm, start)
var startCh, tokenType;
// function findOpening(openCh) {
// for (var at = start.ch, pass = 0;;) {
// var found = at <= 0 ? -1 : lineText.lastIndexOf(openCh, at - 1);
// if (found == -1) {
// if (pass == 1) break;
// pass = 1;
// at = lineText.length;
// continue;
// }
// if (pass == 1 && found < start.ch) break;
// tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1));
// if (!/^(comment|string)/.test(tokenType)) return found + 1;
// at = found - 1;
// }
// }
var startToken = "(", endToken = ")";
var startCh = lineText.lastIndexOf("(", start.ch);
if (startCh == -1) return;
startCh++;
tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, startCh));
var count = 1, lastLine = cm.lastLine(), end, endCh;
outer: for (var i = line; i <= lastLine; ++i) {
var text = cm.getLine(i), pos = i == line ? startCh : 0;
for (;;) {
var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos);
if (nextOpen < 0) nextOpen = text.length;
if (nextClose < 0) nextClose = text.length;
pos = Math.min(nextOpen, nextClose);
if (pos == text.length) break;
if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == tokenType) {
if (pos == nextOpen) ++count;
else if (!--count) { end = i; endCh = pos; break outer; }
}
++pos;
}
}
if (end == null || line == end && endCh == startCh) return;
return {from: CodeMirror.Pos(line, startCh),
to: CodeMirror.Pos(end, endCh)};
}
//if we want to fold what's between ";;;fold:" and ";;;".
//assume that start is already the line with ";;;fold:"
//so find the next ";;;" and return the range from start line + 1 to match.
var tripleCommentRangeFinder = function(cm, start) {
var lastLine = cm.lastLine();
var pos;
for (var i = start.line+1; i<=lastLine; i++) {
var text = cm.getLine(i)
pos = text.indexOf(";;;")
if (pos==0) {
var endCh = cm.getLine(i).length
return {from: CodeMirror.Pos(start.line+1, 0), to: CodeMirror.Pos(i, endCh)}; }
}
return;
}
module.exports = {
tripleCommentRangeFinder: tripleCommentRangeFinder,
myRangeFinder: myRangeFinder
}