diff --git a/spec/display-buffer-spec.coffee b/spec/display-buffer-spec.coffee index b5858007e..f37c9ebf0 100644 --- a/spec/display-buffer-spec.coffee +++ b/spec/display-buffer-spec.coffee @@ -1208,7 +1208,7 @@ describe "DisplayBuffer", -> {oldProperties, newProperties} = updatedSpy.mostRecentCall.args[0] expect(oldProperties).toEqual decorationProperties - expect(newProperties).toEqual type: 'line-number', class: 'two', id: decoration.id + expect(newProperties).toEqual type: 'line-number', gutterName: 'line-number', class: 'two', id: decoration.id describe "::getDecorations(properties)", -> it "returns decorations matching the given optional properties", -> @@ -1367,3 +1367,26 @@ describe "DisplayBuffer", -> displayBuffer.setScrollTop(60) expect(displayBuffer.getVisibleRowRange()).toEqual [0, 13] + + describe "::decorateMarker", -> + describe "when decorating gutters", -> + [marker] = [] + + beforeEach -> + marker = displayBuffer.markBufferRange([[1, 0], [1, 0]]); + + it "creates a decoration that is both of 'line-number' and 'gutter' type when called with the 'line-number' type", -> + decorationProperties = {type: 'line-number', class: 'one'} + decoration = displayBuffer.decorateMarker(marker, decorationProperties) + expect(decoration.isType('line-number')).toBe true + expect(decoration.isType('gutter')).toBe true + expect(decoration.getProperties().gutterName).toBe 'line-number' + expect(decoration.getProperties().class).toBe 'one' + + it "creates a decoration that is only of 'gutter' type if called with the 'gutter' type and a 'gutterName'", -> + decorationProperties = {type: 'gutter', gutterName:'test-gutter', class: 'one'} + decoration = displayBuffer.decorateMarker(marker, decorationProperties) + expect(decoration.isType('gutter')).toBe true + expect(decoration.isType('line-number')).toBe false + expect(decoration.getProperties().gutterName).toBe 'test-gutter' + expect(decoration.getProperties().class).toBe 'one' diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 890eb923f..fec384cb1 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -4263,3 +4263,17 @@ describe "TextEditor", -> it "does not allow a custom gutter with the 'line-number' name.", -> expect(editor.addGutter.bind(editor, {name: 'line-number'})).toThrow() + + describe '::decorateMarker', -> + [marker] = [] + + beforeEach -> + marker = editor.markBufferRange([[1, 0], [1, 0]]) + + it "casts 'gutter' type to 'line-number' unless a gutter name is specified.", -> + lineNumberDecoration = editor.decorateMarker(marker, {type: 'gutter'}) + customGutterDecoration = editor.decorateMarker(marker, {type: 'gutter', gutterName: 'custom'}) + expect(lineNumberDecoration.getProperties().type).toBe 'line-number' + expect(lineNumberDecoration.getProperties().gutterName).toBe 'line-number' + expect(customGutterDecoration.getProperties().type).toBe 'gutter' + expect(customGutterDecoration.getProperties().gutterName).toBe 'custom' diff --git a/src/decoration.coffee b/src/decoration.coffee index 48b26e81f..ca239421e 100644 --- a/src/decoration.coffee +++ b/src/decoration.coffee @@ -5,6 +5,13 @@ Grim = require 'grim' idCounter = 0 nextId = -> idCounter++ +# Applies changes to a decorationsParam {Object} to make it possible to +# differentiate decorations on custom gutters versus the line-number gutter. +translateDecorationParamsOldToNew = (decorationParams) -> + if decorationParams.type is 'line-number' + decorationParams.gutterName = 'line-number' + decorationParams + # Essential: Represents a decoration that follows a {Marker}. A decoration is # basically a visual representation of a marker. It allows you to add CSS # classes to line numbers in the gutter, lines, and add selection-line regions @@ -38,19 +45,29 @@ class Decoration # type matches any in the array. # # Returns {Boolean} + # Note: 'line-number' is a special subtype of the 'gutter' type. I.e., a + # 'line-number' is a 'gutter', but a 'gutter' is not a 'line-number'. @isType: (decorationProperties, type) -> + # 'line-number' is a special case of 'gutter'. if _.isArray(decorationProperties.type) - type in decorationProperties.type + return true if type in decorationProperties.type + if type is 'gutter' + return true if 'line-number' in decorationProperties.type + return false else - type is decorationProperties.type + if type is 'gutter' + return true if decorationProperties.type in ['gutter', 'line-number'] + else + type is decorationProperties.type ### Section: Construction and Destruction ### - constructor: (@marker, @displayBuffer, @properties) -> + constructor: (@marker, @displayBuffer, properties) -> @emitter = new Emitter @id = nextId() + @setProperties properties @properties.id = @id @flashQueue = null @destroyed = false @@ -134,7 +151,7 @@ class Decoration setProperties: (newProperties) -> return if @destroyed oldProperties = @properties - @properties = newProperties + @properties = translateDecorationParamsOldToNew(newProperties) @properties.id = @id @emit 'updated', {oldParams: oldProperties, newParams: newProperties} if Grim.includeDeprecatedAPIs @emitter.emit 'did-change-properties', {oldProperties, newProperties} diff --git a/src/text-editor.coffee b/src/text-editor.coffee index 0498ebe22..997ad6212 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -1275,10 +1275,12 @@ class TextEditor extends Model # * `position` (optional) Only applicable to decorations of type `overlay`, # controls where the overlay view is positioned relative to the marker. # Values can be `'head'` (the default), or `'tail'`. + # * `gutterName` (optional) Only applicable to the `gutter` type. If provided, + # the decoration will be applied to the gutter with the specified name. # # Returns a {Decoration} object decorateMarker: (marker, decorationParams) -> - if includeDeprecatedAPIs and decorationParams.type is 'gutter' + if decorationParams.type is 'gutter' && !decorationParams.gutterName deprecate("Decorations of `type: 'gutter'` have been renamed to `type: 'line-number'`.") decorationParams.type = 'line-number' @displayBuffer.decorateMarker(marker, decorationParams)