mirror of
https://github.com/atom/atom.git
synced 2026-02-12 23:55:10 -05:00
Shift-double-click expands the selection over the clicked word
Also factored `get(Beginning|End)OfCurrentWordBufferPosition` methods out of `moveTo(Beginning|End)OfWord` on Cursor. They are used by a `getCurrentWordBufferRange` method, which Selection now uses to find the range of the current word.
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user