add document indexToPosition and positionToIndex

This commit is contained in:
nightwing 2012-12-26 20:31:20 +04:00
commit 0908b41268
2 changed files with 23 additions and 30 deletions

View file

@ -592,6 +592,28 @@ var Document = function(text) {
}
};
this.indexToPosition = function(index, startRow) {
var lines = this.$lines || this.getAllLines();
var newlineLength = this.getNewLineCharacter().length;
for (var i = startRow || 0, l = lines.length; i < l; i++) {
index -= lines[i].length + newlineLength;
if (index < 0)
return {row: i, column: index + lines[i].length + newlineLength};
}
return {row: l-1, column: lines[l-1].length};
};
this.positionToIndex = function(pos, startRow) {
var lines = this.$lines || this.getAllLines();
var newlineLength = this.getNewLineCharacter().length;
var index = 0;
var row = Math.min(pos.row, lines.length);
for (var i = startRow || 0; i < row; ++i)
index += lines[i].length;
return index + newlineLength * i + pos.column;
};
}).call(Document.prototype);
exports.Document = Document;