Get move(Word)Right/left working with folds
This commit is contained in:
parent
88f55c958a
commit
8c63cd7ff5
2 changed files with 121 additions and 10 deletions
|
|
@ -1502,8 +1502,92 @@ var EditSession = function(text, mode) {
|
||||||
}
|
}
|
||||||
callback(null, endRow, endColumn, lastEnd, isNewRow);
|
callback(null, endRow, endColumn, lastEnd, isNewRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.getStringAt = function(session, row, column, trim) {
|
||||||
|
var fold, lastFold, cmp, str;
|
||||||
|
lastFold = {
|
||||||
|
end: { column: 0 }
|
||||||
|
};
|
||||||
|
for (var i = 0; i < this.folds.length; i++) {
|
||||||
|
fold = this.folds[i];
|
||||||
|
cmp = fold.compare(row, column);
|
||||||
|
if (cmp == -1) {
|
||||||
|
str = session.getLine(fold.start.row).
|
||||||
|
substring(lastFold.end.column, fold.start.column);
|
||||||
|
break;
|
||||||
|
} else if (cmp == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
lastFold = fold;
|
||||||
|
}
|
||||||
|
if (!str) {
|
||||||
|
str = session.getLine(fold.start.row).
|
||||||
|
substring(lastFold.end.column);
|
||||||
|
}
|
||||||
|
if (trim == -1) {
|
||||||
|
return str.substring(0, column - lastFold.end.column);
|
||||||
|
} else if (trim == 1) {
|
||||||
|
return str.substring(column - lastFold.end.column)
|
||||||
|
} else {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
}).call(FoldLine.prototype);
|
}).call(FoldLine.prototype);
|
||||||
|
|
||||||
|
this.getFoldAt = function(row, column, side) {
|
||||||
|
var foldLine = this.getFoldLine(row);
|
||||||
|
if (foldLine) {
|
||||||
|
var folds = foldLine.folds,
|
||||||
|
fold;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the string between folds at the given position.
|
||||||
|
* E.g.
|
||||||
|
* foo<fold>b|ar<fold>wolrd -> "bar"
|
||||||
|
* foo<fold>bar<fold>wol|rd -> "world"
|
||||||
|
* foo<fold>bar<fo|ld>wolrd -> <null>
|
||||||
|
*
|
||||||
|
* where | means the position of row/column
|
||||||
|
*
|
||||||
|
* The trim option determs if the return string should be trimed according
|
||||||
|
* to the "side" passed with the trim value:
|
||||||
|
*
|
||||||
|
* E.g.
|
||||||
|
* foo<fold>b|ar<fold>wolrd -trim=-1> "b"
|
||||||
|
* foo<fold>bar<fold>wol|rd -trim=+1> "rld"
|
||||||
|
* fo|o<fold>bar<fold>wolrd -trim=00> "foo"
|
||||||
|
*/
|
||||||
|
this.getFoldStringAt = function(row, column, trim) {
|
||||||
|
var foldLine = this.getFoldLine(row);
|
||||||
|
if (!foldLine) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return foldLine.getStringAt(this, row, column, trim);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.getFoldLine = function(docRow, startFoldLine) {
|
this.getFoldLine = function(docRow, startFoldLine) {
|
||||||
var foldData = this.$foldData;
|
var foldData = this.$foldData;
|
||||||
var i = Math.max(foldData.indexOf(startFoldLine), 0);
|
var i = Math.max(foldData.indexOf(startFoldLine), 0);
|
||||||
|
|
|
||||||
|
|
@ -262,8 +262,12 @@ var Selection = function(session) {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.moveCursorLeft = function() {
|
this.moveCursorLeft = function() {
|
||||||
var cursor = this.selectionLead.getPosition();
|
var cursor = this.selectionLead.getPosition(),
|
||||||
if (cursor.column == 0) {
|
fold;
|
||||||
|
|
||||||
|
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
|
// cursor is a line (start
|
||||||
if (cursor.row > 0) {
|
if (cursor.row > 0) {
|
||||||
this.moveCursorTo(cursor.row - 1, this.doc.getLine(cursor.row - 1).length);
|
this.moveCursorTo(cursor.row - 1, this.doc.getLine(cursor.row - 1).length);
|
||||||
|
|
@ -279,7 +283,11 @@ var Selection = function(session) {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.moveCursorRight = function() {
|
this.moveCursorRight = function() {
|
||||||
if (this.selectionLead.column == this.doc.getLine(this.selectionLead.row).length) {
|
var cursor = this.selectionLead.getPosition(),
|
||||||
|
fold;
|
||||||
|
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) {
|
if (this.selectionLead.row < this.doc.getLength() - 1) {
|
||||||
this.moveCursorTo(this.selectionLead.row + 1, 0);
|
this.moveCursorTo(this.selectionLead.row + 1, 0);
|
||||||
}
|
}
|
||||||
|
|
@ -339,7 +347,11 @@ var Selection = function(session) {
|
||||||
this.session.nonTokenRe.lastIndex = 0;
|
this.session.nonTokenRe.lastIndex = 0;
|
||||||
this.session.tokenRe.lastIndex = 0;
|
this.session.tokenRe.lastIndex = 0;
|
||||||
|
|
||||||
if (column == line.length) {
|
var fold;
|
||||||
|
if (fold = this.session.getFoldAt(row, column, 1)) {
|
||||||
|
this.moveCursorTo(fold.end.row, fold.end.column);
|
||||||
|
return;
|
||||||
|
} else if (column == line.length) {
|
||||||
this.moveCursorRight();
|
this.moveCursorRight();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -358,18 +370,33 @@ var Selection = function(session) {
|
||||||
this.moveCursorWordLeft = function() {
|
this.moveCursorWordLeft = function() {
|
||||||
var row = this.selectionLead.row;
|
var row = this.selectionLead.row;
|
||||||
var column = this.selectionLead.column;
|
var column = this.selectionLead.column;
|
||||||
var line = this.doc.getLine(row);
|
|
||||||
var leftOfCursor = lang.stringReverse(line.substring(0, column));
|
|
||||||
|
|
||||||
var match;
|
var fold;
|
||||||
this.session.nonTokenRe.lastIndex = 0;
|
if (fold = this.session.getFoldAt(row, column, -1)) {
|
||||||
this.session.tokenRe.lastIndex = 0;
|
this.moveCursorTo(fold.start.row, fold.start.column);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (column == 0) {
|
if (column == 0) {
|
||||||
this.moveCursorLeft();
|
this.moveCursorLeft();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (match = this.session.nonTokenRe.exec(leftOfCursor)) {
|
|
||||||
|
var str = null;
|
||||||
|
var foldLine = this.session.getFoldLine(row);
|
||||||
|
if (foldLine) {
|
||||||
|
str = foldLine.getStringAt(this.session, row, column, -1);
|
||||||
|
}
|
||||||
|
if (str == null) {
|
||||||
|
str = this.doc.getLine(row).substring(0, column)
|
||||||
|
}
|
||||||
|
var leftOfCursor = lang.stringReverse(str);
|
||||||
|
|
||||||
|
var match;
|
||||||
|
this.session.nonTokenRe.lastIndex = 0;
|
||||||
|
this.session.tokenRe.lastIndex = 0;
|
||||||
|
|
||||||
|
if (match = this.session.nonTokenRe.exec(leftOfCursor)) {
|
||||||
column -= this.session.nonTokenRe.lastIndex;
|
column -= this.session.nonTokenRe.lastIndex;
|
||||||
this.session.nonTokenRe.lastIndex = 0;
|
this.session.nonTokenRe.lastIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue