Anchors and AnchorRanges are added to the buffer.

Also: Destroying an AnchorRange removes it from the EditSession / Buffer.
This commit is contained in:
Corey Johnson & Nathan Sobo
2012-07-13 17:18:10 -06:00
parent c7b7135388
commit 9eae88e97b
4 changed files with 48 additions and 8 deletions

View File

@@ -4,8 +4,10 @@ module.exports =
class AnchorRange
start: null
end: null
buffer: null
editSession: null # optional
constructor: (@editSession, bufferRange) ->
constructor: (bufferRange, @buffer, @editSession) ->
bufferRange = Range.fromObject(bufferRange)
@startAnchor = @editSession.addAnchorAtBufferPosition(bufferRange.start, ignoreEqual: true)
@endAnchor = @editSession.addAnchorAtBufferPosition(bufferRange.end)
@@ -22,3 +24,5 @@ class AnchorRange
destroy: ->
@startAnchor.destroy()
@endAnchor.destroy()
@buffer.removeAnchorRange(this)
@editSession?.removeAnchorRange(this)

View File

@@ -4,12 +4,17 @@ _ = require 'underscore'
module.exports =
class Anchor
editor: null
buffer: null
editSession: null # optional
bufferPosition: null
screenPosition: null
ignoreEqual: false
strong: false
constructor: (@editSession, options = {}) ->
{ @ignoreEqual, @strong } = options
constructor: (@buffer, options = {}) ->
throw new Error("no edit session!") unless options.editSession
{ @editSession, @ignoreEqual, @strong } = options
handleBufferChange: (e) ->
{ oldRange, newRange } = e
@@ -69,7 +74,8 @@ class Anchor
@setScreenPosition(screenPosition, bufferChange: options.bufferChange, clip: false, assignBufferPosition: false)
destroy: ->
@editSession.removeAnchor(this)
@buffer.removeAnchor(this)
@editSession?.removeAnchor(this)
@trigger 'destroy'
@off()

View File

@@ -6,6 +6,8 @@ Range = require 'range'
EventEmitter = require 'event-emitter'
UndoManager = require 'undo-manager'
BufferChangeOperation = require 'buffer-change-operation'
Anchor = require 'anchor'
AnchorRange = require 'anchor-range'
module.exports =
class Buffer
@@ -14,9 +16,13 @@ class Buffer
modified: null
lines: null
file: null
anchors: null
anchorRanges: null
constructor: (path) ->
@id = @constructor.idCounter++
@anchors = []
@anchorRanges = []
@lines = ['']
if path
@@ -173,6 +179,27 @@ class Buffer
isModified: ->
@modified
addAnchor: (options) ->
anchor = new Anchor(this, options)
@anchors.push(anchor)
anchor
addAnchorAtPosition: (position, options) ->
anchor = @addAnchor(options)
anchor.setBufferPosition(position)
anchor
addAnchorRange: (range, editSession) ->
anchorRange = new AnchorRange(range, this, editSession)
@anchorRanges.push(anchorRange)
anchorRange
removeAnchor: (anchor) ->
_.remove(@anchors, anchor)
removeAnchorRange: (anchorRange) ->
_.remove(@anchorRanges, anchorRange)
matchesInCharacterRange: (regex, startIndex, endIndex) ->
text = @getText()
matches = []

View File

@@ -258,8 +258,8 @@ class EditSession
getAnchors: ->
new Array(@anchors...)
addAnchor: (options) ->
anchor = new Anchor(this, options)
addAnchor: (options={}) ->
anchor = @buffer.addAnchor(_.extend({editSession: this}, options))
@anchors.push(anchor)
anchor
@@ -269,13 +269,16 @@ class EditSession
anchor
addAnchorRange: (range) ->
anchorRange = new AnchorRange(this, range)
anchorRange = @buffer.addAnchorRange(range, this)
@anchorRanges.push(anchorRange)
anchorRange
removeAnchor: (anchor) ->
_.remove(@anchors, anchor)
removeAnchorRange: (anchorRange) ->
_.remove(@anchorRanges, anchorRange)
getCursors: -> new Array(@cursors...)
getCursor: (index=0) ->