From 1f82e32175a44b9595689d669636462ee31795b7 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Thu, 15 Apr 2010 15:58:13 +0200 Subject: [PATCH] move word selection from the editor to the selection class --- src/Editor.js | 38 ++------------------------------------ src/Selection.js | 37 +++++++++++++++++++++++++++++++++++++ test/SelectionTest.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 36 deletions(-) diff --git a/src/Editor.js b/src/Editor.js index 6779f8a3..ed8a6e87 100644 --- a/src/Editor.js +++ b/src/Editor.js @@ -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) { diff --git a/src/Selection.js b/src/Selection.js index 3430330b..dc9102c7 100644 --- a/src/Selection.js +++ b/src/Selection.js @@ -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() { diff --git a/test/SelectionTest.js b/test/SelectionTest.js index a42278a6..95c81b14 100644 --- a/test/SelectionTest.js +++ b/test/SelectionTest.js @@ -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); } }); \ No newline at end of file