Skip to content

Instantly share code, notes, and snippets.

@pvdz
Created October 12, 2012 12:02
Show Gist options
  • Save pvdz/3878890 to your computer and use it in GitHub Desktop.
Save pvdz/3878890 to your computer and use it in GitHub Desktop.
ace get linear caret position wot?
getCharPos1: function() {
// Get cursor position
var charPos = 0;
var cursorPos = this.editor.getCursorPosition();
for (var i=0; i<cursorPos.row; i++){
charPos += this.editor.session.$rowLengthCache[i]; // +1 for newline
}
charPos += cursorPos.column + cursorPos.row; // Add row count to compensate for newlines
return charPos;
},
caretPos2: function(){
var cursorPos = this.editor.getCursorPosition();
var col = cursorPos.column;
var row = cursorPos.row;
var doc = this.editor.session.doc;
var lines = doc.$lines;
var newlineLength = doc.$autoNewLine.length;
var pos = 0;
for (var y=0; y<row; ++y) {
var line = lines[y].length;
pos += line;
}
pos += newlineLength * y;
pos += col;
return pos;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment