Switch EditSession over to object-oriented markers API

This commit is contained in:
Nathan Sobo 2013-04-26 17:45:13 -06:00
parent 5403bc647a
commit f4be899ae9
5 changed files with 34 additions and 171 deletions

View File

@ -744,17 +744,10 @@ describe "EditSession", ->
expect(editSession.lineForScreenRow(1).fold).toBeDefined()
describe ".selectMarker(marker)", ->
describe "when the marker exists", ->
it "selects the marker's range and returns true", ->
marker = editSession.markBufferRange([[0, 1], [3, 3]])
expect(editSession.selectMarker(marker)).toBeTruthy()
expect(editSession.getSelectedBufferRange()).toEqual [[0, 1], [3, 3]]
describe "when the marker does not exist", ->
it "does not select the marker's range and returns false", ->
rangeBefore = editSession.getSelectedBufferRange()
expect(editSession.selectMarker('bogus')).toBeFalsy()
expect(editSession.getSelectedBufferRange()).toEqual rangeBefore
it "selects the marker's range and returns true", ->
marker = editSession.markBufferRange([[0, 1], [3, 3]])
expect(editSession.selectMarker(marker)).toBeTruthy()
expect(editSession.getSelectedBufferRange()).toEqual [[0, 1], [3, 3]]
describe ".addSelectionBelow()", ->
describe "when the selection is non-empty", ->

View File

@ -20,7 +20,7 @@ class Cursor
constructor: ({@editSession, @marker}) ->
@updateVisibility()
@editSession.observeMarker @marker, (e) =>
@marker.observe (e) =>
@updateVisibility()
{oldHeadScreenPosition, newHeadScreenPosition} = e
{oldHeadBufferPosition, newHeadBufferPosition} = e
@ -42,7 +42,7 @@ class Cursor
destroy: ->
@destroyed = true
@editSession.destroyMarker(@marker)
@marker.destroy()
@editSession.removeCursor(this)
@trigger 'destroyed'
@ -65,13 +65,13 @@ class Cursor
#
setScreenPosition: (screenPosition, options={}) ->
@changePosition options, =>
@editSession.setMarkerHeadScreenPosition(@marker, screenPosition, options)
@marker.setHeadScreenPosition(screenPosition, options)
# Public: Gets the screen position of the cursor.
#
# Returns an {Array} of two numbers: the screen row, and the screen column.
getScreenPosition: ->
@editSession.getMarkerHeadScreenPosition(@marker)
@marker.getHeadScreenPosition()
# Public: Moves a cursor to a given buffer position.
#
@ -81,17 +81,17 @@ class Cursor
#
setBufferPosition: (bufferPosition, options={}) ->
@changePosition options, =>
@editSession.setMarkerHeadBufferPosition(@marker, bufferPosition, options)
@marker.setHeadBufferPosition(bufferPosition, options)
# Public: Gets the current buffer position.
#
# Returns an {Array} of two numbers: the buffer row, and the buffer column.
getBufferPosition: ->
@editSession.getMarkerHeadBufferPosition(@marker)
@marker.getHeadBufferPosition()
# Public: If the marker range is empty, the cursor is marked as being visible.
updateVisibility: ->
@setVisible(@editSession.isMarkerRangeEmpty(@marker))
@setVisible(@marker.getBufferRange().isEmpty())
# Public: Sets the visibility of the cursor.
#

View File

@ -131,6 +131,10 @@ class DisplayBufferMarker
isReversed: ->
@bufferMarker.isReversed()
destroy: ->
delete @displayBuffer.markers[@id]
@bufferMarker.destroy()
###
# Internal #
###

View File

