Move from addDecoration -> decorateMarker

This commit is contained in:
Ben Ogle
2014-07-07 14:43:56 -07:00
parent fe9fec733d
commit e991b3d10c
6 changed files with 44 additions and 66 deletions

View File

@@ -1044,16 +1044,25 @@ describe "DisplayBuffer", ->
decoration = {type: 'gutter', class: 'one'}
marker = displayBuffer.markBufferRange([[2, 13], [3, 15]])
decorationObject = displayBuffer.addDecorationForMarker(marker, decoration)
decorationObject = displayBuffer.decorateMarker(marker, decoration)
expect(decorationObject).toBeDefined()
expect(decorationObject.getParams()).toBe decoration
expect(displayBuffer.decorationForId(decoration.id)).toBe decorationObject
expect(displayBuffer.decorationsForScreenRowRange(2, 3)[marker.id][0]).toBe decorationObject
displayBuffer.removeDecorationForMarker(marker, decoration)
decorationObject.destroy()
expect(displayBuffer.decorationsForScreenRowRange(2, 3)[marker.id]).not.toBeDefined()
expect(displayBuffer.decorationForId(decoration.id)).not.toBeDefined()
it "will not fail if the decoration is removed twice", ->
decoration = {type: 'gutter', class: 'one'}
marker = displayBuffer.markBufferRange([[2, 13], [3, 15]])
decorationObject = displayBuffer.decorateMarker(marker, decoration)
decorationObject.destroy()
decorationObject.destroy()
expect(displayBuffer.decorationForId(decoration.id)).not.toBeDefined()
describe "::setScrollTop", ->
beforeEach ->
displayBuffer.manageScrollPosition = true

View File

