Double mouse click selects word

The mousedown event on editor is causing this to fail right now.
This commit is contained in:
Corey Johnson 2012-02-02 14:13:28 -08:00
parent d8975e7a94
commit c6c762ba23
5 changed files with 67 additions and 1 deletions

View File

@ -253,6 +253,14 @@ describe "Editor", ->
editor.lines.trigger mousedownEvent({pageX, pageY})
expect(editor.getCursorPosition()).toEqual(row: 3, column: 10)
describe "when doubleclick occurs in the editor", ->
it "selects the word under the cursor", ->
editor.attachToDom()
expect(editor.getCursorPosition()).toEqual(row: 0, column: 0)
[pageX, pageY] = window.pixelPositionForPoint(editor, [0, 8])
editor.lines.trigger mouseupEvent({pageX, pageY, originalEvent: {detail: 2}})
expect(editor.getSelectedText()).toBe "quicksort"
describe "selection", ->
selection = null

View File

@ -158,3 +158,33 @@ describe "Selection", ->
selection.setRange new Range([0,4], [0,4])
selection.copy()
expect(atom.native.readFromPasteboard()).toBe 'first'
describe ".selectWord()", ->
describe "when the cursor is inside a word", ->
it "selects the entire word", ->
editor.setCursorPosition [0,8]
selection.selectWord()
expect(selection.getText()).toBe 'quicksort'
describe "when the cursor is on beginning of a word", ->
it "selects the entire word", ->
editor.setCursorPosition [0,4]
selection.selectWord()
expect(selection.getText()).toBe 'quicksort'
describe "when the cursor is at the end of a word", ->
it "selects the entire word", ->
editor.setCursorPosition [0,13]
selection.selectWord()
expect(selection.getText()).toBe 'quicksort'
describe "when the cursor is not on a word", ->
it "selects nothing", ->
editor.setCursorPosition [5,2]
selection.selectWord()
expect(selection.getText()).toBe ''

View File

@ -35,6 +35,9 @@ window.clickEvent = (properties={}) ->
window.mousedownEvent = (properties={}) ->
$.Event "mousedown", properties
window.mouseupEvent = (properties={}) ->
$.Event "mouseup", properties
window.mousemoveEvent = (properties={}) ->
$.Event "mousemove", properties

View File

@ -76,11 +76,17 @@ class Editor extends Template
false
@on 'mousedown', (e) =>
@setCursorPosition(@pointFromMouseEvent(e))
@setCursorPosition @pointFromMouseEvent(e)
moveHandler = (e) => @selectToPosition(@pointFromMouseEvent(e))
@on 'mousemove', moveHandler
$(document).one 'mouseup', => @off 'mousemove', moveHandler
@on 'mouseup', (e) =>
clickCount = e.originalEvent.detail
if clickCount == 2
@setCursorPosition @pointFromMouseEvent(e)
@selection.selectWord()
@hiddenInput.on "textInput", (e) =>
@insertText(e.originalEvent.data)
@ -182,6 +188,7 @@ class Editor extends Template
getSelection: -> @selection
getCurrentLine: -> @buffer.getLine(@getCursorRow())
getSelectedText: -> @selection.getText()
moveCursorUp: -> @cursor.moveUp()
moveCursorDown: -> @cursor.moveDown()
moveCursorRight: -> @cursor.moveRight()

View File

@ -75,6 +75,9 @@ class Selection extends Template
@modifySelection =>
@cursor.setPosition(range.end)
getText: ->
@editor.buffer.getTextInRange @getRange()
insertText: (text) ->
@editor.buffer.change(@getRange(), text)
@ -99,6 +102,21 @@ class Selection extends Template
cursorPosition = @cursor.getPosition()
@anchor = { getPosition: -> cursorPosition }
selectWord: ->
row = @cursor.getRow()
column = @cursor.getColumn()
line = @editor.buffer.getLine(row)
leftSide = line[0...column].split('').reverse().join('') # reverse left side
rightSide = line[column..]
regex = /^\w*/
startOffset = -regex.exec(leftSide)?[0]?.length or 0
endOffset = regex.exec(rightSide)?[0]?.length or 0
range = new Range([row, column + startOffset], [row, column + endOffset])
@setRange range
selectRight: ->
@modifySelection =>
@cursor.moveRight()