@ -922,6 +922,9 @@ class EditSession
# Public #
###
getMarker: (id) ->
@displayBuffer.getMarker(id)
# Public: Constructs a new marker at the given screen range.
#
# range - The marker {Range} (representing the distance between the head and tail)
@ -986,143 +989,6 @@ class EditSession
setMarkerScreenRange: (args...) ->
@displayBuffer.setMarkerScreenRange(args...)
# Public: Gets the buffer range of the display marker.
#
# id - The {Number} of the ID to check
#
# Returns a {Range}.
getMarkerBufferRange: (args...) ->
@displayBuffer.getMarkerBufferRange(args...)
# Public: Modifies the buffer range of the display marker.
#
# id - The {Number} of the ID to check
# screenRange - The new {Range} to use
# options - A hash of options matching those found in {BufferMarker.setRange}
setMarkerBufferRange: (args...) ->
@displayBuffer.setMarkerBufferRange(args...)
# Public: Retrieves the screen position of the marker's head.
#
# id - The {Number} of the ID to check
#
# Returns a {Point}.
getMarkerScreenPosition: (args...) ->
@displayBuffer.getMarkerScreenPosition(args...)
# Public: Retrieves the buffer position of the marker's head.
#
# id - The {Number} of the ID to check
#
# Returns a {Point}.
getMarkerBufferPosition: (args...) ->
@displayBuffer.getMarkerBufferPosition(args...)
# Public: Retrieves the screen position of the marker's head.
#
# id - The {Number} of the ID to check
#
# Returns a {Point}.
getMarkerHeadScreenPosition: (args...) ->
@displayBuffer.getMarkerHeadScreenPosition(args...)
# Public: Sets the screen position of the marker's head.
#
# id - The {Number} of the ID to change
# screenRange - The new {Point} to use
# options - A hash of options matching those found in {DisplayBuffer.bufferPositionForScreenPosition}
setMarkerHeadScreenPosition: (args...) ->
@displayBuffer.setMarkerHeadScreenPosition(args...)
# Public: Retrieves the buffer position of the marker's head.
#
# id - The {Number} of the ID to check
#
# Returns a {Point}.
getMarkerHeadBufferPosition: (args...) ->
@displayBuffer.getMarkerHeadBufferPosition(args...)
# Public: Sets the buffer position of the marker's head.
#
# id - The {Number} of the ID to check
# screenRange - The new {Point} to use
# options - A hash of options matching those found in {DisplayBuffer.bufferPositionForScreenPosition}
setMarkerHeadBufferPosition: (args...) ->
@displayBuffer.setMarkerHeadBufferPosition(args...)
# Public: Retrieves the screen position of the marker's tail.
#
# id - The {Number} of the ID to check
#
# Returns a {Point}.
getMarkerTailScreenPosition: (args...) ->
@displayBuffer.getMarkerTailScreenPosition(args...)
# Public: Sets the screen position of the marker's tail.
#
# id - The {Number} of the ID to change
# screenRange - The new {Point} to use
# options - A hash of options matching those found in {DisplayBuffer.bufferPositionForScreenPosition}
setMarkerTailScreenPosition: (args...) ->
@displayBuffer.setMarkerTailScreenPosition(args...)
# Public: Retrieves the buffer position of the marker's tail.
#
# id - The {Number} of the ID to check
#
# Returns a {Point}.
getMarkerTailBufferPosition: (args...) ->
@displayBuffer.getMarkerTailBufferPosition(args...)
# Public: Sets the buffer position of the marker's tail.
#
# id - The {Number} of the ID to change
# screenRange - The new {Point} to use
# options - A hash of options matching those found in {DisplayBuffer.bufferPositionForScreenPosition}
setMarkerTailBufferPosition: (args...) ->
@displayBuffer.setMarkerTailBufferPosition(args...)
# Public: Sets a callback to be fired whenever a marker is changed.
#
# id - A {Number} representing the marker to watch
# callback - A {Function} to execute
observeMarker: (args...) ->
@displayBuffer.observeMarker(args...)
# Public: Sets the marker's tail to the same position as the marker's head.
#
# This only works if there isn't already a tail position.
#
# id - A {Number} representing the marker to change
#
# Returns a {Point} representing the new tail position.
placeMarkerTail: (args...) ->
@displayBuffer.placeMarkerTail(args...)
# Public: Removes the tail from the marker.
#
# id - A {Number} representing the marker to change
clearMarkerTail: (args...) ->
@displayBuffer.clearMarkerTail(args...)
# Public: Identifies if the ending position of a marker is greater than the starting position.
#
# This can happen when, for example, you highlight text "up" in a {Buffer}.
#
# id - A {Number} representing the marker to check
#
# Returns a {Boolean}.
isMarkerReversed: (args...) ->
@displayBuffer.isMarkerReversed(args...)
# Public: Identifies if the marker's head position is equal to its tail.
#
# id - A {Number} representing the marker to check
#
# Returns a {Boolean}.
isMarkerRangeEmpty: (args...) ->
@displayBuffer.isMarkerRangeEmpty(args...)
# Public: Returns `true` if there are multiple cursors in the edit session.
#
# Returns a {Boolean}.
@ -1185,7 +1051,7 @@ class EditSession
# Returns the new {Selection}.
addSelection: (marker, options={}) ->
unless options.preserveFolds
@destroyFoldsIntersectingBufferRange(@getMarkerBufferRange(marker))
@destroyFoldsIntersectingBufferRange(marker.getBufferRange())
cursor = @addCursor(marker)
selection = new Selection(_.extend({editSession: this, marker, cursor}, options))
@selections.push(selection)
@ -1544,8 +1410,8 @@ class EditSession
expandLastSelectionOverWord: ->
@getLastSelection().expandOverWord()
selectMarker: (id) ->
if bufferRange = @getMarkerBufferRange(id)
selectMarker: (marker) ->
if bufferRange = marker.getBufferRange()
@setSelectedBufferRange(bufferRange)
true
else

