navigate/select word left/right

This commit is contained in:
Fabian Jakobs 2010-04-12 15:05:56 +02:00
commit 895e5b77c1
3 changed files with 878 additions and 668 deletions

View file

@ -3,7 +3,7 @@ if (!window.ace)
(function() { (function() {
var keys = { var keys = {
UP : 38, UP : 38,
RIGHT : 39, RIGHT : 39,
DOWN : 40, DOWN : 40,
@ -16,9 +16,9 @@ if (!window.ace)
BACKSPACE : 8, BACKSPACE : 8,
TAB : 9, TAB : 9,
A : 65 A : 65
}; };
var KeyBinding = function(element, host) { var KeyBinding = function(element, host) {
ace.addListener(element, "keydown", function(e) { ace.addListener(element, "keydown", function(e) {
var key = e.keyCode; var key = e.keyCode;
@ -61,7 +61,13 @@ if (!window.ace)
return ace.stopEvent(e); return ace.stopEvent(e);
case keys.LEFT: 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(); host.selectLineStart();
} }
else if (e.metaKey) { else if (e.metaKey) {
@ -76,7 +82,13 @@ if (!window.ace)
return ace.stopEvent(e); return ace.stopEvent(e);
case keys.RIGHT: 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(); host.selectLineEnd();
} }
else if (e.metaKey) { else if (e.metaKey) {
@ -139,9 +151,9 @@ if (!window.ace)
return ace.stopEvent(e); return ace.stopEvent(e);
} }
}); });
}; };
ace.Editor = function(doc, renderer) { ace.Editor = function(doc, renderer) {
var container = renderer.getContainerElement(); var container = renderer.getContainerElement();
this.renderer = renderer; this.renderer = renderer;
@ -176,36 +188,36 @@ if (!window.ace)
this.selection = null; this.selection = null;
this.renderer.draw(); this.renderer.draw();
}; };
ace.Editor.prototype.resize = function() { ace.Editor.prototype.resize = function() {
this.renderer.draw(); this.renderer.draw();
}; };
ace.Editor.prototype.updateCursor = function() { ace.Editor.prototype.updateCursor = function() {
this.renderer.updateCursor(this.cursor); this.renderer.updateCursor(this.cursor);
}; };
ace.Editor.prototype.onFocus = function() { ace.Editor.prototype.onFocus = function() {
this.renderer.showCursor(); this.renderer.showCursor();
this.renderer.visualizeFocus(); this.renderer.visualizeFocus();
}; };
ace.Editor.prototype.onBlur = function() { ace.Editor.prototype.onBlur = function() {
this.renderer.hideCursor(); this.renderer.hideCursor();
this.renderer.visualizeBlur(); this.renderer.visualizeBlur();
}; };
ace.Editor.prototype.onDocumentChange = function(startRow, endRow) { ace.Editor.prototype.onDocumentChange = function(startRow, endRow) {
this.tokenizer.start(startRow); this.tokenizer.start(startRow);
this.renderer.updateLines(startRow, endRow); this.renderer.updateLines(startRow, endRow);
}; };
ace.Editor.prototype.onTokenizerUpdate = function(startRow, endRow) { ace.Editor.prototype.onTokenizerUpdate = function(startRow, endRow) {
this.renderer.updateLines(startRow, endRow); this.renderer.updateLines(startRow, endRow);
}; };
ace.Editor.prototype.onMouseDown = function(e) { ace.Editor.prototype.onMouseDown = function(e) {
this.textInput.focus(); this.textInput.focus();
var pos = this.renderer.screenToTextCoordinates(e.pageX, e.pageY); var pos = this.renderer.screenToTextCoordinates(e.pageX, e.pageY);
@ -242,25 +254,25 @@ if (!window.ace)
var timerId = setInterval(onSelectionInterval, 20); var timerId = setInterval(onSelectionInterval, 20);
return ace.preventDefault(e); return ace.preventDefault(e);
}; };
ace.Editor.prototype.onMouseDoubleClick = function(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 line = this.doc.getLine(this.cursor.row);
var column = this.cursor.column; var column = this.cursor.column;
var tokenRe = /[a-zA-Z0-9_]+/g;
var nonTokenRe = /[^a-zA-Z0-9_]+/g;
var inToken = false; var inToken = false;
if (column > 0) { if (column > 0) {
inToken = !!line.charAt(column - 1).match(tokenRe); inToken = !!line.charAt(column - 1).match(this.tokenRe);
} }
if (!inToken) { 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; var start = column;
if (start > 0) { if (start > 0) {
@ -280,32 +292,32 @@ if (!window.ace)
this._moveSelection(function() { this._moveSelection(function() {
this.moveCursorTo(this.cursor.row, end); this.moveCursorTo(this.cursor.row, end);
}); });
}; };
ace.Editor.prototype.onMouseWheel = function(e) { ace.Editor.prototype.onMouseWheel = function(e) {
var delta = e.wheel; var delta = e.wheel;
this.renderer.scrollToY(this.renderer.getScrollTop() - (delta * 15)); this.renderer.scrollToY(this.renderer.getScrollTop() - (delta * 15));
return ace.preventDefault(e); return ace.preventDefault(e);
}; };
ace.Editor.prototype.getCopyText = function() { ace.Editor.prototype.getCopyText = function() {
if (this.hasSelection()) { if (this.hasSelection()) {
return this.doc.getTextRange(this.getSelectionRange()); return this.doc.getTextRange(this.getSelectionRange());
} }
else { else {
return ""; return "";
} }
}; };
ace.Editor.prototype.onCut = function() { ace.Editor.prototype.onCut = function() {
if (this.hasSelection()) { if (this.hasSelection()) {
this.cursor = this.doc.remove(this.getSelectionRange()); this.cursor = this.doc.remove(this.getSelectionRange());
this.clearSelection(); this.clearSelection();
this.renderer.updateCursor(this.cursor); this.renderer.updateCursor(this.cursor);
} }
}; };
ace.Editor.prototype.onTextInput = function(text) { ace.Editor.prototype.onTextInput = function(text) {
if (this.hasSelection()) { if (this.hasSelection()) {
this.cursor = this.doc.replace(this.getSelectionRange(), text); this.cursor = this.doc.replace(this.getSelectionRange(), text);
this.clearSelection(); this.clearSelection();
@ -315,9 +327,9 @@ if (!window.ace)
} }
this.renderer.updateCursor(this.cursor); this.renderer.updateCursor(this.cursor);
this.renderer.scrollCursorIntoView(); this.renderer.scrollCursorIntoView();
}; };
ace.Editor.prototype.removeRight = function() { ace.Editor.prototype.removeRight = function() {
if (this.hasSelection()) { if (this.hasSelection()) {
this.cursor = this.doc.remove(this.getSelectionRange()); this.cursor = this.doc.remove(this.getSelectionRange());
this.renderer.updateCursor(this.cursor); this.renderer.updateCursor(this.cursor);
@ -339,7 +351,7 @@ if (!window.ace)
} }
this.renderer.scrollCursorIntoView(); this.renderer.scrollCursorIntoView();
}; };
ace.Editor.prototype.removeLeft = function() { ace.Editor.prototype.removeLeft = function() {
if (this.hasSelection()) { if (this.hasSelection()) {
@ -373,59 +385,59 @@ if (!window.ace)
this.onTextInput(" "); this.onTextInput(" ");
}; };
ace.Editor.prototype.onCompositionUpdate = function(text) { ace.Editor.prototype.onCompositionUpdate = function(text) {
this.renderer.setCompositionText(text); this.renderer.setCompositionText(text);
}; };
ace.Editor.prototype.onCompositionEnd = function() { ace.Editor.prototype.onCompositionEnd = function() {
this.renderer.hideComposition(); this.renderer.hideComposition();
this.removeLeft(); this.removeLeft();
}; };
ace.Editor.prototype.getFirstVisibleRow = function() { ace.Editor.prototype.getFirstVisibleRow = function() {
return this.renderer.getFirstVisibleRow(); return this.renderer.getFirstVisibleRow();
}; };
ace.Editor.prototype.getLastVisibleRow = function() { ace.Editor.prototype.getLastVisibleRow = function() {
return this.renderer.getLastVisibleRow(); return this.renderer.getLastVisibleRow();
}; };
ace.Editor.prototype.getPageDownRow = function() { ace.Editor.prototype.getPageDownRow = function() {
return this.renderer.getLastVisibleRow() - 1; return this.renderer.getLastVisibleRow() - 1;
}; };
ace.Editor.prototype.getPageUpRow = function() { ace.Editor.prototype.getPageUpRow = function() {
var firstRow = this.renderer.getFirstVisibleRow(); var firstRow = this.renderer.getFirstVisibleRow();
var lastRow = this.renderer.getLastVisibleRow(); var lastRow = this.renderer.getLastVisibleRow();
return firstRow - (lastRow - firstRow) + 1; return firstRow - (lastRow - firstRow) + 1;
}; };
ace.Editor.prototype.scrollPageDown = function() { ace.Editor.prototype.scrollPageDown = function() {
this.scrollToRow(this.getPageDownRow()); this.scrollToRow(this.getPageDownRow());
}; };
ace.Editor.prototype.scrollPageUp = function() { ace.Editor.prototype.scrollPageUp = function() {
this.renderer.scrollToRow(this.getPageUpRow()); this.renderer.scrollToRow(this.getPageUpRow());
}; };
ace.Editor.prototype.scrollToRow = function(row) { ace.Editor.prototype.scrollToRow = function(row) {
this.renderer.scrollToRow(row); this.renderer.scrollToRow(row);
}; };
ace.Editor.prototype.navigateUp = function() { ace.Editor.prototype.navigateUp = function() {
this.clearSelection(); this.clearSelection();
this.moveCursorUp(); this.moveCursorUp();
this.renderer.scrollCursorIntoView(); this.renderer.scrollCursorIntoView();
}; };
ace.Editor.prototype.navigateDown = function() { ace.Editor.prototype.navigateDown = function() {
this.clearSelection(); this.clearSelection();
this.moveCursorDown(); this.moveCursorDown();
this.renderer.scrollCursorIntoView(); this.renderer.scrollCursorIntoView();
}; };
ace.Editor.prototype.navigateLeft = function() { ace.Editor.prototype.navigateLeft = function() {
if (this.hasSelection()) { if (this.hasSelection()) {
var selectionStart = this.getSelectionRange().start; var selectionStart = this.getSelectionRange().start;
this.moveCursorTo(selectionStart.row, selectionStart.column); this.moveCursorTo(selectionStart.row, selectionStart.column);
@ -436,9 +448,9 @@ if (!window.ace)
this.clearSelection(); this.clearSelection();
this.renderer.scrollCursorIntoView(); this.renderer.scrollCursorIntoView();
}; };
ace.Editor.prototype.navigateRight = function() { ace.Editor.prototype.navigateRight = function() {
if (this.hasSelection()) { if (this.hasSelection()) {
var selectionEnd = this.getSelectionRange().end; var selectionEnd = this.getSelectionRange().end;
this.moveCursorTo(selectionEnd.row, selectionEnd.column); this.moveCursorTo(selectionEnd.row, selectionEnd.column);
@ -449,41 +461,53 @@ if (!window.ace)
this.clearSelection(); this.clearSelection();
this.renderer.scrollCursorIntoView(); this.renderer.scrollCursorIntoView();
}, },
ace.Editor.prototype.navigateLineStart = function() { ace.Editor.prototype.navigateLineStart = function() {
this.clearSelection(); this.clearSelection();
this.moveCursorLineStart(); this.moveCursorLineStart();
this.renderer.scrollCursorIntoView(); this.renderer.scrollCursorIntoView();
}; };
ace.Editor.prototype.navigateLineEnd = function() { ace.Editor.prototype.navigateLineEnd = function() {
this.clearSelection(); this.clearSelection();
this.moveCursorLineEnd(); this.moveCursorLineEnd();
this.renderer.scrollCursorIntoView(); this.renderer.scrollCursorIntoView();
}; };
ace.Editor.prototype.navigateFileEnd = function() { ace.Editor.prototype.navigateFileEnd = function() {
this.clearSelection(); this.clearSelection();
this.moveCursorFileEnd(); this.moveCursorFileEnd();
this.renderer.scrollCursorIntoView(); this.renderer.scrollCursorIntoView();
}, },
ace.Editor.prototype.navigateFileStart = function() { ace.Editor.prototype.navigateFileStart = function() {
this.clearSelection(); this.clearSelection();
this.moveCursorFileStart(); this.moveCursorFileStart();
this.renderer.scrollCursorIntoView(); this.renderer.scrollCursorIntoView();
}, },
ace.Editor.prototype.moveCursorUp = function() { 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); this.moveCursorBy(-1, 0);
}; };
ace.Editor.prototype.moveCursorDown = function() { ace.Editor.prototype.moveCursorDown = function() {
this.moveCursorBy(1, 0); this.moveCursorBy(1, 0);
}; };
ace.Editor.prototype.moveCursorLeft = function() { ace.Editor.prototype.moveCursorLeft = function() {
if (this.cursor.column == 0) { if (this.cursor.column == 0) {
if (this.cursor.row > 0) { if (this.cursor.row > 0) {
this.moveCursorTo(this.cursor.row - 1, this.doc this.moveCursorTo(this.cursor.row - 1, this.doc
@ -493,9 +517,9 @@ if (!window.ace)
else { else {
this.moveCursorBy(0, -1); this.moveCursorBy(0, -1);
} }
}; };
ace.Editor.prototype.moveCursorRight = function() { ace.Editor.prototype.moveCursorRight = function() {
if (this.cursor.column == this.doc.getLine(this.cursor.row).length) { if (this.cursor.column == this.doc.getLine(this.cursor.row).length) {
if (this.cursor.row < this.doc.getLength() - 1) { if (this.cursor.row < this.doc.getLength() - 1) {
this.moveCursorTo(this.cursor.row + 1, 0); this.moveCursorTo(this.cursor.row + 1, 0);
@ -504,32 +528,84 @@ if (!window.ace)
else { else {
this.moveCursorBy(0, 1); this.moveCursorBy(0, 1);
} }
}; };
ace.Editor.prototype.moveCursorLineStart = function() { ace.Editor.prototype.moveCursorLineStart = function() {
this.moveCursorTo(this.cursor.row, 0); this.moveCursorTo(this.cursor.row, 0);
}; };
ace.Editor.prototype.moveCursorLineEnd = function() { ace.Editor.prototype.moveCursorLineEnd = function() {
this.moveCursorTo(this.cursor.row, this.moveCursorTo(this.cursor.row,
this.doc.getLine(this.cursor.row).length); this.doc.getLine(this.cursor.row).length);
}; };
ace.Editor.prototype.moveCursorFileEnd = function() { ace.Editor.prototype.moveCursorFileEnd = function() {
var row = this.doc.getLength() - 1; var row = this.doc.getLength() - 1;
var column = this.doc.getLine(row).length; var column = this.doc.getLine(row).length;
this.moveCursorTo(row, column); this.moveCursorTo(row, column);
}; };
ace.Editor.prototype.moveCursorFileStart = function() { ace.Editor.prototype.moveCursorFileStart = function() {
this.moveCursorTo(0, 0); this.moveCursorTo(0, 0);
}; };
ace.Editor.prototype.moveCursorBy = function(rows, chars) { 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); this.moveCursorTo(this.cursor.row + rows, this.cursor.column + chars);
}; };
ace.Editor.prototype.moveCursorTo = function(row, column) { ace.Editor.prototype.moveCursorTo = function(row, column) {
if (row >= this.doc.getLength()) { if (row >= this.doc.getLength()) {
this.cursor.row = this.doc.getLength() - 1; this.cursor.row = this.doc.getLength() - 1;
this.cursor.column = this.doc.getLine(this.cursor.row).length; this.cursor.column = this.doc.getLine(this.cursor.row).length;
@ -545,20 +621,20 @@ if (!window.ace)
.max(0, column)); .max(0, column));
} }
this.updateCursor(); this.updateCursor();
}; };
ace.Editor.prototype.getCursorPosition = function() { ace.Editor.prototype.getCursorPosition = function() {
return { return {
row : this.cursor.row, row : this.cursor.row,
column : this.cursor.column column : this.cursor.column
}; };
}; };
ace.Editor.prototype.hasSelection = function() { ace.Editor.prototype.hasSelection = function() {
return !!this.selectionLead; return !!this.selectionLead;
}; };
ace.Editor.prototype.setSelectionAnchor = function(row, column) { ace.Editor.prototype.setSelectionAnchor = function(row, column) {
this.clearSelection(); this.clearSelection();
this.selectionAnchor = { this.selectionAnchor = {
@ -568,9 +644,9 @@ if (!window.ace)
}; };
this.selectionLead = null; this.selectionLead = null;
}; };
ace.Editor.prototype.getSelectionRange = function() { ace.Editor.prototype.getSelectionRange = function() {
var anchor = this.selectionAnchor; var anchor = this.selectionAnchor;
var lead = this.selectionLead; var lead = this.selectionLead;
@ -592,9 +668,9 @@ if (!window.ace)
}; };
} }
} }
}; };
ace.Editor.prototype.clearSelection = function() { ace.Editor.prototype.clearSelection = function() {
this.selectionLead = null; this.selectionLead = null;
this.selectionAnchor = null; this.selectionAnchor = null;
@ -602,18 +678,18 @@ if (!window.ace)
this.renderer.removeMarker(this.selection); this.renderer.removeMarker(this.selection);
this.selection = null; this.selection = null;
} }
}; };
ace.Editor.prototype.selectAll = function() { ace.Editor.prototype.selectAll = function() {
var lastRow = this.doc.getLength() - 1; var lastRow = this.doc.getLength() - 1;
this.setSelectionAnchor(lastRow, this.doc.getLine(lastRow).length); this.setSelectionAnchor(lastRow, this.doc.getLine(lastRow).length);
this._moveSelection(function() { this._moveSelection(function() {
this.moveCursorTo(0, 0); this.moveCursorTo(0, 0);
}); });
}; };
ace.Editor.prototype._moveSelection = function(mover) { ace.Editor.prototype._moveSelection = function(mover) {
if (!this.selectionAnchor) { if (!this.selectionAnchor) {
this.selectionAnchor = { this.selectionAnchor = {
row : this.cursor.row, row : this.cursor.row,
@ -634,33 +710,33 @@ if (!window.ace)
this.selection = this.renderer.addMarker(this.getSelectionRange(), this.selection = this.renderer.addMarker(this.getSelectionRange(),
"selection"); "selection");
this.renderer.scrollCursorIntoView(); this.renderer.scrollCursorIntoView();
}; };
ace.Editor.prototype.selectUp = function() { ace.Editor.prototype.selectUp = function() {
this._moveSelection(this.moveCursorUp); this._moveSelection(this.moveCursorUp);
}; };
ace.Editor.prototype.selectDown = function() { ace.Editor.prototype.selectDown = function() {
this._moveSelection(this.moveCursorDown); this._moveSelection(this.moveCursorDown);
}; };
ace.Editor.prototype.selectRight = function() { ace.Editor.prototype.selectRight = function() {
this._moveSelection(this.moveCursorRight); this._moveSelection(this.moveCursorRight);
}; };
ace.Editor.prototype.selectLeft = function() { ace.Editor.prototype.selectLeft = function() {
this._moveSelection(this.moveCursorLeft); this._moveSelection(this.moveCursorLeft);
}; };
ace.Editor.prototype.selectLineStart = function() { ace.Editor.prototype.selectLineStart = function() {
this._moveSelection(this.moveCursorLineStart); this._moveSelection(this.moveCursorLineStart);
}; };
ace.Editor.prototype.selectLineEnd = function() { ace.Editor.prototype.selectLineEnd = function() {
this._moveSelection(this.moveCursorLineEnd); this._moveSelection(this.moveCursorLineEnd);
}; };
ace.Editor.prototype.selectPageDown = function() { ace.Editor.prototype.selectPageDown = function() {
var visibleRows = this.getLastVisibleRow() - this.getFirstVisibleRow(); var visibleRows = this.getLastVisibleRow() - this.getFirstVisibleRow();
var row = this.getPageDownRow() + Math.round(visibleRows / 2); var row = this.getPageDownRow() + Math.round(visibleRows / 2);
@ -669,9 +745,9 @@ if (!window.ace)
this._moveSelection(function() { this._moveSelection(function() {
this.moveCursorTo(row, this.cursor.column); this.moveCursorTo(row, this.cursor.column);
}); });
}; };
ace.Editor.prototype.selectPageUp = function() { ace.Editor.prototype.selectPageUp = function() {
var visibleRows = this.getLastVisibleRow() - this.getFirstVisibleRow(); var visibleRows = this.getLastVisibleRow() - this.getFirstVisibleRow();
var row = this.getPageUpRow() + Math.round(visibleRows / 2); var row = this.getPageUpRow() + Math.round(visibleRows / 2);
@ -680,21 +756,29 @@ if (!window.ace)
this._moveSelection(function() { this._moveSelection(function() {
this.moveCursorTo(row, this.cursor.column); this.moveCursorTo(row, this.cursor.column);
}); });
}; };
ace.Editor.prototype.selectFileEnd = function() { ace.Editor.prototype.selectFileEnd = function() {
this._moveSelection(this.moveCursorFileEnd); this._moveSelection(this.moveCursorFileEnd);
}; };
ace.Editor.prototype.selectFileStart = function() { ace.Editor.prototype.selectFileStart = function() {
this._moveSelection(this.moveCursorFileStart); this._moveSelection(this.moveCursorFileStart);
}; };
ace.Editor.prototype.selectCurrentLine = function() { 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.setSelectionAnchor(this.cursor.row, 0);
this._moveSelection(function() { this._moveSelection(function() {
this.moveCursorTo(this.cursor.row + 1, 0); this.moveCursorTo(this.cursor.row + 1, 0);
}); });
}; };
})(); })();

View file

@ -97,6 +97,10 @@ if (!window.ace)
return height; return height;
}; };
ace.stringReverse = function(string) {
return string.split("").reverse().join("");
};
ace.bind = function(fcn, context) { ace.bind = function(fcn, context) {
return function() { return function() {
return fcn.apply(context, arguments); return fcn.apply(context, arguments);

View file

@ -1,20 +1,24 @@
var NavigationTest = TestCase("NavigationTest", { var NavigationTest = TestCase("NavigationTest",
{
createTextDocument : function(rows, cols) { createTextDocument : function(rows, cols) {
var line = new Array(cols+1).join("a"); var line = new Array(cols + 1).join("a");
var text = new Array(rows).join(line + "\n") + line; var text = new Array(rows).join(line + "\n") + line;
return new ace.TextDocument(text); 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() { "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), var editor = new ace.Editor(this.createTextDocument(200, 10),
new MockRenderer()); new MockRenderer());
editor.navigateFileEnd(); editor.navigateFileEnd();
this.assertPosition(199, 10, editor.getCursorPosition());
var cursor = editor.getCursorPosition();
assertEquals(199, cursor.row);
assertEquals(10, cursor.column);
}, },
"test: navigate to end of file should scroll the last line into view" : function() { "test: navigate to end of file should scroll the last line into view" : function() {
@ -33,10 +37,7 @@ var NavigationTest = TestCase("NavigationTest", {
new MockRenderer()); new MockRenderer());
editor.navigateFileStart(); editor.navigateFileStart();
this.assertPosition(0, 0, editor.getCursorPosition());
var cursor = editor.getCursorPosition();
assertEquals(0, cursor.row);
assertEquals(0, cursor.column);
}, },
"test: navigate to start of file should scroll the first row into view" : function() { "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(); var selection = editor.getSelectionRange();
assertEquals(100, selection.start.row); this.assertPosition(100, 5, selection.start);
assertEquals(5, selection.start.column); this.assertPosition(199, 10, selection.end);
assertEquals(199, selection.end.row);
assertEquals(10, selection.end.column);
}, },
"test: move selection lead to start of file" : function() { "test: move selection lead to start of file" : function() {
@ -74,10 +72,134 @@ var NavigationTest = TestCase("NavigationTest", {
var selection = editor.getSelectionRange(); var selection = editor.getSelectionRange();
assertEquals(0, selection.start.row); this.assertPosition(0, 0, selection.start);
assertEquals(0, selection.start.column); this.assertPosition(100, 5, selection.end);
},
assertEquals(100, selection.end.row); "test: navigate word right" : function() {
assertEquals(5, selection.end.column); 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);
} }
}); });