Add property filters to TextEditor::getDecorations methods

This makes it much easier to write tests that inspect the state of
decorations.
This commit is contained in:
Nathan Sobo 2014-12-10 18:40:39 -07:00
parent 453abf8b90
commit 7422f4378e
3 changed files with 45 additions and 19 deletions

View File

@ -1085,6 +1085,12 @@ describe "DisplayBuffer", ->
expect(oldProperties).toEqual decorationProperties
expect(newProperties).toEqual type: 'gutter', class: 'two', id: decoration.id
describe "::getDecorations(properties)", ->
it "returns decorations matching the given optional properties", ->
expect(displayBuffer.getDecorations()).toEqual [decoration]
expect(displayBuffer.getDecorations(class: 'two').length).toEqual 0
expect(displayBuffer.getDecorations(class: 'one').length).toEqual 1
describe "::setScrollTop", ->
beforeEach ->
displayBuffer.manageScrollPosition = true

View File

@ -883,23 +883,28 @@ class DisplayBuffer extends Model
decorationForId: (id) ->
@decorationsById[id]
getDecorations: ->
getDecorations: (propertyFilter) ->
allDecorations = []
for markerId, decorations of @decorationsByMarkerId
allDecorations = allDecorations.concat(decorations) if decorations?
if propertyFilter?
allDecorations = allDecorations.filter (decoration) ->
for key, value of propertyFilter
return false unless decoration.properties[key] is value
true
allDecorations
getLineDecorations: ->
@getDecorations().filter (decoration) -> decoration.isType('line')
getLineDecorations: (propertyFilter) ->
@getDecorations(propertyFilter).filter (decoration) -> decoration.isType('line')
getGutterDecorations: ->
@getDecorations().filter (decoration) -> decoration.isType('gutter')
getGutterDecorations: (propertyFilter) ->
@getDecorations(propertyFilter).filter (decoration) -> decoration.isType('gutter')
getHighlightDecorations: ->
@getDecorations().filter (decoration) -> decoration.isType('highlight')
getHighlightDecorations: (propertyFilter) ->
@getDecorations(propertyFilter).filter (decoration) -> decoration.isType('highlight')
getOverlayDecorations: ->
@getDecorations().filter (decoration) -> decoration.isType('overlay')
getOverlayDecorations: (propertyFilter) ->
@getDecorations(propertyFilter).filter (decoration) -> decoration.isType('overlay')
decorationsForScreenRowRange: (startScreenRow, endScreenRow) ->
decorationsByMarkerId = {}

View File

@ -1310,33 +1310,48 @@ class TextEditor extends Model
# Extended: Get all decorations.
#
# * `propertyFilter` (optional) An {Object} containing key value pairs that
# the returned decorations' properties must match.
#
# Returns an {Array} of {Decoration}s.
getDecorations: ->
@displayBuffer.getDecorations()
getDecorations: (propertyFilter) ->
@displayBuffer.getDecorations(propertyFilter)
# Extended: Get all decorations of type 'line'.
#
# * `propertyFilter` (optional) An {Object} containing key value pairs that
# the returned decorations' properties must match.
#
# Returns an {Array} of {Decoration}s.
getLineDecorations: ->
@displayBuffer.getLineDecorations()
getLineDecorations: (propertyFilter) ->
@displayBuffer.getLineDecorations(propertyFilter)
# Extended: Get all decorations of type 'gutter'.
#
# * `propertyFilter` (optional) An {Object} containing key value pairs that
# the returned decorations' properties must match.
#
# Returns an {Array} of {Decoration}s.
getGutterDecorations: ->
@displayBuffer.getGutterDecorations()
getGutterDecorations: (propertyFilter) ->
@displayBuffer.getGutterDecorations(propertyFilter)
# Extended: Get all decorations of type 'highlight'.
#
# * `propertyFilter` (optional) An {Object} containing key value pairs that
# the returned decorations' properties must match.
#
# Returns an {Array} of {Decoration}s.
getHighlightDecorations: ->
@displayBuffer.getHighlightDecorations()
getHighlightDecorations: (propertyFilter) ->
@displayBuffer.getHighlightDecorations(propertyFilter)
# Extended: Get all decorations of type 'overlay'.
#
# * `propertyFilter` (optional) An {Object} containing key value pairs that
# the returned decorations' properties must match.
#
# Returns an {Array} of {Decoration}s.
getOverlayDecorations: ->
@displayBuffer.getOverlayDecorations()
getOverlayDecorations: (propertyFilter) ->
@displayBuffer.getOverlayDecorations(propertyFilter)
decorationForId: (id) ->
@displayBuffer.decorationForId(id)