Skip to content

Commit 70b92d4

Browse files
authored
Merge pull request #1398 from casperdcl/master
add multi-line block comment folding
2 parents 2029182 + 9f2ed36 commit 70b92d4

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* allow folding of multi-line comments */
2+
CodeMirror.registerHelper("fold", "blockcomment", function(cm, start) {
3+
var mode = cm.getMode(), Token = mode.lineComment;
4+
var lineText = cm.getLine(start.line);
5+
var found = lineText.lastIndexOf(Token,0);
6+
if (found == 0) { // current line is a comment
7+
if (start.line == 0) {
8+
found = -1;
9+
} else {
10+
lineText = cm.getLine(start.line - 1);
11+
found = lineText.lastIndexOf(Token,0);
12+
}
13+
if (start.line == 0 || found != 0) { // no previous comment line
14+
end = start.line;
15+
for (var i=start.line + 1; i<=cm.lastLine(); ++i) { // final comment line
16+
lineText = cm.getLine(i);
17+
found = lineText.lastIndexOf(Token,0);
18+
if (found == 0) {
19+
end = i;
20+
} else {
21+
break;
22+
}
23+
}
24+
if(end > start.line) {
25+
return {from: CodeMirror.Pos(start.line, null),
26+
to: CodeMirror.Pos(end, null)};
27+
}
28+
}
29+
}
30+
return ;
31+
});

src/jupyter_contrib_nbextensions/nbextensions/codefolding/magic-fold.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ CodeMirror.registerHelper("fold", "magic", function(cm, start) {
55
var lineText = cm.getLine(start.line);
66
var found = lineText.lastIndexOf(Token,0);
77
if (found == 0) {
8-
end = cm.lastLine();
8+
end = cm.lastLine();
99
return {from: CodeMirror.Pos(start.line, null),
10-
to: CodeMirror.Pos(end, null)};
10+
to: CodeMirror.Pos(end, null)};
1111
}
1212
}
13-
return ;
13+
return ;
1414
});

src/jupyter_contrib_nbextensions/nbextensions/codefolding/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ define([
140140
rangeFinder: new CodeMirror.fold.combine(
141141
CodeMirror.fold.firstline,
142142
CodeMirror.fold.magic,
143+
CodeMirror.fold.blockcomment,
143144
cm.getMode().fold === 'indent' ? CodeMirror.fold.indent : CodeMirror.fold.brace
144145
)
145146
};
@@ -256,7 +257,7 @@ define([
256257

257258
if (Jupyter.notebook) {
258259
/* require our additional custom codefolding modes before initialising fully */
259-
requirejs(['./firstline-fold', './magic-fold'], function () {
260+
requirejs(['./firstline-fold', './magic-fold', './blockcomment-fold'], function () {
260261
if (Jupyter.notebook._fully_loaded) {
261262
setTimeout(function () {
262263
console.log('Codefolding: Wait for', params.init_delay, 'ms');

0 commit comments

Comments
 (0)