mirror of
https://github.com/atom/atom.git
synced 2026-02-11 07:05:11 -05:00
Cursors and selections no longer need to handle buffer/display-buffer changes directly. They register anchors with the EditSession, and subscribe to changes in their position instead. This opens up the anchor facility for use by extensions that want to track the screen position of a point in the buffer as the buffer and display buffer change.
60 lines
1.9 KiB
CoffeeScript
60 lines
1.9 KiB
CoffeeScript
Point = require 'point'
|
|
EventEmitter = require 'event-emitter'
|
|
_ = require 'underscore'
|
|
|
|
module.exports =
|
|
class Anchor
|
|
editor: null
|
|
bufferPosition: null
|
|
screenPosition: null
|
|
|
|
constructor: (@editSession) ->
|
|
|
|
handleBufferChange: (e) ->
|
|
{ oldRange, newRange } = e
|
|
position = @getBufferPosition()
|
|
return if position.isLessThan(oldRange.end)
|
|
|
|
newRow = newRange.end.row
|
|
newColumn = newRange.end.column
|
|
if position.row == oldRange.end.row
|
|
newColumn += position.column - oldRange.end.column
|
|
else
|
|
newColumn = position.column
|
|
newRow += position.row - oldRange.end.row
|
|
|
|
@setBufferPosition([newRow, newColumn], bufferChange: true)
|
|
|
|
getBufferPosition: ->
|
|
@bufferPosition
|
|
|
|
setBufferPosition: (position, options={}) ->
|
|
@bufferPosition = Point.fromObject(position)
|
|
clip = options.clip ? true
|
|
@bufferPosition = @editSession.clipBufferPosition(@bufferPosition) if clip
|
|
@refreshScreenPosition(options)
|
|
|
|
getScreenPosition: ->
|
|
@screenPosition
|
|
|
|
setScreenPosition: (position, options={}) ->
|
|
previousScreenPosition = @screenPosition
|
|
@screenPosition = Point.fromObject(position)
|
|
clip = options.clip ? true
|
|
assignBufferPosition = options.assignBufferPosition ? true
|
|
|
|
@screenPosition = @editSession.clipScreenPosition(@screenPosition, options) if clip
|
|
@bufferPosition = @editSession.bufferPositionForScreenPosition(@screenPosition, options) if assignBufferPosition
|
|
|
|
Object.freeze @screenPosition
|
|
Object.freeze @bufferPosition
|
|
|
|
unless @screenPosition.isEqual(previousScreenPosition)
|
|
@trigger 'change-screen-position', @screenPosition, bufferChange: options.bufferChange
|
|
|
|
refreshScreenPosition: (options={}) ->
|
|
screenPosition = @editSession.screenPositionForBufferPosition(@bufferPosition, options)
|
|
@setScreenPosition(screenPosition, bufferChange: options.bufferChange, clip: false, assignBufferPosition: false)
|
|
|
|
_.extend(Anchor.prototype, EventEmitter)
|