Beginning of decorations

This commit is contained in:
Ben Ogle
2014-06-03 18:05:30 -07:00
parent b23009a8f3
commit 77d269c6d9
4 changed files with 54 additions and 1 deletions

View File

@@ -1622,7 +1622,7 @@ describe "Editor", ->
editor.setCursorBufferPosition([9,2])
editor.insertNewline()
expect(editor.lineForBufferRow(10)).toBe ' };'
describe ".backspace()", ->
describe "when there is a single cursor", ->
changeScreenRangeHandler = null
@@ -3205,3 +3205,13 @@ describe "Editor", ->
editor.pageUp()
expect(editor.getScrollTop()).toBe 0
fdescribe "decorations", ->
it "can add decorations", ->
decoration = {type: 'gutter-class', class: 'one'}
editor.addDecorationForBufferRow(2, decoration)
editor.addDecorationForBufferRow(2, decoration)
decorations = editor.decorationsForBufferRow(2)
expect(decorations).toHaveLength 1
expect(decorations).toContain decoration

View File

@@ -43,6 +43,7 @@ class DisplayBuffer extends Model
@charWidthsByScope = {}
@markers = {}
@foldsByMarkerId = {}
@decorations = {}
@updateAllScreenLines()
@createFoldForMarker(marker) for marker in @buffer.findMarkers(@getFoldMarkerAttributes())
@subscribe @tokenizedBuffer, 'grammar-changed', (grammar) => @emit 'grammar-changed', grammar
@@ -718,6 +719,30 @@ class DisplayBuffer extends Model
rangeForAllLines: ->
new Range([0, 0], @clipScreenPosition([Infinity, Infinity]))
decorationsForBufferRow: (bufferRow) ->
@decorations[bufferRow] ? []
addDecorationForBufferRow: (bufferRow, decoration) ->
@decorations[bufferRow] ?= []
for current in @decorations[bufferRow]
return if _.isEqual(current, decoration)
@decorations[bufferRow].push(decoration)
@emit 'decoration-changed', {bufferRow, add: decoration}
removeDecorationForBufferRow: (bufferRow, decoration) ->
return unless @decorations[bufferRow]
removed = @findDecorationsForBufferRow(bufferRow, decoration)
@decorations[bufferRow] = _.without(@decorations, removed)
@emit 'decoration-changed', {bufferRow, remove: removed}
findDecorationsForBufferRow: (bufferRow, options) ->
return unless @decorations[bufferRow]
(dec for dec in @decorations[bufferRow] when _.isEqual(options, _.pick(decoration, _.keys(options))))
addGutterClassForMarker: (bufferRow) ->
removeGutterClassForMarker: (bufferRow) ->
# Retrieves a {DisplayBufferMarker} based on its id.
#
# id - A {Number} representing a marker id

View File

@@ -1057,6 +1057,15 @@ class Editor extends Model
selection.insertText(fn(text))
selection.setBufferRange(range)
decorationsForBufferRow: (bufferRow) ->
@displayBuffer.decorationsForBufferRow(bufferRow)
addDecorationForBufferRow: (bufferRow, decoration) ->
@displayBuffer.addDecorationForBufferRow(bufferRow, decoration)
removeDecorationForBufferRow: (bufferRow, decoration) ->
@displayBuffer.removeDecorationForBufferRow(bufferRow, decoration)
# Public: Get the {DisplayBufferMarker} for the given marker id.
getMarker: (id) ->
@displayBuffer.getMarker(id)

View File

@@ -27,6 +27,13 @@ GutterComponent = React.createClass
componentDidMount: ->
@appendDummyLineNumber()
@subscribeToEditor()
componentWillUnmount: ->
@unsubscribe()
subscribeToEditor: ->
@subscribe @props.editor, 'decoration-changed', @onDecorationChanged
# Only update the gutter if the visible row range has changed or if a
# non-zero-delta change to the screen lines has occurred within the current
@@ -168,3 +175,5 @@ GutterComponent = React.createClass
toggleClass: (node, klass, condition) ->
if condition then node.classList.add(klass) else node.classList.remove(klass)
onDecorationChanged: (change) ->
@forceUpdate()