Clamp row before column so that selection works at document's end

This commit is contained in:
nightwing 2011-01-31 00:32:53 +08:00 • committed by Fabian Jakobs
commit 1f4c093b6e

View file

@ -918,7 +918,7 @@ var EditSession = function(text, mode) {
/** /**
* *
* @returns array * @returns array
* - array[0]: The documentRow aquivalent. * - array[0]: The documentRow equivalent.
* - array[1]: The screenRowOffset to the first documentRow on the screen. * - array[1]: The screenRowOffset to the first documentRow on the screen.
*/ */
this.$screenToDocumentRow = function(row) { this.$screenToDocumentRow = function(row) {
@ -949,23 +949,24 @@ var EditSession = function(text, mode) {
var docRow; var docRow;
var docColumn; var docColumn;
var remaining = column; var remaining = column;
var linesCount = this.getLength();
if (!this.$useWrapMode) { if (!this.$useWrapMode) {
docRow = row; docRow = row >= linesCount? linesCount-1 : (row < 0 ? 0 : row);
row = 0; row = 0;
docColumn = 0; docColumn = 0;
line = this.getLine(docRow); line = this.getLine(docRow);
} else { } else {
var wrapData = this.$wrapData, linesCount = this.getLength(); var wrapData = this.$wrapData;
var rowData = this.$screenToDocumentRow(row); var docRow = 0;
row = rowData[1]; while (docRow < linesCount && row >= wrapData[docRow].length + 1) {
docRow = rowData[0]; row -= wrapData[docRow].length + 1;
docRow ++;
}
if (docRow >= linesCount) { if (docRow >= linesCount) {
return { docRow = linesCount-1
row: docRow, row = wrapData[docRow].length;
column: 0
};
} }
docColumn = wrapData[docRow][row - 1] || 0; docColumn = wrapData[docRow][row - 1] || 0;
line = this.getLine(docRow).substring(docColumn); line = this.getLine(docRow).substring(docColumn);
@ -1009,15 +1010,16 @@ var EditSession = function(text, mode) {
} }
// Clamp docColumn. // Clamp docColumn.
if (docRow < linesCount && wrapData[docRow][row]) { if (this.$useWrapMode) {
if (docColumn >= wrapData[docRow][row]) { column = wrapData[docRow][row]
if (docColumn >= column) {
// We remove one character at the end such that the docColumn // We remove one character at the end such that the docColumn
// position returned is not associated to the next row on the // position returned is not associated to the next row on the
// screen. // screen.
docColumn = wrapData[docRow][row] - 1; docColumn = column - 1;
} }
} else if (this.getLine(docRow)) { } else if (line) {
docColumn = Math.min(docColumn, this.getLine(docRow).length); docColumn = Math.min(docColumn, line.length);
} }
return { return {