navigate/select word left/right
This commit is contained in:
parent
4ad2a8cf66
commit
895e5b77c1
3 changed files with 878 additions and 668 deletions
100
src/Editor.js
100
src/Editor.js
|
|
@ -61,7 +61,13 @@ if (!window.ace)
|
|||
return ace.stopEvent(e);
|
||||
|
||||
case keys.LEFT:
|
||||
if (e.metaKey && e.shiftKey) {
|
||||
if (e.altKey && e.shiftKey) {
|
||||
host.selectWordLeft();
|
||||
}
|
||||
else if (e.altKey) {
|
||||
host.navigateWordLeft();
|
||||
}
|
||||
else if (e.metaKey && e.shiftKey) {
|
||||
host.selectLineStart();
|
||||
}
|
||||
else if (e.metaKey) {
|
||||
|
|
@ -76,7 +82,13 @@ if (!window.ace)
|
|||
return ace.stopEvent(e);
|
||||
|
||||
case keys.RIGHT:
|
||||
if (e.metaKey && e.shiftKey) {
|
||||
if (e.altKey && e.shiftKey) {
|
||||
host.selectWordRight();
|
||||
}
|
||||
else if (e.altKey) {
|
||||
host.navigateWordRight();
|
||||
}
|
||||
else if (e.metaKey && e.shiftKey) {
|
||||
host.selectLineEnd();
|
||||
}
|
||||
else if (e.metaKey) {
|
||||
|
|
@ -244,23 +256,23 @@ if (!window.ace)
|
|||
return ace.preventDefault(e);
|
||||
};
|
||||
|
||||
ace.Editor.prototype.tokenRe = /^[\w\d]+/g;
|
||||
ace.Editor.prototype.nonTokenRe = /^[^\w\d]+/g;
|
||||
|
||||
ace.Editor.prototype.onMouseDoubleClick = function(e) {
|
||||
var line = this.doc.getLine(this.cursor.row);
|
||||
var column = this.cursor.column;
|
||||
|
||||
var tokenRe = /[a-zA-Z0-9_]+/g;
|
||||
var nonTokenRe = /[^a-zA-Z0-9_]+/g;
|
||||
|
||||
var inToken = false;
|
||||
if (column > 0) {
|
||||
inToken = !!line.charAt(column - 1).match(tokenRe);
|
||||
inToken = !!line.charAt(column - 1).match(this.tokenRe);
|
||||
}
|
||||
|
||||
if (!inToken) {
|
||||
inToken = !!line.charAt(column).match(tokenRe);
|
||||
inToken = !!line.charAt(column).match(this.tokenRe);
|
||||
}
|
||||
|
||||
var re = inToken ? tokenRe : nonTokenRe;
|
||||
var re = inToken ? this.tokenRe : this.nonTokenRe;
|
||||
|
||||
var start = column;
|
||||
if (start > 0) {
|
||||
|
|
@ -475,6 +487,18 @@ if (!window.ace)
|
|||
this.renderer.scrollCursorIntoView();
|
||||
},
|
||||
|
||||
ace.Editor.prototype.navigateWordRight = function() {
|
||||
this.clearSelection();
|
||||
this.moveCursorWordRight();
|
||||
this.renderer.scrollCursorIntoView();
|
||||
},
|
||||
|
||||
ace.Editor.prototype.navigateWordLeft = function() {
|
||||
this.clearSelection();
|
||||
this.moveCursorWordLeft();
|
||||
this.renderer.scrollCursorIntoView();
|
||||
},
|
||||
|
||||
ace.Editor.prototype.moveCursorUp = function() {
|
||||
this.moveCursorBy(-1, 0);
|
||||
};
|
||||
|
|
@ -525,6 +549,58 @@ if (!window.ace)
|
|||
this.moveCursorTo(0, 0);
|
||||
};
|
||||
|
||||
ace.Editor.prototype.moveCursorWordRight = function() {
|
||||
var row = this.cursor.row;
|
||||
var column = this.cursor.column;
|
||||
var line = this.doc.getLine(row);
|
||||
var rightOfCursor = line.substring(column);
|
||||
|
||||
var match;
|
||||
this.nonTokenRe.lastIndex = 0;
|
||||
this.tokenRe.lastIndex = 0;
|
||||
|
||||
if (column == line.length) {
|
||||
this.moveCursorRight();
|
||||
return;
|
||||
}
|
||||
else if (match = this.nonTokenRe.exec(rightOfCursor)) {
|
||||
column += this.nonTokenRe.lastIndex;
|
||||
this.nonTokenRe.lastIndex = 0;
|
||||
}
|
||||
else if (match = this.tokenRe.exec(rightOfCursor)) {
|
||||
column += this.tokenRe.lastIndex;
|
||||
this.tokenRe.lastIndex = 0;
|
||||
}
|
||||
|
||||
this.moveCursorTo(row, column);
|
||||
};
|
||||
|
||||
ace.Editor.prototype.moveCursorWordLeft = function() {
|
||||
var row = this.cursor.row;
|
||||
var column = this.cursor.column;
|
||||
var line = this.doc.getLine(row);
|
||||
var leftOfCursor = ace.stringReverse(line.substring(0, column));
|
||||
|
||||
var match;
|
||||
this.nonTokenRe.lastIndex = 0;
|
||||
this.tokenRe.lastIndex = 0;
|
||||
|
||||
if (column == 0) {
|
||||
this.moveCursorLeft();
|
||||
return;
|
||||
}
|
||||
else if (match = this.nonTokenRe.exec(leftOfCursor)) {
|
||||
column -= this.nonTokenRe.lastIndex;
|
||||
this.nonTokenRe.lastIndex = 0;
|
||||
}
|
||||
else if (match = this.tokenRe.exec(leftOfCursor)) {
|
||||
column -= this.tokenRe.lastIndex;
|
||||
this.tokenRe.lastIndex = 0;
|
||||
}
|
||||
|
||||
this.moveCursorTo(row, column);
|
||||
};
|
||||
|
||||
ace.Editor.prototype.moveCursorBy = function(rows, chars) {
|
||||
this.moveCursorTo(this.cursor.row + rows, this.cursor.column + chars);
|
||||
};
|
||||
|
|
@ -690,6 +766,14 @@ if (!window.ace)
|
|||
this._moveSelection(this.moveCursorFileStart);
|
||||
};
|
||||
|
||||
ace.Editor.prototype.selectWordRight = function() {
|
||||
this._moveSelection(this.moveCursorWordRight);
|
||||
};
|
||||
|
||||
ace.Editor.prototype.selectWordLeft = function() {
|
||||
this._moveSelection(this.moveCursorWordLeft);
|
||||
};
|
||||
|
||||
ace.Editor.prototype.selectCurrentLine = function() {
|
||||
this.setSelectionAnchor(this.cursor.row, 0);
|
||||
this._moveSelection(function() {
|
||||
|
|
|
|||
|
|
@ -97,6 +97,10 @@ if (!window.ace)
|
|||
return height;
|
||||
};
|
||||
|
||||
ace.stringReverse = function(string) {
|
||||
return string.split("").reverse().join("");
|
||||
};
|
||||
|
||||
ace.bind = function(fcn, context) {
|
||||
return function() {
|
||||
return fcn.apply(context, arguments);
|
||||
|
|
|
|||
|
|
@ -1,20 +1,24 @@
|
|||
var NavigationTest = TestCase("NavigationTest", {
|
||||
|
||||
var NavigationTest = TestCase("NavigationTest",
|
||||
{
|
||||
createTextDocument : function(rows, cols) {
|
||||
var line = new Array(cols + 1).join("a");
|
||||
var text = new Array(rows).join(line + "\n") + line;
|
||||
return new ace.TextDocument(text);
|
||||
},
|
||||
|
||||
assertPosition : function(row, column, cursor) {
|
||||
assertEquals(row, cursor.row);
|
||||
assertEquals(column, cursor.column);
|
||||
},
|
||||
|
||||
// End of file, start of files tests
|
||||
|
||||
"test: navigate to end of file should place the cursor on last row and column" : function() {
|
||||
var editor = new ace.Editor(this.createTextDocument(200, 10),
|
||||
new MockRenderer());
|
||||
|
||||
editor.navigateFileEnd();
|
||||
|
||||
var cursor = editor.getCursorPosition();
|
||||
assertEquals(199, cursor.row);
|
||||
assertEquals(10, cursor.column);
|
||||
this.assertPosition(199, 10, editor.getCursorPosition());
|
||||
},
|
||||
|
||||
"test: navigate to end of file should scroll the last line into view" : function() {
|
||||
|
|
@ -33,10 +37,7 @@ var NavigationTest = TestCase("NavigationTest", {
|
|||
new MockRenderer());
|
||||
|
||||
editor.navigateFileStart();
|
||||
|
||||
var cursor = editor.getCursorPosition();
|
||||
assertEquals(0, cursor.row);
|
||||
assertEquals(0, cursor.column);
|
||||
this.assertPosition(0, 0, editor.getCursorPosition());
|
||||
},
|
||||
|
||||
"test: navigate to start of file should scroll the first row into view" : function() {
|
||||
|
|
@ -58,11 +59,8 @@ var NavigationTest = TestCase("NavigationTest", {
|
|||
|
||||
var selection = editor.getSelectionRange();
|
||||
|
||||
assertEquals(100, selection.start.row);
|
||||
assertEquals(5, selection.start.column);
|
||||
|
||||
assertEquals(199, selection.end.row);
|
||||
assertEquals(10, selection.end.column);
|
||||
this.assertPosition(100, 5, selection.start);
|
||||
this.assertPosition(199, 10, selection.end);
|
||||
},
|
||||
|
||||
"test: move selection lead to start of file" : function() {
|
||||
|
|
@ -74,10 +72,134 @@ var NavigationTest = TestCase("NavigationTest", {
|
|||
|
||||
var selection = editor.getSelectionRange();
|
||||
|
||||
assertEquals(0, selection.start.row);
|
||||
assertEquals(0, selection.start.column);
|
||||
this.assertPosition(0, 0, selection.start);
|
||||
this.assertPosition(100, 5, selection.end);
|
||||
},
|
||||
|
||||
assertEquals(100, selection.end.row);
|
||||
assertEquals(5, selection.end.column);
|
||||
"test: navigate word right" : function() {
|
||||
var doc = new ace.TextDocument( ["ab",
|
||||
" Juhu Kinners (abc, 12)", " cde"].join("\n"));
|
||||
var editor = new ace.Editor(doc, new MockRenderer());
|
||||
|
||||
editor.navigateDown();
|
||||
this.assertPosition(1, 0, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordRight();
|
||||
this.assertPosition(1, 1, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordRight();
|
||||
this.assertPosition(1, 5, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordRight();
|
||||
this.assertPosition(1, 6, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordRight();
|
||||
this.assertPosition(1, 13, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordRight();
|
||||
this.assertPosition(1, 15, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordRight();
|
||||
this.assertPosition(1, 18, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordRight();
|
||||
this.assertPosition(1, 20, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordRight();
|
||||
this.assertPosition(1, 22, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordRight();
|
||||
this.assertPosition(1, 23, editor.getCursorPosition());
|
||||
|
||||
// wrap line
|
||||
editor.navigateWordRight();
|
||||
this.assertPosition(2, 0, editor.getCursorPosition());
|
||||
},
|
||||
|
||||
"test: select word right if cursor in word" : function() {
|
||||
var doc = new ace.TextDocument("Juhu Kinners");
|
||||
var editor = new ace.Editor(doc, new MockRenderer());
|
||||
|
||||
editor.moveCursorTo(0, 2);
|
||||
|
||||
editor.navigateWordRight();
|
||||
this.assertPosition(0, 4, editor.getCursorPosition());
|
||||
},
|
||||
|
||||
"test: navigate word left" : function() {
|
||||
var doc = new ace.TextDocument( ["ab",
|
||||
" Juhu Kinners (abc, 12)", " cde"].join("\n"));
|
||||
var editor = new ace.Editor(doc, new MockRenderer());
|
||||
|
||||
editor.navigateDown();
|
||||
editor.navigateLineEnd();
|
||||
this.assertPosition(1, 23, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordLeft();
|
||||
this.assertPosition(1, 22, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordLeft();
|
||||
this.assertPosition(1, 20, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordLeft();
|
||||
this.assertPosition(1, 18, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordLeft();
|
||||
this.assertPosition(1, 15, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordLeft();
|
||||
this.assertPosition(1, 13, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordLeft();
|
||||
this.assertPosition(1, 6, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordLeft();
|
||||
this.assertPosition(1, 5, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordLeft();
|
||||
this.assertPosition(1, 1, editor.getCursorPosition());
|
||||
|
||||
editor.navigateWordLeft();
|
||||
this.assertPosition(1, 0, editor.getCursorPosition());
|
||||
|
||||
// wrap line
|
||||
editor.navigateWordLeft();
|
||||
this.assertPosition(0, 2, editor.getCursorPosition());
|
||||
},
|
||||
|
||||
"test: select word left if cursor in word" : function() {
|
||||
var doc = new ace.TextDocument("Juhu Kinners");
|
||||
var editor = new ace.Editor(doc, new MockRenderer());
|
||||
|
||||
editor.moveCursorTo(0, 8);
|
||||
|
||||
editor.navigateWordLeft();
|
||||
this.assertPosition(0, 5, editor.getCursorPosition());
|
||||
},
|
||||
|
||||
"test: select word right and select" : function() {
|
||||
var doc = new ace.TextDocument("Juhu Kinners");
|
||||
var editor = new ace.Editor(doc, new MockRenderer());
|
||||
|
||||
editor.moveCursorTo(0, 0);
|
||||
editor.selectWordRight();
|
||||
|
||||
var selection = editor.getSelectionRange();
|
||||
|
||||
this.assertPosition(0, 0, selection.start);
|
||||
this.assertPosition(0, 4, selection.end);
|
||||
},
|
||||
|
||||
"test: select word left and select" : function() {
|
||||
var doc = new ace.TextDocument("Juhu Kinners");
|
||||
var editor = new ace.Editor(doc, new MockRenderer());
|
||||
|
||||
editor.moveCursorTo(0, 3);
|
||||
editor.selectWordLeft();
|
||||
|
||||
var selection = editor.getSelectionRange();
|
||||
|
||||
this.assertPosition(0, 0, selection.start);
|
||||
this.assertPosition(0, 3, selection.end);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue