Add Range.compareRange function used by EditSession.getFoldsInRange.

This commit is contained in:
Julian Viereck 2011-04-25 22:06:54 +02:00
commit a1497c097d
3 changed files with 70 additions and 19 deletions

View file

@ -1714,6 +1714,7 @@ var EditSession = function(text, mode) {
*
*/
this.getFoldsInRange = function(range) {
range = range.clone();
var start = range.start,
end = range.end;
var foldLines = this.$foldData,
@ -1721,33 +1722,35 @@ var EditSession = function(text, mode) {
fold;
var cmp,
foundFolds = [];
start.column += 1;
end.column -= 1;
for (var i = 0; i < foldLines.length; i++) {
cmp = foldLines[i].range.compare(start.row, start.column + 1);
if (cmp == 1) {
cmp = foldLines[i].range.compareRange(range);
// Range is before foldLine. No intersection. This means,
// there might be other foldLines that intersect.
if (cmp == 2) {
continue;
} else if (cmp == -1) {
cmp = foldLines[i].range.compare(end.row, end.column - 1);
if (cmp == -1) {
break;
}
} else
// Range is after foldLine. There can't be any other foldLines then,
// so let's give up.
if (cmp == -2) {
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) {
cmp = fold.range.compare(start.row, start.column + 1);
if (cmp == 1) {
continue;
}
} else if (cmp == -1) {
cmp = fold.range.compareRange(range);
if (cmp == -2) {
break;
} else if (cmp == 2) {
continue;
} else
// WTF-state: Can happen due to -1/+1 to start/end column.
if (cmp == 42) {
break;
} else {
cmp = fold.range.compare(start.row, start.column + 1);
if (cmp == 1) {
continue;
}
}
foundFolds.push(fold);
}

View file

@ -476,6 +476,7 @@ module.exports = {
test(2, 0, 2, 13, [ folds[1] ]);
test(2, 10, 2, 20, [ ]);
test(2, 10, 2, 11, [ ]);
test(2, 19, 2, 20, [ ]);
},
"test fold one-line text insert": function() {

View file

@ -60,6 +60,53 @@ var Range = function(startRow, startColumn, endRow, endColumn) {
return this.compare(row, column) == 0;
};
/**
* Compares this range (A) with another range (B), where B is the passed in
* range.
*
* Return values:
* -2: (B) is infront of (A) and doesn't intersect with (A)
* -1: (B) begins before (A) but ends inside of (A)
* 0: (B) is completly inside of (A) OR (A) is complety inside of (B)
* +1: (B) begins inside of (A) but ends outside of (A)
* +2: (B) is after (A) and doesn't intersect with (A)
*
* 42: FTW state: (B) ends in (A) but starts outside of (A)
*/
this.compareRange = function(range) {
var cmp,
end = range.end,
start = range.start;
cmp = this.compare(end.row, end.column);
if (cmp == 1) {
cmp = this.compare(start.row, start.column);
if (cmp == 1) {
return 2;
} else if (cmp == 0) {
return 1;
} else {
return 0;
}
} else if (cmp == -1) {
return -2;
} else {
cmp = this.compare(start.row, start.column);
if (cmp == -1) {
return -1;
} else if (cmp == 1) {
return 42;
} else {
return 0;
}
}
}
this.containsRange = function(range) {
var cmp = this.compareRange(range);
return (cmp == -1 || cmp == 0 || cmp == 1);
}
this.isEnd = function(row, column) {
return this.end.row == row && this.end.column == column;
}