fix nested folds
This commit is contained in:
parent
6f7a33e39b
commit
4e82a96353
2 changed files with 41 additions and 11 deletions
|
|
@ -31,6 +31,9 @@
|
||||||
define(function(require, exports, module) {
|
define(function(require, exports, module) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
var Range = require("../range").Range;
|
||||||
|
var RangeList = require("../range_list").RangeList;
|
||||||
|
var oop = require("../lib/oop")
|
||||||
/*
|
/*
|
||||||
* Simple fold-data struct.
|
* Simple fold-data struct.
|
||||||
**/
|
**/
|
||||||
|
|
@ -42,9 +45,11 @@ var Fold = exports.Fold = function(range, placeholder) {
|
||||||
this.end = range.end;
|
this.end = range.end;
|
||||||
|
|
||||||
this.sameRow = range.start.row == range.end.row;
|
this.sameRow = range.start.row == range.end.row;
|
||||||
this.subFolds = [];
|
this.subFolds = this.ranges = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
oop.inherits(Fold, RangeList);
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
this.toString = function() {
|
this.toString = function() {
|
||||||
|
|
@ -64,17 +69,21 @@ var Fold = exports.Fold = function(range, placeholder) {
|
||||||
this.subFolds.forEach(function(subFold) {
|
this.subFolds.forEach(function(subFold) {
|
||||||
fold.subFolds.push(subFold.clone());
|
fold.subFolds.push(subFold.clone());
|
||||||
});
|
});
|
||||||
|
fold.collapseChildren = this.collapseChildren;
|
||||||
return fold;
|
return fold;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.addSubFold = function(fold) {
|
this.addSubFold = function(fold) {
|
||||||
if (this.range.isEqual(fold))
|
if (this.range.isEqual(fold))
|
||||||
return this;
|
return;
|
||||||
|
|
||||||
if (!this.range.containsRange(fold))
|
if (!this.range.containsRange(fold))
|
||||||
throw "A fold can't intersect already existing fold" + fold.range + this.range;
|
throw "A fold can't intersect already existing fold" + fold.range + this.range;
|
||||||
|
|
||||||
var row = fold.range.start.row, column = fold.range.start.column;
|
// transform fold to local coordinates
|
||||||
|
consumeRange(fold, this.start);
|
||||||
|
|
||||||
|
var row = fold.start.row, column = fold.start.column;
|
||||||
for (var i = 0, cmp = -1; i < this.subFolds.length; i++) {
|
for (var i = 0, cmp = -1; i < this.subFolds.length; i++) {
|
||||||
cmp = this.subFolds[i].range.compare(row, column);
|
cmp = this.subFolds[i].range.compare(row, column);
|
||||||
if (cmp != 1)
|
if (cmp != 1)
|
||||||
|
|
@ -102,7 +111,30 @@ var Fold = exports.Fold = function(range, placeholder) {
|
||||||
|
|
||||||
return fold;
|
return fold;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.restoreRange = function(range) {
|
||||||
|
return restoreRange(range, this.start);
|
||||||
|
};
|
||||||
|
|
||||||
}).call(Fold.prototype);
|
}).call(Fold.prototype);
|
||||||
|
|
||||||
|
function consumePoint(point, anchor) {
|
||||||
|
point.row -= anchor.row;
|
||||||
|
if (point.row == 0)
|
||||||
|
point.column -= anchor.column;
|
||||||
|
}
|
||||||
|
function consumeRange(range, anchor) {
|
||||||
|
consumePoint(range.start, anchor);
|
||||||
|
consumePoint(range.end, anchor);
|
||||||
|
}
|
||||||
|
function restorePoint(point, anchor) {
|
||||||
|
if (point.row == 0)
|
||||||
|
point.column += anchor.column;
|
||||||
|
point.row += anchor.row;
|
||||||
|
}
|
||||||
|
function restoreRange(range, anchor) {
|
||||||
|
restorePoint(range.start, anchor);
|
||||||
|
restorePoint(range.end, anchor);
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -118,11 +118,6 @@ function Folding() {
|
||||||
|
|
||||||
function addFold(fold) {
|
function addFold(fold) {
|
||||||
folds.push(fold);
|
folds.push(fold);
|
||||||
if (!fold.subFolds)
|
|
||||||
return;
|
|
||||||
|
|
||||||
for (var i = 0; i < fold.subFolds.length; i++)
|
|
||||||
addFold(fold.subFolds[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < foldLines.length; i++)
|
for (var i = 0; i < foldLines.length; i++)
|
||||||
|
|
@ -297,7 +292,9 @@ function Folding() {
|
||||||
// Remove the folds from fold data.
|
// Remove the folds from fold data.
|
||||||
this.removeFolds(folds);
|
this.removeFolds(folds);
|
||||||
// Add the removed folds as subfolds on the new fold.
|
// Add the removed folds as subfolds on the new fold.
|
||||||
fold.subFolds = folds;
|
folds.forEach(function(subFold) {
|
||||||
|
fold.addSubFold(subFold);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < foldData.length; i++) {
|
for (var i = 0; i < foldData.length; i++) {
|
||||||
|
|
@ -415,8 +412,9 @@ function Folding() {
|
||||||
|
|
||||||
this.expandFold = function(fold) {
|
this.expandFold = function(fold) {
|
||||||
this.removeFold(fold);
|
this.removeFold(fold);
|
||||||
fold.subFolds.forEach(function(fold) {
|
fold.subFolds.forEach(function(subFold) {
|
||||||
this.addFold(fold);
|
fold.restoreRange(subFold);
|
||||||
|
this.addFold(subFold);
|
||||||
}, this);
|
}, this);
|
||||||
fold.subFolds = [];
|
fold.subFolds = [];
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue