Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit fe524fc

Browse files
committed
Merge pull request #11584 from thehogfather/code-folding-unit-tests
Code folding unit tests
2 parents c670ffe + e2ec5a9 commit fe524fc

File tree

4 files changed

+485
-7
lines changed

4 files changed

+485
-7
lines changed

src/extensions/default/CodeFolding/foldhelpers/foldcode.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ define(function (require, exports, module) {
9090
});
9191

9292
textRange.on("clear", function (from, to) {
93-
delete cm._lineFolds[from.line];
94-
CodeMirror.signal(cm, "unfold", cm, from, to);
93+
delete cm._lineFolds[pos.line];
94+
CodeMirror.signal(cm, "unfold", cm, from, to, pos.line);
9595
});
9696

9797
if (force === "fold") {
@@ -101,7 +101,7 @@ define(function (require, exports, module) {
101101
delete cm._lineFolds[pos.line];
102102
}
103103

104-
CodeMirror.signal(cm, force, cm, range.from, range.to);
104+
CodeMirror.signal(cm, force, cm, range.from, range.to, pos.line);
105105
return range;
106106
}
107107

src/extensions/default/CodeFolding/foldhelpers/foldgutter.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,19 +355,30 @@ define(function (require, exports, module) {
355355
* @param {!CodeMirror} cm the CodeMirror instance for the active editor
356356
* @param {!Object} from the ch and line position that designates the start of the region
357357
* @param {!Object} to the ch and line position that designates the end of the region
358+
* @param {?Number} gutterLineNumber the gutter line number that was clicked to signal the fold event
358359
*/
359-
function onFold(cm, from, to) {
360-
updateFoldInfo(cm, from.line, from.line + 1);
360+
function onFold(cm, from, to, gutterLineNumber) {
361+
var state = cm.state.foldGutter,
362+
line = isNaN(gutterLineNumber) ? from.line : gutterLineNumber;
363+
if (line >= state.from && line < state.to) {
364+
updateFoldInfo(cm, line, line + 1);
365+
}
361366
}
362367

363368
/**
364369
* Triggered when a folded code segment is unfolded.
365370
* @param {!CodeMirror} cm the CodeMirror instance for the active editor
366371
* @param {!{line:number, ch:number}} from the ch and line position that designates the start of the region
367372
* @param {!{line:number, ch:number}} to the ch and line position that designates the end of the region
373+
* @param {?Number} gutterLineNumber the gutter line number that was clicked to signal the fold event
368374
*/
369-
function onUnFold(cm, from, to) {
370-
updateFoldInfo(cm, from.line, from.line + 1);
375+
function onUnFold(cm, from, to, gutterLineNumber) {
376+
var state = cm.state.foldGutter,
377+
line = isNaN(gutterLineNumber) ? from.line : gutterLineNumber;
378+
var vp = cm.getViewport();
379+
if (line >= state.from && line < state.to) {
380+
updateFoldInfo(cm, line, Math.min(vp.to, to.line));
381+
}
371382
}
372383

373384
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Synchronises the code folding states in the CM doc to cm._lineFolds cache.
3+
* When an undo operation is done, if folded code fragments are restored, then
4+
* we need to update cm._lineFolds with the fragments
5+
* @param {Object} cm cm the CodeMirror instance for the active editor
6+
* @param {Object} from starting position in the doc to sync the fold states from
7+
* @param {[[Type]]} lineAdded a number to show how many lines where added to the document
8+
*/
9+
function syncDocToFoldsCache(cm, from, lineAdded) {
10+
var minFoldSize = prefs.getSetting("minFoldSize") || 2;
11+
var opts = cm.state.foldGutter.options || {};
12+
var rf = opts.rangeFinder || CodeMirror.fold.auto;
13+
var i, pos, folds, fold, range;
14+
if (lineAdded <= 0) {
15+
return;
16+
}
17+
18+
for (i = from; i <= from + lineAdded; i = i + 1) {
19+
pos = CodeMirror.Pos(i);
20+
folds = cm.doc.findMarksAt(pos).filter(isFold);
21+
fold = folds.length ? fold = folds[0] : undefined;
22+
if (fold) {
23+
range = rf(cm, CodeMirror.Pos(i));
24+
if (range && range.to.line - range.from.line >= minFoldSize) {
25+
cm._lineFolds[i] = range;
26+
i = range.to.line;
27+
} else {
28+
delete cm._lineFolds[i];
29+
}
30+
}
31+
}
32+
33+
}

0 commit comments

Comments
 (0)