Added getAWordRange and selectAWord methods

These new methods are necessary to select a word along with its right
whitespace, which is a reasonably common scenario.
This commit is contained in:
Sergi Mansilla 2011-11-24 18:18:01 +01:00
commit bb7fd9bb66
2 changed files with 25 additions and 7 deletions

View file

@ -441,6 +441,17 @@ var EditSession = function(text, mode) {
return new Range(row, start, row, end); return new Range(row, start, row, end);
}; };
// Gets the range of a word including its right whitespace
this.getAWordRange = function(row, column) {
var wordRange = this.getWordRange(row, column);
var line = this.getLine(wordRange.end.row);
while (line.charAt(wordRange.end.column).match(/[ \t]/)) {
wordRange.end.column += 1;
}
return wordRange;
};
this.setNewLineMode = function(newLineMode) { this.setNewLineMode = function(newLineMode) {
this.doc.setNewLineMode(newLineMode); this.doc.setNewLineMode(newLineMode);
}; };

View file

@ -45,11 +45,11 @@ var Range = require("./range").Range;
/** /**
* Keeps cursor position and the text selection of an edit session. * Keeps cursor position and the text selection of an edit session.
* *
* The row/columns used in the selection are in document coordinates * The row/columns used in the selection are in document coordinates
* representing ths coordinates as thez appear in the document * representing ths coordinates as thez appear in the document
* before applying soft wrap and folding. * before applying soft wrap and folding.
*/ */
var Selection = function(session) { var Selection = function(session) {
this.session = session; this.session = session;
this.doc = session.getDocument(); this.doc = session.getDocument();
@ -123,7 +123,7 @@ var Selection = function(session) {
}; };
var anchor = this.getSelectionAnchor(); var anchor = this.getSelectionAnchor();
var lead = this.getSelectionLead(); var lead = this.getSelectionLead();
var isBackwards = this.isBackwards(); var isBackwards = this.isBackwards();
@ -253,6 +253,13 @@ var Selection = function(session) {
this.setSelectionRange(range); this.setSelectionRange(range);
}; };
// Selects a word including its right whitespace
this.selectAWord = function() {
var cursor = this.getCursor();
var range = this.session.getAWordRange(cursor.row, cursor.column);
this.setSelectionRange(range);
};
this.selectLine = function() { this.selectLine = function() {
var rowStart = this.selectionLead.row; var rowStart = this.selectionLead.row;
var rowEnd; var rowEnd;
@ -440,10 +447,10 @@ var Selection = function(session) {
this.selectionLead.row, this.selectionLead.row,
this.selectionLead.column this.selectionLead.column
); );
var screenCol = (chars === 0 && this.$desiredColumn) || screenPos.column; var screenCol = (chars === 0 && this.$desiredColumn) || screenPos.column;
var docPos = this.session.screenToDocumentPosition(screenPos.row + rows, screenCol); var docPos = this.session.screenToDocumentPosition(screenPos.row + rows, screenCol);
// move the cursor and update the desired column // move the cursor and update the desired column
this.moveCursorTo(docPos.row, docPos.column + chars, chars === 0); this.moveCursorTo(docPos.row, docPos.column + chars, chars === 0);
}; };
@ -459,11 +466,11 @@ var Selection = function(session) {
row = fold.start.row; row = fold.start.row;
column = fold.start.column; column = fold.start.column;
} }
this.$preventUpdateDesiredColumnOnChange = true; this.$preventUpdateDesiredColumnOnChange = true;
this.selectionLead.setPosition(row, column); this.selectionLead.setPosition(row, column);
this.$preventUpdateDesiredColumnOnChange = false; this.$preventUpdateDesiredColumnOnChange = false;
if (!preventUpdateDesiredColumn) if (!preventUpdateDesiredColumn)
this.$updateDesiredColumn(this.selectionLead.column); this.$updateDesiredColumn(this.selectionLead.column);
}; };