View File

@ -19,7 +19,7 @@ class Selection
constructor: ({@cursor, @marker, @editSession, @goalBufferRange}) ->
@cursor.selection = this
@editSession.observeMarker @marker, => @screenRangeChanged()
@marker.observe => @screenRangeChanged()
@cursor.on 'destroyed.selection', =>
@cursor = null
@destroy()
@ -54,7 +54,7 @@ class Selection
#
# Returns a {Boolean}.
isReversed: ->
@editSession.isMarkerReversed(@marker)
@marker.isReversed()
# Public: Identifies if the selection is a single line.
#
@ -66,7 +66,7 @@ class Selection
#
# Returns a {Range}.
getScreenRange: ->
@editSession.getMarkerScreenRange(@marker)
@marker.getScreenRange()
# Public: Modifies the screen range for the selection.
#
@ -79,7 +79,7 @@ class Selection
#
# Returns a {Range}.
getBufferRange: ->
@editSession.getMarkerBufferRange(@marker)
@marker.getBufferRange()
# Public: Modifies the buffer range for the selection.
#
@ -94,7 +94,7 @@ class Selection
@editSession.destroyFoldsIntersectingBufferRange(bufferRange) unless options.preserveFolds
@modifySelection =>
@cursor.needsAutoscroll = false if options.autoscroll?
@editSession.setMarkerBufferRange(@marker, bufferRange, options)
@marker.setBufferRange(bufferRange, options)
# Public: Retrieves the starting and ending buffer rows the selection is highlighting.
#
@ -119,7 +119,7 @@ class Selection
# Public: Clears the selection, moving the marker to move to the head.
clear: ->
@editSession.clearMarkerTail(@marker)
@marker.clearTail()
# Public: Modifies the selection to mark the current word.
#
@ -156,9 +156,9 @@ class Selection
@modifySelection =>
if @initialScreenRange
if position.isLessThan(@initialScreenRange.start)
@editSession.setMarkerScreenRange(@marker, [position, @initialScreenRange.end], reverse: true)
@marker.setScreenRange([position, @initialScreenRange.end], reverse: true)
else
@editSession.setMarkerScreenRange(@marker, [@initialScreenRange.start, position])
@marker.setScreenRange([@initialScreenRange.start, position])
else
@cursor.setScreenPosition(position)
@ -438,9 +438,9 @@ class Selection
@deleteSelectedText()
if joinMarker?
newSelectedRange = @editSession.getMarkerBufferRange(joinMarker)
newSelectedRange = joinMarker.getBufferRange()
@setBufferRange(newSelectedRange)
@editSession.destroyMarker(joinMarker)
joinMarker.destroy()
outdentSelectedRows: ->
[start, end] = @getBufferRowRange()
@ -507,7 +507,7 @@ class Selection
@retainSelection = false
placeTail: ->
@editSession.placeMarkerTail(@marker)
@marker.placeTail()
# Public: Identifies if a selection intersects with a given buffer range.
#