From b361e1719cdac69552fbc746746f99f735a65392 Mon Sep 17 00:00:00 2001 From: Jess Lin Date: Mon, 2 Feb 2015 09:57:28 -0800 Subject: [PATCH] [Gutter] Add decorateMarker method to Gutter model --- spec/gutter-container-spec.coffee | 9 ++++++--- spec/text-editor-spec.coffee | 19 +++++++++++++++++++ src/gutter-container.coffee | 14 +++++++++++++- src/gutter.coffee | 8 ++++++++ src/text-editor.coffee | 2 +- 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/spec/gutter-container-spec.coffee b/spec/gutter-container-spec.coffee index 299ae7097..03c9ebc10 100644 --- a/spec/gutter-container-spec.coffee +++ b/spec/gutter-container-spec.coffee @@ -3,8 +3,10 @@ GutterContainer = require '../src/gutter-container' describe 'GutterContainer', -> gutterContainer = null + fakeTextEditor = {} + beforeEach -> - gutterContainer = new GutterContainer + gutterContainer = new GutterContainer fakeTextEditor describe 'when initialized', -> it 'it has no gutters', -> @@ -31,7 +33,7 @@ describe 'GutterContainer', -> removedGutters = null beforeEach -> - gutterContainer = new GutterContainer + gutterContainer = new GutterContainer fakeTextEditor removedGutters = [] gutterContainer.onDidRemoveGutter (gutterName) -> removedGutters.push gutterName @@ -44,6 +46,7 @@ describe 'GutterContainer', -> expect(removedGutters).toEqual [gutter.name] it 'throws an error if the gutter is not within this GutterContainer', -> - otherGutterContainer = new GutterContainer + fakeOtherTextEditor = {} + otherGutterContainer = new GutterContainer fakeOtherTextEditor gutter = new Gutter 'gutter-name', otherGutterContainer expect(gutterContainer.removeGutter.bind(null, gutter)).toThrow() diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index fec384cb1..8f187d189 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -4277,3 +4277,22 @@ describe "TextEditor", -> expect(lineNumberDecoration.getProperties().gutterName).toBe 'line-number' expect(customGutterDecoration.getProperties().type).toBe 'gutter' expect(customGutterDecoration.getProperties().gutterName).toBe 'custom' + + it 'reflects an added decoration when one of its custom gutters is decorated.', -> + gutter = editor.addGutter {'name': 'custom-gutter'} + decoration = gutter.decorateMarker marker, {class: 'custom-class'} + gutterDecorations = editor.getDecorations + type: 'gutter' + gutterName: 'custom-gutter' + class: 'custom-class' + expect(gutterDecorations.length).toBe 1 + expect(gutterDecorations[0]).toBe decoration + + it 'reflects an added decoration when its line-number gutter is decorated.', -> + decoration = editor.gutterWithName('line-number').decorateMarker marker, {class: 'test-class'} + gutterDecorations = editor.getDecorations + type: 'line-number' + gutterName: 'line-number' + class: 'test-class' + expect(gutterDecorations.length).toBe 1 + expect(gutterDecorations[0]).toBe decoration diff --git a/src/gutter-container.coffee b/src/gutter-container.coffee index 06d85a628..6d62da27b 100644 --- a/src/gutter-container.coffee +++ b/src/gutter-container.coffee @@ -7,8 +7,11 @@ Gutter = require './gutter' module.exports = class GutterContainer Subscriber.includeInto(this) - constructor: -> + + # * `textEditor` The {TextEditor} to which this {GutterContainer} belongs. + constructor: (textEditor) -> @gutters = [] + @textEditor = textEditor @emitter = new Emitter destroy: -> @@ -76,3 +79,12 @@ class GutterContainer else throw new Error 'The given gutter cannot be removed because it is not ' + 'within this GutterContainer.' + + # The public interface is Gutter::decorateMarker or TextEditor::decorateMarker. + addGutterDecoration: (gutter, marker, options) -> + if gutter.name is 'line-number' + options.type = 'line-number' + else + options.type = 'gutter' + options.gutterName = gutter.name + @textEditor.decorateMarker marker, options diff --git a/src/gutter.coffee b/src/gutter.coffee index 9e002674b..f59fa7acd 100644 --- a/src/gutter.coffee +++ b/src/gutter.coffee @@ -43,6 +43,14 @@ class Gutter isVisible: -> @visible + # * `marker` (required) A Marker object. + # * `options` (optional) An object with the following fields: + # * `class` (optional) + # * `item` (optional) A model {Object} with a corresponding view registered, + # or an {HTMLElement}. + decorateMarker: (marker, options) -> + @gutterContainer.addGutterDecoration this, marker, options + # Calls your `callback` when the {Gutter}'s' visibility changes. # # * `callback` {Function} diff --git a/src/text-editor.coffee b/src/text-editor.coffee index 997ad6212..f27e7f4a0 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -114,7 +114,7 @@ class TextEditor extends Model @emit 'scroll-left-changed', scrollLeft if includeDeprecatedAPIs @emitter.emit 'did-change-scroll-left', scrollLeft - @gutterContainer = new GutterContainer + @gutterContainer = new GutterContainer this @lineNumberGutter = @gutterContainer.addGutter name: 'line-number' priority: 0