move word selection from the editor to the selection class
This commit is contained in:
parent
7529f90080
commit
1f82e32175
3 changed files with 82 additions and 36 deletions
|
|
@ -34,7 +34,7 @@ ace.Editor.prototype.setDocument = function(doc) {
|
|||
|
||||
this.doc = doc;
|
||||
|
||||
doc.addChangeListener(ace.bind(this.onDocumentChange, this));
|
||||
doc.addEventListener("change", ace.bind(this.onDocumentChange, this));
|
||||
this.renderer.setDocument(doc);
|
||||
|
||||
this.selection = doc.getSelection();
|
||||
|
|
@ -192,41 +192,7 @@ ace.Editor.prototype.tokenRe = /^[\w\d]+/g;
|
|||
ace.Editor.prototype.nonTokenRe = /^[^\w\d]+/g;
|
||||
|
||||
ace.Editor.prototype.onMouseDoubleClick = function(e) {
|
||||
var cursor = this.selection.getCursor();
|
||||
|
||||
var line = this.doc.getLine(cursor.row);
|
||||
var column = cursor.column;
|
||||
|
||||
var inToken = false;
|
||||
if (column > 0) {
|
||||
inToken = !!line.charAt(column - 1).match(this.tokenRe);
|
||||
}
|
||||
|
||||
if (!inToken) {
|
||||
inToken = !!line.charAt(column).match(this.tokenRe);
|
||||
}
|
||||
|
||||
var re = inToken ? this.tokenRe : this.nonTokenRe;
|
||||
|
||||
var start = column;
|
||||
if (start > 0) {
|
||||
do {
|
||||
start--;
|
||||
}
|
||||
while (start >= 0 && line.charAt(start).match(re));
|
||||
start++;
|
||||
}
|
||||
|
||||
var end = column;
|
||||
while (end < line.length && line.charAt(end).match(re)) {
|
||||
end++;
|
||||
}
|
||||
|
||||
var selection = this.selection;
|
||||
selection.setSelectionAnchor(cursor.row, start);
|
||||
selection._moveSelection(function() {
|
||||
selection.moveCursorTo(cursor.row, end);
|
||||
});
|
||||
this.selection.selectWord();
|
||||
};
|
||||
|
||||
ace.Editor.prototype.onMouseWheel = function(e) {
|
||||
|
|
|
|||
|
|
@ -205,6 +205,43 @@ ace.Selection.prototype.selectWordLeft = function() {
|
|||
this._moveSelection(this.moveCursorWordLeft);
|
||||
};
|
||||
|
||||
ace.Selection.prototype.selectWord = function() {
|
||||
var cursor = this.cursor;
|
||||
|
||||
var line = this.doc.getLine(cursor.row);
|
||||
var column = cursor.column;
|
||||
|
||||
var inToken = false;
|
||||
if (column > 0) {
|
||||
inToken = !!line.charAt(column - 1).match(this.tokenRe);
|
||||
}
|
||||
|
||||
if (!inToken) {
|
||||
inToken = !!line.charAt(column).match(this.tokenRe);
|
||||
}
|
||||
|
||||
var re = inToken ? this.tokenRe : this.nonTokenRe;
|
||||
|
||||
var start = column;
|
||||
if (start > 0) {
|
||||
do {
|
||||
start--;
|
||||
}
|
||||
while (start >= 0 && line.charAt(start).match(re));
|
||||
start++;
|
||||
}
|
||||
|
||||
var end = column;
|
||||
while (end < line.length && line.charAt(end).match(re)) {
|
||||
end++;
|
||||
}
|
||||
|
||||
this.setSelectionAnchor(cursor.row, start);
|
||||
this._moveSelection(function() {
|
||||
this.moveCursorTo(cursor.row, end);
|
||||
});
|
||||
};
|
||||
|
||||
ace.Selection.prototype.selectLine = function() {
|
||||
this.setSelectionAnchor(this.cursor.row, 0);
|
||||
this._moveSelection(function() {
|
||||
|
|
|
|||
|
|
@ -173,5 +173,48 @@ var SelectionTest = TestCase("SelectionTest",
|
|||
|
||||
assertPosition(0, 0, range.start);
|
||||
assertPosition(0, 3, range.end);
|
||||
},
|
||||
|
||||
"test: select word with cursor in word should select the word" : function() {
|
||||
var doc = new ace.TextDocument("Juhu Kinners 123");
|
||||
var selection = doc.getSelection();
|
||||
|
||||
selection.moveCursorTo(0, 8);
|
||||
selection.selectWord();
|
||||
|
||||
var range = selection.getRange();
|
||||
assertPosition(0, 5, range.start);
|
||||
assertPosition(0, 12, range.end);
|
||||
},
|
||||
|
||||
"test: select word with cursor betwen white space and word should select the word" : function() {
|
||||
var doc = new ace.TextDocument("Juhu Kinners");
|
||||
var selection = doc.getSelection();
|
||||
|
||||
selection.moveCursorTo(0, 4);
|
||||
selection.selectWord();
|
||||
|
||||
var range = selection.getRange();
|
||||
assertPosition(0, 0, range.start);
|
||||
assertPosition(0, 4, range.end);
|
||||
|
||||
selection.moveCursorTo(0, 5);
|
||||
selection.selectWord();
|
||||
|
||||
var range = selection.getRange();
|
||||
assertPosition(0, 5, range.start);
|
||||
assertPosition(0, 12, range.end);
|
||||
},
|
||||
|
||||
"test: select word with cursor in white space should select white space" : function() {
|
||||
var doc = new ace.TextDocument("Juhu Kinners");
|
||||
var selection = doc.getSelection();
|
||||
|
||||
selection.moveCursorTo(0, 5);
|
||||
selection.selectWord();
|
||||
|
||||
var range = selection.getRange();
|
||||
assertPosition(0, 4, range.start);
|
||||
assertPosition(0, 6, range.end);
|
||||
}
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue