Transfer word movement methods into cursor model

This commit is contained in:
Nathan Sobo
2012-06-07 15:33:01 -06:00
parent d50c8f2d0e
commit d872d12e04
5 changed files with 75 additions and 60 deletions

View File

@@ -11,7 +11,6 @@ class CursorView extends View
anchor: null
editor: null
wordRegex: /(\w+)|([^\w\s]+)/g
hidden: false
initialize: (@cursor, @editor) ->
@@ -77,57 +76,6 @@ class CursorView extends View
isOnEOL: ->
@getScreenPosition().column == @getCurrentBufferLine().length
moveToNextWord: ->
bufferPosition = @getBufferPosition()
range = [bufferPosition, @editor.getEofPosition()]
nextPosition = null
@editor.scanInRange @wordRegex, range, (match, matchRange, { stop }) =>
if matchRange.start.isGreaterThan(bufferPosition)
nextPosition = matchRange.start
stop()
@setBufferPosition(nextPosition or @editor.getEofPosition())
moveToBeginningOfWord: ->
@setBufferPosition(@getBeginningOfCurrentWordBufferPosition())
moveToEndOfWord: ->
@setBufferPosition(@getEndOfCurrentWordBufferPosition())
getBeginningOfCurrentWordBufferPosition: (options = {}) ->
allowPrevious = options.allowPrevious ? true
currentBufferPosition = @getBufferPosition()
previousRow = Math.max(0, currentBufferPosition.row - 1)
previousLinesRange = [[previousRow, 0], currentBufferPosition]
beginningOfWordPosition = currentBufferPosition
@editor.backwardsScanInRange @wordRegex, previousLinesRange, (match, matchRange, { stop }) =>
if matchRange.end.isGreaterThanOrEqual(currentBufferPosition) or allowPrevious
beginningOfWordPosition = matchRange.start
stop()
beginningOfWordPosition
getEndOfCurrentWordBufferPosition: (options = {}) ->
allowNext = options.allowNext ? true
position = null
bufferPosition = @getBufferPosition()
range = [bufferPosition, @editor.getEofPosition()]
@editor.scanInRange @wordRegex, range, (match, matchRange, { stop }) =>
position = matchRange.end
if not allowNext and matchRange.start.isGreaterThan(bufferPosition)
position = bufferPosition
stop()
position
getCurrentWordBufferRange: ->
new Range(@getBeginningOfCurrentWordBufferPosition(allowPrevious: false), @getEndOfCurrentWordBufferPosition(allowNext: false))
getCurrentLineBufferRange: ->
@editor.rangeForBufferRow(@getBufferPosition().row)
updateAppearance: ->
screenPosition = @getScreenPosition()
pixelPosition = @editor.pixelPositionForScreenPosition(screenPosition)

View File

@@ -1,4 +1,5 @@
Point = require 'point'
Range = require 'range'
Anchor = require 'new-anchor'
EventEmitter = require 'event-emitter'
_ = require 'underscore'
@@ -8,6 +9,7 @@ class Cursor
screenPosition: null
bufferPosition: null
goalColumn: null
wordRegex: /(\w+)|([^\w\s]+)/g
constructor: ({@editSession, screenPosition, bufferPosition}) ->
@anchor = new Anchor(@editSession)
@@ -84,4 +86,57 @@ class Cursor
moveToEndOfLine: ->
@setBufferPosition([@getBufferRow(), Infinity], clip: true)
moveToBeginningOfWord: ->
@setBufferPosition(@getBeginningOfCurrentWordBufferPosition())
moveToEndOfWord: ->
@setBufferPosition(@getEndOfCurrentWordBufferPosition())
moveToNextWord: ->
@setBufferPosition(@getBeginningOfNextWordBufferPosition())
getBeginningOfCurrentWordBufferPosition: (options = {}) ->
allowPrevious = options.allowPrevious ? true
currentBufferPosition = @getBufferPosition()
previousRow = Math.max(0, currentBufferPosition.row - 1)
previousLinesRange = [[previousRow, 0], currentBufferPosition]
beginningOfWordPosition = currentBufferPosition
@editSession.backwardsScanInRange @wordRegex, previousLinesRange, (match, matchRange, { stop }) =>
if matchRange.end.isGreaterThanOrEqual(currentBufferPosition) or allowPrevious
beginningOfWordPosition = matchRange.start
stop()
beginningOfWordPosition
getEndOfCurrentWordBufferPosition: (options = {}) ->
allowNext = options.allowNext ? true
currentBufferPosition = @getBufferPosition()
range = [currentBufferPosition, @editSession.getEofBufferPosition()]
endOfWordPosition = null
@editSession.scanInRange @wordRegex, range, (match, matchRange, { stop }) =>
endOfWordPosition = matchRange.end
if not allowNext and matchRange.start.isGreaterThan(currentBufferPosition)
endOfWordPosition = currentBufferPosition
stop()
endOfWordPosition
getBeginningOfNextWordBufferPosition: ->
currentBufferPosition = @getBufferPosition()
eofBufferPosition = @editSession.getEofBufferPosition()
range = [currentBufferPosition, eofBufferPosition]
nextWordPosition = eofBufferPosition
@editSession.scanInRange @wordRegex, range, (match, matchRange, { stop }) =>
if matchRange.start.isGreaterThan(currentBufferPosition)
nextWordPosition = matchRange.start
stop()
nextWordPosition
getCurrentWordBufferRange: ->
new Range(@getBeginningOfCurrentWordBufferPosition(allowPrevious: false), @getEndOfCurrentWordBufferPosition(allowNext: false))
getCurrentLineBufferRange: ->
@editSession.bufferRangeForBufferRow(@getBufferRow())
_.extend Cursor.prototype, EventEmitter

