Add EditSession.getFoldsInRange function + unit tests

This commit is contained in:
Julian Viereck 2011-04-25 15:50:33 +02:00
commit ae20be743f
2 changed files with 72 additions and 9 deletions

View file

@ -1427,6 +1427,7 @@ var EditSession = function(text, mode) {
* Simple fold-data struct.
**/
function Fold(range, placeholder) {
this.foldLine = null;
this.placeholder = placeholder;
this.range = range;
this.start = range.start;
@ -1441,18 +1442,20 @@ var EditSession = function(text, mode) {
*/
function FoldLine(folds) {
if (Array.isArray(folds)) {
var last = folds[folds.length - 1]
this.folds = folds;
this.range = new Range(folds[0].start.row, folds[0].start.column,
last.end.row, last.end.column);
this.start = this.range.start;
this.end = this.range.end;
} else {
this.folds = [folds];
this.range = folds.range.clone();
this.start = this.range.start;
this.end = this.range.end;
folds = this.folds = [ folds ];
}
var last = folds[folds.length - 1]
this.range = new Range(folds[0].start.row, folds[0].start.column,
last.end.row, last.end.column);
this.start = this.range.start;
this.end = this.range.end;
this.folds.forEach(function(fold) {
fold.foldLine = this;
}, this);
}
(function() {
@ -1643,6 +1646,43 @@ var EditSession = function(text, mode) {
}
}
/**
* Returns all folds in the given range. Note, that this will return folds
*
*/
this.getFoldsInRange = function(range) {
var start = range.start,
end = range.end;
var foldLines = this.$foldData,
folds,
fold;
var cmp,
foundFolds = [];
for (var i = 0; i < foldLines.length; i++) {
cmp = foldLines[i].range.compare(start.row, start.column + 1);
if (cmp == 1) {
break;
} else if (cmp == -1) {
cmp = foldLines[i].range.compare(end.row, end.column - 1);
if (cmp == -1) {
break;
}
}
folds = foldLines[i].folds;
for (var j = 0; j < folds.length; j++) {
fold = folds[j];
cmp = fold.range.compare(end.row, end.column + 1);
if (cmp == -1) {
break;
} else {
foundFolds.push(fold);
}
}
}
return foundFolds;
}
/**
* Returns the string between folds at the given position.
* E.g.

View file

@ -541,6 +541,29 @@ module.exports = {
assert.range(foldLines[2].range, 6, 13, 6, 18);
// TODO: Add test for inseration inside of folds.
},
"test getFoldsInRange()": function() {
var session = createFoldTestSession(),
foldLines = session.$foldData;
folds = foldLines[0].folds.concat(foldLines[1].folds);
function test(startRow, startColumn, endColumn, endRow, folds) {
var r = new Range(startRow, startColumn, endColumn, endRow);
var retFolds = session.getFoldsInRange(r);
assert.ok(retFolds.length == folds.length);
for (var i = 0; i < retFolds.length; i++) {
assert.equal(retFolds[i].range + "", folds[i].range + "");
}
}
test(0, 0, 0, 13, [ ]);
test(0, 0, 0, 14, [ folds[0] ]);
test(0, 0, 0, 18, [ folds[0] ]);
test(0, 0, 1, 10, [ folds[0] ]);
test(0, 0, 1, 11, [ folds[0], folds[1] ]);
test(0, 18, 1, 11, [ folds[1] ]);
}
};