@@ -757,12 +757,12 @@ describe "EditorComponent", ->
expect(selectionNode.classList.contains('flash')).toBe true
describe "line decoration rendering", ->
[marker, decoration] = []
[marker, decoration, decorationParams] = []
beforeEach ->
marker = editor.displayBuffer.markBufferRange([[2, 13], [3, 15]], invalidate: 'inside')
decoration = {type: ['gutter', 'line'], class: 'a'}
editor.addDecorationForMarker(marker, decoration)
decorationParams = {type: ['gutter', 'line'], class: 'a'}
decoration = editor.decorateMarker(marker, decorationParams)
runSetImmediateCallbacks()
it "applies line decoration classes to lines and line numbers", ->
@@ -776,7 +776,7 @@ describe "EditorComponent", ->
# Add decorations that are out of range
marker2 = editor.displayBuffer.markBufferRange([[9, 0], [9, 0]])
editor.addDecorationForMarker(marker2, type: ['gutter', 'line'], class: 'b')
editor.decorateMarker(marker2, type: ['gutter', 'line'], class: 'b')
runSetImmediateCallbacks()
# Scroll decorations into view
@@ -799,7 +799,7 @@ describe "EditorComponent", ->
marker.destroy()
marker = editor.markBufferRange([[0, 0], [0, 2]])
editor.addDecorationForMarker(marker, type: ['gutter', 'line'], class: 'b')
editor.decorateMarker(marker, type: ['gutter', 'line'], class: 'b')
runSetImmediateCallbacks()
expect(lineNumberHasClass(0, 'b')).toBe true
expect(lineNumberHasClass(1, 'b')).toBe false
@@ -833,7 +833,7 @@ describe "EditorComponent", ->
it "remove decoration classes and unsubscribes from markers decorations are removed", ->
expect(marker.getSubscriptionCount('changed'))
editor.removeDecorationForMarker(marker, decoration)
decoration.destroy()
runSetImmediateCallbacks()
expect(lineNumberHasClass(1, 'a')).toBe false
expect(lineNumberHasClass(2, 'a')).toBe false
@@ -868,7 +868,7 @@ describe "EditorComponent", ->
describe "when the decoration's 'onlyHead' property is true", ->
it "only applies the decoration's class to lines containing the marker's head", ->
editor.addDecorationForMarker(marker, type: ['gutter', 'line'], class: 'only-head', onlyHead: true)
editor.decorateMarker(marker, type: ['gutter', 'line'], class: 'only-head', onlyHead: true)
runSetImmediateCallbacks()
expect(lineAndLineNumberHaveClass(1, 'only-head')).toBe false
expect(lineAndLineNumberHaveClass(2, 'only-head')).toBe false
@@ -877,7 +877,7 @@ describe "EditorComponent", ->
describe "when the decoration's 'onlyEmpty' property is true", ->
it "only applies the decoration when its marker is empty", ->
editor.addDecorationForMarker(marker, type: ['gutter', 'line'], class: 'only-empty', onlyEmpty: true)
editor.decorateMarker(marker, type: ['gutter', 'line'], class: 'only-empty', onlyEmpty: true)
runSetImmediateCallbacks()
expect(lineAndLineNumberHaveClass(2, 'only-empty')).toBe false
expect(lineAndLineNumberHaveClass(3, 'only-empty')).toBe false
@@ -889,7 +889,7 @@ describe "EditorComponent", ->
describe "when the decoration's 'onlyNonEmpty' property is true", ->
it "only applies the decoration when its marker is non-empty", ->
editor.addDecorationForMarker(marker, type: ['gutter', 'line'], class: 'only-non-empty', onlyNonEmpty: true)
editor.decorateMarker(marker, type: ['gutter', 'line'], class: 'only-non-empty', onlyNonEmpty: true)
runSetImmediateCallbacks()
expect(lineAndLineNumberHaveClass(2, 'only-non-empty')).toBe true
expect(lineAndLineNumberHaveClass(3, 'only-non-empty')).toBe true
@@ -905,7 +905,7 @@ describe "EditorComponent", ->
scrollViewClientLeft = node.querySelector('.scroll-view').getBoundingClientRect().left
marker = editor.displayBuffer.markBufferRange([[2, 13], [3, 15]], invalidate: 'inside')
decorationParams = {type: 'highlight', class: 'test-highlight'}
decoration = editor.addDecorationForMarker(marker, decorationParams)
decoration = editor.decorateMarker(marker, decorationParams)
runSetImmediateCallbacks()
it "does not render highlights for off-screen lines until they come on-screen", ->
@@ -914,7 +914,7 @@ describe "EditorComponent", ->
runSetImmediateCallbacks()
marker = editor.displayBuffer.markBufferRange([[9, 2], [9, 4]], invalidate: 'inside')
editor.addDecorationForMarker(marker, type: 'highlight', class: 'some-highlight')
editor.decorateMarker(marker, type: 'highlight', class: 'some-highlight')
runSetImmediateCallbacks()
# Should not be rendering range containing the marker
@@ -942,7 +942,7 @@ describe "EditorComponent", ->
expect(regions.length).toBe 2
it "removes highlights when a decoration is removed", ->
editor.removeDecorationForMarker(marker, decorationParams)
decoration.destroy()
runSetImmediateCallbacks()
regions = node.querySelectorAll('.test-highlight .region')
expect(regions.length).toBe 0

View File

@@ -14,13 +14,14 @@ class Decoration
else
type is decorationParams.type
constructor: (@marker, @params) ->
constructor: (@marker, @displayBuffer, @params) ->
@id = nextId()
@params.id = @id
@flashQueue = null
destroy: ->
# TODO: implement
@displayBuffer.removeDecoration(this)
@emit 'destoryed'
update: (newParams) ->
# TODO: implement
@@ -41,7 +42,7 @@ class Decoration
flashObject = {class: klass, duration}
@flashQueue ?= []
@flashQueue.push(flashObject)
@emit('flash')
@emit 'flash'
consumeNextFlash: ->
return @flashQueue.shift() if @flashQueue?.length > 0

View File

@@ -767,7 +767,7 @@ class DisplayBuffer extends Model
decorationsByMarkerId[marker.id] = decorations
decorationsByMarkerId
addDecorationForMarker: (marker, decorationParams) ->
decorateMarker: (marker, decorationParams) ->
marker = @getMarker(marker.id)
@decorationMarkerDestroyedSubscriptions[marker.id] ?= @subscribe marker, 'destroyed', =>
@@ -782,24 +782,23 @@ class DisplayBuffer extends Model
for decoration in decorations
@emit 'decoration-changed', marker, decoration, event
decoration = new Decoration(marker, decorationParams)
decoration = new Decoration(marker, this, decorationParams)
@decorationsByMarkerId[marker.id] ?= []
@decorationsByMarkerId[marker.id].push(decoration)
@decorationsById[decoration.id] = decoration
@emit 'decoration-added', marker, decoration
decoration
removeDecorationForMarker: (marker, decorationPattern) ->
removeDecoration: (decoration) ->
{marker} = decoration
return unless decorations = @decorationsByMarkerId[marker.id]
index = decorations.indexOf(decoration)
for i in [decorations.length - 1..0]
decoration = decorations[i]
if decoration.matchesPattern(decorationPattern)
decorations.splice(i, 1)
delete @decorationsById[decoration.id]
@emit 'decoration-removed', marker, decoration
@removedAllMarkerDecorations(marker) if decorations.length is 0
if index > -1
decorations.splice(index, 1)
delete @decorationsById[decoration.id]
@emit 'decoration-removed', marker, decoration
@removedAllMarkerDecorations(marker) if decorations.length is 0
removeAllDecorationsForMarker: (marker) ->
decorations = @decorationsByMarkerId[marker.id].slice()
@@ -1092,7 +1091,7 @@ class DisplayBuffer extends Model
@emit 'marker-created', @getMarker(marker.id)
createFoldForMarker: (marker) ->
@addDecorationForMarker(marker, type: 'gutter', class: 'folded')
@decorateMarker(marker, type: 'gutter', class: 'folded')
new Fold(this, marker)
foldForMarker: (marker) ->

View File

@@ -1109,39 +1109,8 @@ class Editor extends Model
# * onlyNonEmpty: If `true`, the decoration will only be applied if the
# associated marker is non-empty. Only applicable to the `line` and
# gutter types.
addDecorationForMarker: (marker, decoration) ->
@displayBuffer.addDecorationForMarker(marker, decoration)
# Public: Removes all decorations associated with a {Marker} that match a
# `decorationPattern` and stop tracking the {Marker}.
#
# ```coffee
# marker = editor.markBufferRange([[4, 13], [5, 17]])
# editor.removeDecorationForMarker(marker, {type: 'gutter', class: 'linter-error'})
# ```
#
# All decorations matching a pattern will be removed. For example, you might
# have decorations with a namespace like this attached to a row:
#
# ```coffee
# [
# {type: 'gutter', namespace: 'myns', class: 'something'},
# {type: 'gutter', namespace: 'myns', class: 'something-else'}
# ]
# ```
#
# You can remove both with:
#
# ```coffee
# editor.removeDecorationForMarker(marker, {namespace: 'myns'})
# ```
#
# marker - the {Marker} to detach from
# decorationPattern - the {Object} decoration type to filter by eg. `{type: 'gutter', class: 'linter-error'}`
#
# Returns nothing
removeDecorationForMarker: (marker, decorationPattern) ->
@displayBuffer.removeDecorationForMarker(marker, decorationPattern)
decorateMarker: (marker, decoration) ->
@displayBuffer.decorateMarker(marker, decoration)
decorationForId: (id) ->
@displayBuffer.decorationForId(id)
@@ -1251,9 +1220,9 @@ class Editor extends Model
addCursor: (marker) ->
cursor = new Cursor(editor: this, marker: marker)
@cursors.push(cursor)
@addDecorationForMarker(marker, type: 'gutter', class: 'cursor-line')
@addDecorationForMarker(marker, type: 'gutter', class: 'cursor-line-no-selection', onlyHead: true, onlyEmpty: true)
@addDecorationForMarker(marker, type: 'line', class: 'cursor-line', onlyEmpty: true)
@decorateMarker(marker, type: 'gutter', class: 'cursor-line')
@decorateMarker(marker, type: 'gutter', class: 'cursor-line-no-selection', onlyHead: true, onlyEmpty: true)
@decorateMarker(marker, type: 'line', class: 'cursor-line', onlyEmpty: true)
@emit 'cursor-added', cursor
cursor

View File

@@ -15,7 +15,7 @@ class Selection extends Model
constructor: ({@cursor, @marker, @editor, id}) ->
@assignId(id)
@cursor.selection = this
@decoration = @editor.addDecorationForMarker(@marker, type: 'highlight', class: 'selection')
@decoration = @editor.decorateMarker(@marker, type: 'highlight', class: 'selection')
@marker.on 'changed', => @screenRangeChanged()
@marker.on 'destroyed', =>