Move moveTo(Beginning/FirstCharacter/End)OfLine methods to cursor model

Also, implement move to end of line by adding a clip option to setBufferPosition and passing [row, Infinity]
This commit is contained in:
Nathan Sobo
2012-06-07 11:28:40 -06:00
parent fccc88acaf
commit 21ae28657c
8 changed files with 58 additions and 29 deletions

View File

@@ -128,24 +128,6 @@ class CursorView extends View
getCurrentLineBufferRange: ->
@editor.rangeForBufferRow(@getBufferPosition().row)
moveToEndOfLine: ->
{ row } = @getBufferPosition()
@setBufferPosition({ row, column: @editor.buffer.lineForRow(row).length })
moveToBeginningOfLine: ->
{ row } = @getScreenPosition()
@setScreenPosition({ row, column: 0 })
moveToFirstCharacterOfLine: ->
position = @getBufferPosition()
range = @editor.rangeForBufferRow(position.row)
newPosition = null
@editor.scanInRange /^\s*/, range, (match, matchRange) =>
newPosition = matchRange.end
return unless newPosition
newPosition = [position.row, 0] if newPosition.isEqual(position)
@setBufferPosition(newPosition)
updateAppearance: ->
screenPosition = @getScreenPosition()
pixelPosition = @editor.pixelPositionForScreenPosition(screenPosition)

View File

@@ -14,6 +14,10 @@ class Cursor
@setScreenPosition(screenPosition) if screenPosition
@setBufferPosition(bufferPosition) if bufferPosition
destroy: ->
@editSession.removeCursor(this)
@trigger 'destroy'
setScreenPosition: (screenPosition, options) ->
@anchor.setScreenPosition(screenPosition, options)
@goalColumn = null
@@ -30,6 +34,9 @@ class Cursor
getBufferPosition: ->
@anchor.getBufferPosition()
getBufferRow: ->
@getBufferPosition().row
handleBufferChange: (e) ->
@anchor.handleBufferChange(e)
@trigger 'change-screen-position', @getScreenPosition(), bufferChange: true
@@ -61,8 +68,20 @@ class Cursor
moveToBottom: ->
@setBufferPosition(@editSession.getEofBufferPosition())
destroy: ->
@editSession.removeCursor(this)
@trigger 'destroy'
moveToBeginningOfLine: ->
@setBufferPosition([@getBufferRow(), 0])
moveToFirstCharacterOfLine: ->
position = @getBufferPosition()
range = @editSession.bufferRangeForBufferRow(position.row)
newPosition = null
@editSession.scanInRange /^\s*/, range, (match, matchRange) =>
newPosition = matchRange.end
return unless newPosition
newPosition = [position.row, 0] if newPosition.isEqual(position)
@setBufferPosition(newPosition)
moveToEndOfLine: ->
@setBufferPosition([@getBufferRow(), Infinity], clip: true)
_.extend Cursor.prototype, EventEmitter

View File

@@ -51,9 +51,18 @@ class EditSession
clipScreenPosition: (screenPosition, options) ->
@renderer.clipScreenPosition(screenPosition, options)
clipBufferPosition: (bufferPosition, options) ->
@renderer.clipBufferPosition(bufferPosition, options)
getEofBufferPosition: ->
@buffer.getEofPosition()
bufferRangeForBufferRow: (row) ->
@buffer.rangeForRow(row)
scanInRange: (args...) ->
@buffer.scanInRange(args...)
getCursors: -> @cursors
addCursorAtScreenPosition: (screenPosition) ->
@@ -103,6 +112,15 @@ class EditSession
moveCursorToBottom: ->
@moveCursors (cursor) -> cursor.moveToBottom()
moveCursorToBeginningOfLine: ->
@moveCursors (cursor) -> cursor.moveToBeginningOfLine()
moveCursorToFirstCharacterOfLine: ->
@moveCursors (cursor) -> cursor.moveToFirstCharacterOfLine()
moveCursorToEndOfLine: ->
@moveCursors (cursor) -> cursor.moveToEndOfLine()
moveCursors: (fn) ->
fn(cursor) for cursor in @getCursors()
@mergeCursors()

View File

@@ -649,9 +649,9 @@ class Editor extends View
moveCursorToEndOfWord: -> @compositeCursor.moveToEndOfWord()
moveCursorToTop: -> @activeEditSession.moveCursorToTop()
moveCursorToBottom: -> @activeEditSession.moveCursorToBottom()
moveCursorToBeginningOfLine: -> @compositeCursor.moveToBeginningOfLine()
moveCursorToFirstCharacterOfLine: -> @compositeCursor.moveToFirstCharacterOfLine()
moveCursorToEndOfLine: -> @compositeCursor.moveToEndOfLine()
moveCursorToBeginningOfLine: -> @activeEditSession.moveCursorToBeginningOfLine()
moveCursorToFirstCharacterOfLine: -> @activeEditSession.moveCursorToFirstCharacterOfLine()
moveCursorToEndOfLine: -> @activeEditSession.moveCursorToEndOfLine()
setCursorScreenPosition: (position) -> @activeEditSession.setCursorScreenPosition(position)
getCursorScreenPosition: -> @activeEditSession.getCursorScreenPosition()
setCursorBufferPosition: (position) -> @activeEditSession.setCursorBufferPosition(position)

View File

@@ -81,6 +81,9 @@ class LineMap
clipScreenPosition: (screenPosition, options) ->
@clipPosition('screenDelta', screenPosition, options)
clipBufferPosition: (bufferPosition, options) ->
@clipPosition('bufferDelta', bufferPosition, options)
clipPosition: (deltaType, position, options={}) ->
options.clipToBounds = true
@translatePosition(deltaType, deltaType, position, options)

View File

@@ -26,8 +26,12 @@ class Anchor
getBufferPosition: ->
@bufferPosition
setBufferPosition: (position, options) ->
@bufferPosition = Point.fromObject(position)
setBufferPosition: (position, options={}) ->
if options.clip
@bufferPosition = @editSession.clipBufferPosition(position)
else
@bufferPosition = Point.fromObject(position)
screenPosition = @editSession.screenPositionForBufferPosition(@bufferPosition, options)
@setScreenPosition(screenPosition, clip: false, assignBufferPosition: false)

View File

@@ -159,9 +159,12 @@ class Renderer
bufferPositionForScreenPosition: (position, options) ->
@lineMap.bufferPositionForScreenPosition(position, options)
clipScreenPosition: (position, options={}) ->
clipScreenPosition: (position, options) ->
@lineMap.clipScreenPosition(position, options)
clipBufferPosition: (position, options) ->
@lineMap.clipBufferPosition(position, options)
handleBufferChange: (e) ->
allFolds = [] # Folds can modify @activeFolds, so first make sure we have a stable array of folds
allFolds.push(folds...) for row, folds of @activeFolds

View File

@@ -223,10 +223,10 @@ class Selection extends View
@setBufferRange(@editor.buffer.getRange())
selectToBeginningOfLine: ->
@modifySelection => @cursor.moveToBeginningOfLine()
@modifySelection => @cursor.cursor.moveToBeginningOfLine()
selectToEndOfLine: ->
@modifySelection => @cursor.moveToEndOfLine()
@modifySelection => @cursor.cursor.moveToEndOfLine()
selectToBeginningOfWord: ->
@modifySelection => @cursor.moveToBeginningOfWord()