Remove Fold.compare/contains functions. Rename Fold.sameLine to Fold.sameRow. Add new functions to Range that repalce the Fold.compare/contains functions but are more explicit on where to drop the start/end.
This commit is contained in:
parent
15d2c50117
commit
56f74f1368
3 changed files with 85 additions and 53 deletions
|
|
@ -935,7 +935,7 @@ var EditSession = function(text, mode) {
|
|||
var folds = foldLine.folds;
|
||||
for (var i = folds.indexOf(ret.fold); i < folds.length; i++) {
|
||||
folds[i].start.column += len;
|
||||
if (!folds[i].sameLine) {
|
||||
if (!folds[i].sameRow) {
|
||||
break;
|
||||
}
|
||||
folds[i].end.column += len;
|
||||
|
|
@ -1274,21 +1274,10 @@ var EditSession = function(text, mode) {
|
|||
foldLine = null, lastFoldLine = null;
|
||||
|
||||
// Clamp the docRow position in case it's inside of a folded block.
|
||||
foldLine = this.getFoldLine(docRow);
|
||||
if (foldLine) {
|
||||
folds = foldLine.folds;
|
||||
for (var i = 0; i < folds.length; i++) {
|
||||
fold = folds[i];
|
||||
comp = fold.compare(docRow, docColumn);
|
||||
if (comp == 0) {
|
||||
docRow = fold.start.row;
|
||||
docColumn = fold.start.column;
|
||||
break;
|
||||
} else if (comp == -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
foldLine = null;
|
||||
fold = this.getFoldAt(docRow, docColumn, -1);
|
||||
if (fold) {
|
||||
docRow = fold.start.row;
|
||||
docColumn = fold.start.column;
|
||||
}
|
||||
|
||||
for (var row = 0; row < docRow; row++) {
|
||||
|
|
@ -1437,21 +1426,7 @@ var EditSession = function(text, mode) {
|
|||
this.start = range.start;
|
||||
this.end = range.end;
|
||||
|
||||
this.sameLine = range.start.row == range.end.row;
|
||||
}
|
||||
|
||||
Fold.prototype.contains = function(row, column) {
|
||||
if (this.end.row == row && this.end.column == column) {
|
||||
return false;
|
||||
}
|
||||
return this.range.contains(row, column);
|
||||
}
|
||||
|
||||
Fold.prototype.compare = function(row, column) {
|
||||
if (this.end.row == row && this.end.column == column) {
|
||||
return 1;
|
||||
}
|
||||
return this.range.compare(row, column);
|
||||
this.sameRow = range.start.row == range.end.row;
|
||||
}
|
||||
|
||||
function FoldLine(fold) {
|
||||
|
|
@ -1472,13 +1447,13 @@ var EditSession = function(text, mode) {
|
|||
}
|
||||
|
||||
this.addFold = function(fold) {
|
||||
if (fold.sameLine) {
|
||||
if (fold.sameRow) {
|
||||
if (fold.start.row < this.startRow || fold.endRow > this.endRow) {
|
||||
throw "Can't add a fold to this FoldLine as it has no connection";
|
||||
}
|
||||
this.folds.push(fold);
|
||||
this.folds.sort(function(a, b) {
|
||||
return -a.compare(b.start.row, b.start.column);
|
||||
return -a.range.compareEnd(b.start.row, b.start.column);
|
||||
});
|
||||
} else if (fold.start.row == this.end.row) {
|
||||
this.folds.push(fold);
|
||||
|
|
@ -1521,7 +1496,7 @@ var EditSession = function(text, mode) {
|
|||
for (var i = 0; i < folds.length; i++) {
|
||||
fold = folds[i];
|
||||
|
||||
comp = fold.compare(endRow, endColumn);
|
||||
comp = fold.range.compareEnd(endRow, endColumn);
|
||||
// This fold is after the endRow/Column.
|
||||
if (comp == -1) {
|
||||
callback(null, endRow, endColumn, lastEnd, isNewRow);
|
||||
|
|
@ -1542,7 +1517,7 @@ var EditSession = function(text, mode) {
|
|||
|
||||
// Note the new lastEnd might not be on the same line. However,
|
||||
// it's the callback's job to recognize this.
|
||||
isNewRow = !fold.sameLine;
|
||||
isNewRow = !fold.sameRow;
|
||||
lastEnd = fold.end.column;
|
||||
}
|
||||
callback(null, endRow, endColumn, lastEnd, isNewRow);
|
||||
|
|
@ -1552,7 +1527,7 @@ var EditSession = function(text, mode) {
|
|||
var fold;
|
||||
for (var i = 0; i < this.folds.length; i++) {
|
||||
fold = this.folds[i];
|
||||
cmp = fold.compare(row, column);
|
||||
cmp = fold.range.compareEnd(row, column);
|
||||
if (cmp == -1) {
|
||||
return {
|
||||
fold: fold,
|
||||
|
|
@ -1576,7 +1551,7 @@ var EditSession = function(text, mode) {
|
|||
// TODO: Refactor to use getNextFoldTo function.
|
||||
for (var i = 0; i < this.folds.length; i++) {
|
||||
fold = this.folds[i];
|
||||
cmp = fold.compare(row, column);
|
||||
cmp = fold.range.compareEnd(row, column);
|
||||
if (cmp == -1) {
|
||||
str = session.getLine(fold.start.row).
|
||||
substring(lastFold.end.column, fold.start.column);
|
||||
|
|
@ -1609,18 +1584,12 @@ var EditSession = function(text, mode) {
|
|||
for (var i = 0; i < folds.length; i++) {
|
||||
fold = folds[i];
|
||||
if (fold.range.contains(row, column)) {
|
||||
if (side == -1) {
|
||||
if (fold.start.row != row || fold.start.column != column) {
|
||||
return fold;
|
||||
}
|
||||
} else if (side == 1) {
|
||||
if (fold.end.row != row || fold.end.column != column) {
|
||||
return fold;
|
||||
}
|
||||
} else if (side == 0) {
|
||||
return fold;
|
||||
if (side == -1 && fold.range.isEnd(row, column)) {
|
||||
return null;
|
||||
} else if (side == 1 && fold.range.isStart(row, column)) {
|
||||
return null
|
||||
}
|
||||
return null;
|
||||
return fold;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
@ -1690,7 +1659,7 @@ var EditSession = function(text, mode) {
|
|||
} else if (startRow == foldLine.end.row) {
|
||||
foldLine.addFold(fold);
|
||||
added = true;
|
||||
if (!fold.sameLine) {
|
||||
if (!fold.sameRow) {
|
||||
// Check if we might have to merge two FoldLines.
|
||||
foldLineNext = foldData[i + 1];
|
||||
if (foldLineNext && foldLineNext.start.row == endRow) {
|
||||
|
|
|
|||
|
|
@ -60,6 +60,47 @@ var Range = function(startRow, startColumn, endRow, endColumn) {
|
|||
return this.compare(row, column) == 0;
|
||||
};
|
||||
|
||||
this.isEnd = function(row, column) {
|
||||
return this.end.row == row && this.end.column == column;
|
||||
}
|
||||
|
||||
this.isStart = function(row, column) {
|
||||
return this.start.row == row && this.start.column == column;
|
||||
}
|
||||
|
||||
this.inside = function(row, column) {
|
||||
if (this.compare(row, column) == 0) {
|
||||
if (this.isEnd(row, column) || this.isStart(row, column)) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
this.insideStart = function(row, column) {
|
||||
if (this.compare(row, column) == 0) {
|
||||
if (this.isEnd(row, column)) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
this.insideEnd = function(row, column) {
|
||||
if (this.compare(row, column) == 0) {
|
||||
if (this.isStart(row, column)) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
this.compare = function(row, column) {
|
||||
if (!this.isMultiLine()) {
|
||||
if (row === this.start.row) {
|
||||
|
|
@ -82,6 +123,28 @@ var Range = function(startRow, startColumn, endRow, endColumn) {
|
|||
return 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Like .compare(), but if isStart is true, return -1;
|
||||
*/
|
||||
this.compareStart = function(row, column) {
|
||||
if (this.start.row == row && this.start.column == column) {
|
||||
return -1;
|
||||
} else {
|
||||
return this.compare(row, column);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Like .compare(), but if isEnd is true, return 1;
|
||||
*/
|
||||
this.compareEnd = function(row, column) {
|
||||
if (this.end.row == row && this.end.column == column) {
|
||||
return 1;
|
||||
} else {
|
||||
return this.compare(row, column);
|
||||
}
|
||||
}
|
||||
|
||||
this.clipRows = function(firstRow, lastRow) {
|
||||
if (this.end.row > lastRow) {
|
||||
var end = {
|
||||
|
|
|
|||
|
|
@ -265,7 +265,7 @@ var Selection = function(session) {
|
|||
var cursor = this.selectionLead.getPosition(),
|
||||
fold;
|
||||
|
||||
if (fold = this.session.getFoldAt(cursor.row, cursor.column, -1)) {
|
||||
if (fold = this.session.getFoldAt(cursor.row, cursor.column, 1)) {
|
||||
this.moveCursorTo(fold.start.row, fold.start.column);
|
||||
} else if (cursor.column == 0) {
|
||||
// cursor is a line (start
|
||||
|
|
@ -285,7 +285,7 @@ var Selection = function(session) {
|
|||
this.moveCursorRight = function() {
|
||||
var cursor = this.selectionLead.getPosition(),
|
||||
fold;
|
||||
if (fold = this.session.getFoldAt(cursor.row, cursor.column, 1)) {
|
||||
if (fold = this.session.getFoldAt(cursor.row, cursor.column, -1)) {
|
||||
this.moveCursorTo(fold.end.row, fold.end.column);
|
||||
} else if (this.selectionLead.column == this.doc.getLine(this.selectionLead.row).length) {
|
||||
if (this.selectionLead.row < this.doc.getLength() - 1) {
|
||||
|
|
@ -348,7 +348,7 @@ var Selection = function(session) {
|
|||
this.session.tokenRe.lastIndex = 0;
|
||||
|
||||
var fold;
|
||||
if (fold = this.session.getFoldAt(row, column, 1)) {
|
||||
if (fold = this.session.getFoldAt(row, column, -1)) {
|
||||
this.moveCursorTo(fold.end.row, fold.end.column);
|
||||
return;
|
||||
} else if (column == line.length) {
|
||||
|
|
@ -372,7 +372,7 @@ var Selection = function(session) {
|
|||
var column = this.selectionLead.column;
|
||||
|
||||
var fold;
|
||||
if (fold = this.session.getFoldAt(row, column, -1)) {
|
||||
if (fold = this.session.getFoldAt(row, column, 1)) {
|
||||
this.moveCursorTo(fold.start.row, fold.start.column);
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue