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

Commit

Permalink
Fixing stray fold icons on undo
Browse files Browse the repository at this point in the history
  • Loading branch information
abose committed Apr 14, 2015
1 parent 6194e27 commit 45c7a99
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/extensions/default/CodeFolding/foldhelpers/foldgutter.js
Expand Up @@ -151,6 +151,30 @@ define(function (require, exports, module) {
});
}

function syncDocToFoldsCache(cm, from, lineDiff) {
var minFoldSize = prefs.getSetting("minFoldSize") || 2;
var opts = cm.state.foldGutter.options || {};
var rf = opts.rangeFinder || CodeMirror.fold.auto;
var i, pos, folds, fold, range;
if (lineDiff <= 0) {
return;
}
for (i = from; i <= from + lineDiff; i = i + 1) {
pos = CodeMirror.Pos(i);
folds = cm.doc.findMarksAt(pos);

This comment has been minimized.

Copy link
@thehogfather

thehogfather Apr 15, 2015

Contributor

We should add a filter to the return marks to get only marks related to codefolding e.g.,

 folds = cm.doc.findMarksAt(pos).filter(function (m) { return mark.__isFold;});

This makes retrieving the first item as done in the next line work as intended since other marks might exist on the line.
There is an isFold function updateFoldInfo that can be moved out of that scope and reused for this purpose instead of redefining a lambda expression in the filter argument.

fold = folds.length ? fold = folds[0] : undefined;
if (fold && fold.collapsed) {
range = rf(cm, CodeMirror.Pos(from));
if (range && range.to.line - range.from.line >= minFoldSize) {
cm._lineFolds[from] = range;
} else {
delete cm._lineFolds[from];
}
i = i + range.to.line - range.from.line;
}
}
}

/**
* Updates the line folds cache usually when the document changes.
* @param {!CodeMirror} cm the CodeMirror instance for the active editor
Expand Down Expand Up @@ -179,7 +203,8 @@ define(function (require, exports, module) {
var newFolds = {};
foldedLines.forEach(function (line) {
range = cm._lineFolds[line];
if (line < from || linesDiff === 0) {
if (line < from || linesDiff === 0 ||
(range.from.line >= from && range.to.line <= from + linesDiff && linesDiff > 0)) {
newFolds[line] = range;
} else if (!(range.from.line + linesDiff <= from && linesDiff < 0)) {
// Do not add folds in deleted region to the new folds list
Expand Down Expand Up @@ -209,6 +234,9 @@ define(function (require, exports, module) {
} else {
var state = cm.state.foldGutter;
var lineChanges = changeObj.text.length - changeObj.removed.length;
if (changeObj.origin === "undo" && lineChanges > 0) {
syncDocToFoldsCache(cm, changeObj.from.line, lineChanges);
}
//update the lineFolds cache
updateFoldsCache(cm, changeObj.from.line, lineChanges);
if (lineChanges !== 0) {
Expand Down

0 comments on commit 45c7a99

Please sign in to comment.