   constructor: (editorView) ->
     {@editor, @gutter} = editorView
 
-    @marker = undefined
+    @decoration = undefined
 
-    @subscribe editorView, 'editor:display-updated', => @updateGutter()
     @subscribe @editor, 'destroyed', => @unsubscribe()
     @subscribeToCommand editorView, 'mark:toggle', => @toggle()
     @subscribeToCommand editorView, 'mark:clear-mark', => @clearMark()

    @subscribeToCommand editorView, 'mark:goto-mark', => @gotoMark()
    @subscribeToCommand editorView, 'mark:swap', => @swapWithMark()

  updateGutter: ->
    if @marker?
      @gutter.addClassToLine(@marker.getHeadBufferPosition().row, 'marked')

  createMark: (point) ->
    @marker = @editor.markBufferPosition(point)
    @gutter.addClassToLine(point.row, 'marked')

    @subscribe @marker, 'changed', ({ isValid }) =>
      if not isValid
        @unsubscribe(@marker)
        @marker.destroy()
        @marker = undefined

  clearMark: () ->
    if @marker
      @gutter.removeClassFromLine(@marker.getHeadBufferPosition().row, 'marked')
      @marker.destroy()
      @marker = undefined

  toggle: ->
    if not @gutter.isVisible
      return

    markPoint = @marker?.getHeadBufferPosition()
    cursorPoint = @editor.getCursorBufferPosition()

    if markPoint?

      @createMark(cursorPoint)

  selectToMark: ->
    if @decoration
      markPoint   = @decoration?.getMarker().getHeadBufferPosition()
      cursorPoint = @editor.getCursorBufferPosition()
      if not cursorPoint.isEqual(markPoint)
        @editor.setSelectedBufferRange([markPoint, cursorPoint])

  gotoMark: ->
    if @decoration
      @editor.setCursorBufferPosition(@decoration.getMarker().getHeadBufferPosition())

  swapWithMark: ->
    if @decoration
      cursorPoint = @editor.getCursorBufferPosition()
      markPoint   = @decoration.getMarker().getHeadBufferPosition()
      if not cursorPoint.isEqual(markPoint)
        @decoration.getMarker().setHeadBufferPosition(cursorPoint)
        @editor.setCursorBufferPosition(markPoint)
