Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Add Selection::onDidChangeRange and ::onDidDestroy and deprecate ::on
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Sobo committed Sep 4, 2014
1 parent 3aabe90 commit 79a0626
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion spec/editor-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1889,7 +1889,7 @@ describe "Editor", ->
beforeEach ->
selection = editor.getLastSelection()
changeScreenRangeHandler = jasmine.createSpy('changeScreenRangeHandler')
selection.on 'screen-range-changed', changeScreenRangeHandler
selection.onDidChangeRange changeScreenRangeHandler

describe "when the cursor is on the middle of the line", ->
it "removes the character before the cursor", ->
Expand Down
4 changes: 2 additions & 2 deletions spec/selection-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ describe "Selection", ->
expect(selection.isReversed()).toBeFalsy()

describe "when only the selection's tail is moved (regression)", ->
it "emits the 'screen-range-changed' event", ->
it "notifies ::onDidChangeRange observers", ->
selection.setBufferRange([[2, 0], [2, 10]], reversed: true)
changeScreenRangeHandler = jasmine.createSpy('changeScreenRangeHandler')
selection.on 'screen-range-changed', changeScreenRangeHandler
selection.onDidChangeRange changeScreenRangeHandler

buffer.insert([2, 5], 'abc')
expect(changeScreenRangeHandler).toHaveBeenCalled()
Expand Down
4 changes: 2 additions & 2 deletions src/editor-component.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,8 @@ EditorComponent = React.createClass
onSelectionAdded: (selection) ->
{editor} = @props

@subscribe selection, 'screen-range-changed', => @onSelectionChanged(selection)
@subscribe selection, 'destroyed', =>
@subscribe selection.onDidChangeRange => @onSelectionChanged(selection)
@subscribe selection.onDidDestroy =>
@onSelectionChanged(selection)
@unsubscribe(selection)

Expand Down
32 changes: 27 additions & 5 deletions src/selection.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{Point, Range} = require 'text-buffer'
{Model} = require 'theorist'
{pick} = require 'underscore-plus'
{Emitter} = require 'event-kit'
Grim = require 'grim'

# Extended: Represents a selection in the {Editor}.
#
Expand All @@ -26,15 +28,35 @@ class Selection extends Model
needsAutoscroll: null

constructor: ({@cursor, @marker, @editor, id}) ->
@emitter = new Emitter

@assignId(id)
@cursor.selection = this
@decoration = @editor.decorateMarker(@marker, type: 'highlight', class: 'selection')

@marker.onDidChange => @screenRangeChanged()
@marker.onDidDestroy =>
@destroyed = true
@editor.removeSelection(this)
@emit 'destroyed' unless @editor.isDestroyed()
unless @editor.isDestroyed()
@destroyed = true
@editor.removeSelection(this)
@emit 'destroyed'
@emitter.emit 'did-destroy'
@emitter.dispose()

onDidChangeRange: (callback) ->
@emitter.on 'did-change-range', callback

onDidDestroy: (callback) ->
@emitter.on 'did-destroy', callback

on: (eventName) ->
switch eventName
when 'screen-range-changed'
Grim.deprecate("Use Selection::onDidChangeRange instead. Call ::getScreenRange() yourself in your callback if you need the range.")
when 'destroyed'
Grim.deprecate("Use Selection::onDidDestroy instead.")

super

destroy: ->
@marker.destroy()
Expand Down Expand Up @@ -665,6 +687,6 @@ class Selection extends Model
@getBufferRange().compare(otherSelection.getBufferRange())

screenRangeChanged: ->
screenRange = @getScreenRange()
@emit 'screen-range-changed', screenRange
@emit 'screen-range-changed', @getScreenRange()
@emitter.emit 'did-change-range'
@editor.selectionScreenRangeChanged(this)

0 comments on commit 79a0626

Please sign in to comment.