Add EditSession.getFoldsInRange function + unit tests
This commit is contained in:
parent
ccfe285bab
commit
ae20be743f
2 changed files with 72 additions and 9 deletions
|
|
@ -1427,6 +1427,7 @@ var EditSession = function(text, mode) {
|
||||||
* Simple fold-data struct.
|
* Simple fold-data struct.
|
||||||
**/
|
**/
|
||||||
function Fold(range, placeholder) {
|
function Fold(range, placeholder) {
|
||||||
|
this.foldLine = null;
|
||||||
this.placeholder = placeholder;
|
this.placeholder = placeholder;
|
||||||
this.range = range;
|
this.range = range;
|
||||||
this.start = range.start;
|
this.start = range.start;
|
||||||
|
|
@ -1441,18 +1442,20 @@ var EditSession = function(text, mode) {
|
||||||
*/
|
*/
|
||||||
function FoldLine(folds) {
|
function FoldLine(folds) {
|
||||||
if (Array.isArray(folds)) {
|
if (Array.isArray(folds)) {
|
||||||
var last = folds[folds.length - 1]
|
|
||||||
this.folds = folds;
|
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 {
|
} else {
|
||||||
this.folds = [folds];
|
folds = this.folds = [ folds ];
|
||||||
this.range = folds.range.clone();
|
|
||||||
this.start = this.range.start;
|
|
||||||
this.end = this.range.end;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
(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.
|
* Returns the string between folds at the given position.
|
||||||
* E.g.
|
* E.g.
|
||||||
|
|
|
||||||
|
|
@ -541,6 +541,29 @@ module.exports = {
|
||||||
assert.range(foldLines[2].range, 6, 13, 6, 18);
|
assert.range(foldLines[2].range, 6, 13, 6, 18);
|
||||||
|
|
||||||
// TODO: Add test for inseration inside of folds.
|
// 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] ]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue