diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 5d7546cd6..c13c525cb 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -911,14 +911,20 @@ describe "Editor", -> expect(editor.getCursorScreenPosition()).toEqual(row: 5, column: 27) describe "shift-click", -> - it "selects from the cursor's current location to the clicked location", -> + beforeEach -> editor.attachToDom() editor.css(position: 'absolute', top: 10, left: 10) - editor.setCursorScreenPosition([4, 7]) - editor.lines.trigger mousedownEvent(editor: editor, point: [5, 27], shiftKey: true) - expect(editor.getSelection().getScreenRange()).toEqual [[4, 7], [5, 27]] + it "selects from the cursor's current location to the clicked location", -> + editor.lines.trigger mousedownEvent(editor: editor, point: [5, 24], shiftKey: true) + expect(editor.getSelection().getScreenRange()).toEqual [[4, 7], [5, 24]] + + describe "when it is a double-click", -> + it "expands the selection to include the double-clicked word", -> + editor.lines.trigger mousedownEvent(editor: editor, point: [5, 24], shiftKey: true, originalEvent: { detail: 1 }) + editor.lines.trigger mousedownEvent(editor: editor, point: [5, 24], shiftKey: true, originalEvent: { detail: 2 }) + expect(editor.getSelection().getScreenRange()).toEqual [[4, 7], [5, 27]] describe "select-to-top", -> it "selects text from cusor position to the top of the buffer", -> diff --git a/src/app/cursor.coffee b/src/app/cursor.coffee index 37d4d795a..2a0c231c1 100644 --- a/src/app/cursor.coffee +++ b/src/app/cursor.coffee @@ -1,6 +1,7 @@ {View} = require 'space-pen' Anchor = require 'anchor' Point = require 'point' +Range = require 'range' _ = require 'underscore' module.exports = @@ -93,19 +94,37 @@ class Cursor extends View @setBufferPosition(nextPosition or @editor.getEofPosition()) moveToBeginningOfWord: -> + @setBufferPosition(@getBeginningOfCurrentWordBufferPosition()) + + moveToEndOfWord: -> + @setBufferPosition(@getEndOfCurrentWordBufferPosition()) + + getBeginningOfCurrentWordBufferPosition: (options = {}) -> + allowPrevious = options.allowPrevious ? true + position = null bufferPosition = @getBufferPosition() range = [[0,0], bufferPosition] @editor.backwardsTraverseRegexMatchesInRange @wordRegex, range, (match, matchRange, { stop }) => - @setBufferPosition matchRange.start + position = matchRange.start + if not allowPrevious and matchRange.end.isLessThan(bufferPosition) + position = bufferPosition stop() + position - moveToEndOfWord: -> + getEndOfCurrentWordBufferPosition: (options = {}) -> + allowNext = options.allowNext ? true + position = null bufferPosition = @getBufferPosition() range = [bufferPosition, @editor.getEofPosition()] - @editor.scanRegexMatchesInRange @wordRegex, range, (match, matchRange, { stop }) => - @setBufferPosition matchRange.end + position = matchRange.end + if not allowNext and matchRange.start.isGreaterThan(bufferPosition) + position = bufferPosition stop() + position + + getCurrentWordBufferRange: -> + new Range(@getBeginningOfCurrentWordBufferPosition(allowPrevious: false), @getEndOfCurrentWordBufferPosition(allowNext: false)) moveToEndOfLine: -> { row } = @getBufferPosition() diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 883368fca..bc442be04 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -140,7 +140,10 @@ class Editor extends View else @setCursorScreenPosition(screenPosition) else if clickCount == 2 - @compositeSelection.getLastSelection().selectWord() + if e.shiftKey + @compositeSelection.getLastSelection().expandOverWord() + else + @compositeSelection.getLastSelection().selectWord() else if clickCount >= 3 @compositeSelection.getLastSelection().selectLine() diff --git a/src/app/selection.coffee b/src/app/selection.coffee index fd9f71e60..6404eed40 100644 --- a/src/app/selection.coffee +++ b/src/app/selection.coffee @@ -160,21 +160,10 @@ class Selection extends View @retainSelection = false selectWord: -> - row = @cursor.getScreenPosition().row - column = @cursor.getScreenPosition().column + @setBufferRange(@cursor.getCurrentWordBufferRange()) - { row, column } = @cursor.getBufferPosition() - - line = @editor.buffer.lineForRow(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]) - @setBufferRange range + expandOverWord: -> + @setBufferRange(@getBufferRange().union(@cursor.getCurrentWordBufferRange())) selectLine: (row=@cursor.getBufferPosition().row) -> rowLength = @editor.buffer.lineForRow(row).length