mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Anchors and AnchorRanges are added to the buffer.
Also: Destroying an AnchorRange removes it from the EditSession / Buffer.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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 = []
|
||||
|
||||
@@ -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) ->
|
||||
|
||||
Reference in New Issue
Block a user