Merge pull request #531 from ajaxorg/vim_methods

Added `getAWordRange` and `selectAWord` methods
This commit is contained in:
Zef Hemel 2011-11-25 02:21:34 -08:00
commit 7e84cef35b
3 changed files with 73 additions and 43 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);
}; };

View file

@ -149,7 +149,7 @@ module.exports = {
" Juhu Kinners (abc, 12)", " Juhu Kinners (abc, 12)",
" cde" " cde"
].join("\n")); ].join("\n"));
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorDown(); selection.moveCursorDown();
@ -187,7 +187,7 @@ module.exports = {
selection.moveCursorWordLeft(); selection.moveCursorWordLeft();
assert.position(selection.getCursor(), 0, 2); assert.position(selection.getCursor(), 0, 2);
}, },
"test: moveCursor word left" : function() { "test: moveCursor word left" : function() {
var session = new EditSession(" Fuß Füße"); var session = new EditSession(" Fuß Füße");
@ -195,7 +195,7 @@ module.exports = {
selection.moveCursorTo(0, 9) selection.moveCursorTo(0, 9)
selection.moveCursorWordLeft(); selection.moveCursorWordLeft();
assert.position(selection.getCursor(), 0, 5); assert.position(selection.getCursor(), 0, 5);
selection.moveCursorWordLeft(); selection.moveCursorWordLeft();
assert.position(selection.getCursor(), 0, 4); assert.position(selection.getCursor(), 0, 4);
}, },
@ -248,6 +248,18 @@ module.exports = {
assert.position(range.end, 0, 12); assert.position(range.end, 0, 12);
}, },
"test: select word with cursor in word including right whitespace should select the word" : function() {
var session = new EditSession("Juhu Kinners 123");
var selection = session.getSelection();
selection.moveCursorTo(0, 8);
selection.selectAWord();
var range = selection.getRange();
assert.position(range.start, 0, 5);
assert.position(range.end, 0, 18);
},
"test: select word with cursor betwen white space and word should select the word" : function() { "test: select word with cursor betwen white space and word should select the word" : function() {
var session = new EditSession("Juhu Kinners"); var session = new EditSession("Juhu Kinners");
var selection = session.getSelection(); var selection = session.getSelection();
@ -308,67 +320,67 @@ module.exports = {
selection.moveCursorTo(0, 5); selection.moveCursorTo(0, 5);
assert.notOk(called); assert.notOk(called);
}, },
"test: moveWordLeft should move past || and [": function() { "test: moveWordLeft should move past || and [": function() {
var session = new EditSession("||foo["); var session = new EditSession("||foo[");
var selection = session.getSelection(); var selection = session.getSelection();
// Move behind || // Move behind ||
selection.moveCursorWordRight(); selection.moveCursorWordRight();
assert.position(selection.getCursor(), 0, 2); assert.position(selection.getCursor(), 0, 2);
// Move beind foo // Move beind foo
selection.moveCursorWordRight(); selection.moveCursorWordRight();
assert.position(selection.getCursor(), 0, 5); assert.position(selection.getCursor(), 0, 5);
// Move behind [ // Move behind [
selection.moveCursorWordRight(); selection.moveCursorWordRight();
assert.position(selection.getCursor(), 0, 6); assert.position(selection.getCursor(), 0, 6);
}, },
"test: moveWordRight should move past || and [": function() { "test: moveWordRight should move past || and [": function() {
var session = new EditSession("||foo["); var session = new EditSession("||foo[");
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorTo(0, 6); selection.moveCursorTo(0, 6);
// Move behind [ // Move behind [
selection.moveCursorWordLeft(); selection.moveCursorWordLeft();
assert.position(selection.getCursor(), 0, 5); assert.position(selection.getCursor(), 0, 5);
// Move beind foo // Move beind foo
selection.moveCursorWordLeft(); selection.moveCursorWordLeft();
assert.position(selection.getCursor(), 0, 2); assert.position(selection.getCursor(), 0, 2);
// Move behind || // Move behind ||
selection.moveCursorWordLeft(); selection.moveCursorWordLeft();
assert.position(selection.getCursor(), 0, 0); assert.position(selection.getCursor(), 0, 0);
}, },
"test: move cursor to line start should move cursor to end of the indentation first": function() { "test: move cursor to line start should move cursor to end of the indentation first": function() {
var session = new EditSession("12\n Juhu\n12"); var session = new EditSession("12\n Juhu\n12");
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorTo(1, 6); selection.moveCursorTo(1, 6);
selection.moveCursorLineStart(); selection.moveCursorLineStart();
assert.position(selection.getCursor(), 1, 4); assert.position(selection.getCursor(), 1, 4);
}, },
"test: move cursor to line start when the cursor is at the end of the indentation should move cursor to column 0": function() { "test: move cursor to line start when the cursor is at the end of the indentation should move cursor to column 0": function() {
var session = new EditSession("12\n Juhu\n12"); var session = new EditSession("12\n Juhu\n12");
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorTo(1, 4); selection.moveCursorTo(1, 4);
selection.moveCursorLineStart(); selection.moveCursorLineStart();
assert.position(selection.getCursor(), 1, 0); assert.position(selection.getCursor(), 1, 0);
}, },
"test: move cursor to line start when the cursor is at column 0 should move cursor to the end of the indentation": function() { "test: move cursor to line start when the cursor is at column 0 should move cursor to the end of the indentation": function() {
var session = new EditSession("12\n Juhu\n12"); var session = new EditSession("12\n Juhu\n12");
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorTo(1, 0); selection.moveCursorTo(1, 0);
selection.moveCursorLineStart(); selection.moveCursorLineStart();
@ -379,64 +391,64 @@ module.exports = {
"test: move cursor to line start when the cursor is before the initial indentation should move cursor to the end of the indentation": function() { "test: move cursor to line start when the cursor is before the initial indentation should move cursor to the end of the indentation": function() {
var session = new EditSession("12\n Juhu\n12"); var session = new EditSession("12\n Juhu\n12");
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorTo(1, 2); selection.moveCursorTo(1, 2);
selection.moveCursorLineStart(); selection.moveCursorLineStart();
assert.position(selection.getCursor(), 1, 4); assert.position(selection.getCursor(), 1, 4);
}, },
"test go line up when in the middle of the first line should go to document start": function() { "test go line up when in the middle of the first line should go to document start": function() {
var session = new EditSession("juhu kinners"); var session = new EditSession("juhu kinners");
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorTo(0, 4); selection.moveCursorTo(0, 4);
selection.moveCursorUp(); selection.moveCursorUp();
assert.position(selection.getCursor(), 0, 0); assert.position(selection.getCursor(), 0, 0);
}, },
"test: (wrap) go line up when in the middle of the first line should go to document start": function() { "test: (wrap) go line up when in the middle of the first line should go to document start": function() {
var session = new EditSession("juhu kinners"); var session = new EditSession("juhu kinners");
session.setWrapLimitRange(5, 5); session.setWrapLimitRange(5, 5);
session.adjustWrapLimit(80); session.adjustWrapLimit(80);
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorTo(0, 4); selection.moveCursorTo(0, 4);
selection.moveCursorUp(); selection.moveCursorUp();
assert.position(selection.getCursor(), 0, 0); assert.position(selection.getCursor(), 0, 0);
}, },
"test go line down when in the middle of the last line should go to document end": function() { "test go line down when in the middle of the last line should go to document end": function() {
var session = new EditSession("juhu kinners"); var session = new EditSession("juhu kinners");
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorTo(0, 4); selection.moveCursorTo(0, 4);
selection.moveCursorDown(); selection.moveCursorDown();
assert.position(selection.getCursor(), 0, 12); assert.position(selection.getCursor(), 0, 12);
}, },
"test (wrap) go line down when in the middle of the last line should go to document end": function() { "test (wrap) go line down when in the middle of the last line should go to document end": function() {
var session = new EditSession("juhu kinners"); var session = new EditSession("juhu kinners");
session.setWrapLimitRange(8, 8); session.setWrapLimitRange(8, 8);
session.adjustWrapLimit(80); session.adjustWrapLimit(80);
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorTo(0, 10); selection.moveCursorTo(0, 10);
selection.moveCursorDown(); selection.moveCursorDown();
assert.position(selection.getCursor(), 0, 12); assert.position(selection.getCursor(), 0, 12);
}, },
"test go line up twice and then once down when in the second should go back to the previous column": function() { "test go line up twice and then once down when in the second should go back to the previous column": function() {
var session = new EditSession("juhu\nkinners"); var session = new EditSession("juhu\nkinners");
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorTo(1, 4); selection.moveCursorTo(1, 4);
selection.moveCursorUp(); selection.moveCursorUp();
selection.moveCursorUp(); selection.moveCursorUp();
@ -444,25 +456,25 @@ module.exports = {
assert.position(selection.getCursor(), 1, 4); assert.position(selection.getCursor(), 1, 4);
}, },
"test (keyboard navigation) when curLine is not EOL and targetLine is all whitespace new column should be current column": function() { "test (keyboard navigation) when curLine is not EOL and targetLine is all whitespace new column should be current column": function() {
var session = new EditSession("function (a) {\n\ var session = new EditSession("function (a) {\n\
\n\ \n\
}"); }");
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorTo(2, 0); selection.moveCursorTo(2, 0);
selection.moveCursorUp(); selection.moveCursorUp();
assert.position(selection.getCursor(), 1, 0); assert.position(selection.getCursor(), 1, 0);
}, },
"test (keyboard navigation) when curLine is EOL and targetLine is shorter dan current column, new column should be targetLine's EOL": function() { "test (keyboard navigation) when curLine is EOL and targetLine is shorter dan current column, new column should be targetLine's EOL": function() {
var session = new EditSession("function (a) {\n\ var session = new EditSession("function (a) {\n\
\n\ \n\
}"); }");
var selection = session.getSelection(); var selection = session.getSelection();
selection.moveCursorTo(0, 14); selection.moveCursorTo(0, 14);
selection.moveCursorDown(); selection.moveCursorDown();
@ -474,4 +486,4 @@ module.exports = {
if (typeof module !== "undefined" && module === require.main) { if (typeof module !== "undefined" && module === require.main) {
require("asyncjs").test.testcase(module.exports).exec() require("asyncjs").test.testcase(module.exports).exec()
} }