View File

@@ -63,6 +63,9 @@ class EditSession
scanInRange: (args...) ->
@buffer.scanInRange(args...)
backwardsScanInRange: (args...) ->
@buffer.backwardsScanInRange(args...)
getCursors: -> @cursors
addCursorAtScreenPosition: (screenPosition) ->
@@ -121,6 +124,15 @@ class EditSession
moveCursorToEndOfLine: ->
@moveCursors (cursor) -> cursor.moveToEndOfLine()
moveCursorToNextWord: ->
@moveCursors (cursor) -> cursor.moveToNextWord()
moveCursorToBeginningOfWord: ->
@moveCursors (cursor) -> cursor.moveToBeginningOfWord()
moveCursorToEndOfWord: ->
@moveCursors (cursor) -> cursor.moveToEndOfWord()
moveCursors: (fn) ->
fn(cursor) for cursor in @getCursors()
@mergeCursors()

View File

@@ -654,9 +654,9 @@ class Editor extends View
moveCursorDown: -> @activeEditSession.moveCursorDown()
moveCursorLeft: -> @activeEditSession.moveCursorLeft()
moveCursorRight: -> @activeEditSession.moveCursorRight()
moveCursorToNextWord: -> @compositeCursor.moveToNextWord()
moveCursorToBeginningOfWord: -> @compositeCursor.moveToBeginningOfWord()
moveCursorToEndOfWord: -> @compositeCursor.moveToEndOfWord()
moveCursorToNextWord: -> @activeEditSession.moveCursorToNextWord()
moveCursorToBeginningOfWord: -> @activeEditSession.moveCursorToBeginningOfWord()
moveCursorToEndOfWord: -> @activeEditSession.moveCursorToEndOfWord()
moveCursorToTop: -> @activeEditSession.moveCursorToTop()
moveCursorToBottom: -> @activeEditSession.moveCursorToBottom()
moveCursorToBeginningOfLine: -> @activeEditSession.moveCursorToBeginningOfLine()

View File

@@ -187,16 +187,16 @@ class Selection extends View
@retainSelection = false
selectWord: ->
@setBufferRange(@cursor.getCurrentWordBufferRange())
@setBufferRange(@cursor.cursor.getCurrentWordBufferRange())
expandOverWord: ->
@setBufferRange(@getBufferRange().union(@cursor.getCurrentWordBufferRange()))
@setBufferRange(@getBufferRange().union(@cursor.cursor.getCurrentWordBufferRange()))
selectLine: (row=@cursor.getBufferPosition().row) ->
@setBufferRange(@editor.rangeForBufferRow(row))
expandOverLine: ->
@setBufferRange(@getBufferRange().union(@cursor.getCurrentLineBufferRange()))
@setBufferRange(@getBufferRange().union(@cursor.cursor.getCurrentLineBufferRange()))
selectToScreenPosition: (position) ->
@modifySelection => @cursor.setScreenPosition(position)
@@ -229,10 +229,10 @@ class Selection extends View
@modifySelection => @cursor.cursor.moveToEndOfLine()
selectToBeginningOfWord: ->
@modifySelection => @cursor.moveToBeginningOfWord()
@modifySelection => @cursor.cursor.moveToBeginningOfWord()
selectToEndOfWord: ->
@modifySelection => @cursor.moveToEndOfWord()
@modifySelection => @cursor.cursor.moveToEndOfWord()
cutToEndOfLine: (maintainPasteboard) ->
@selectToEndOfLine() if @isEmpty()