Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Added AutocompleteView#dispose to clean up event handlers after closi…
Browse files Browse the repository at this point in the history
…ng tabs / panes. Closes #21
  • Loading branch information
saschagehlich committed Mar 19, 2014
1 parent 638998d commit 9f41b2f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
51 changes: 32 additions & 19 deletions lib/autocomplete-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,14 @@ class AutocompleteView extends SimpleSelectListView

# Listen to `contents-modified` event when live completion is disabled
unless atom.config.get('autocomplete-plus.liveCompletion')
@editor.on 'contents-modified', => @contentsModified()
@editor.on 'contents-modified', @contentsModified

# Is this the event for switching tabs? Dunno...
@editor.on 'title-changed-subscription-removed', =>
@cancel()
@editor.on 'title-changed-subscription-removed', @cancel

# Close the overlay when the cursor moved without
# changing any text
@editor.on 'cursor-moved', (data) =>
if not data.textChanged and @active
@cancel()
@editor.on 'cursor-moved', @cursorMoved

###
* Return false so that the events don't bubble up to the editor
Expand Down Expand Up @@ -158,7 +155,7 @@ class AutocompleteView extends SimpleSelectListView
* Clears the list, sets back the cursor, focuses the editor and
* detaches the list DOM element
###
cancel: ->
cancel: =>
@active = false

@list.empty()
Expand All @@ -167,7 +164,7 @@ class AutocompleteView extends SimpleSelectListView

@detach()

contentsModified: ->
contentsModified: =>
if @active
@detach()
@list.empty()
Expand All @@ -191,6 +188,23 @@ class AutocompleteView extends SimpleSelectListView

@setActive()

cursorMoved: (data) =>
if not data.textChanged and @active
@cancel()

onSaved: =>
@buildWordList()
@cancel()

onChanged: (e) =>
if e.newText in ["\n", " "]
@addLastWordToList e.newText is "\n"

if e.newText.length is 1
@contentsModified()
else
@cancel()

findMatchesForWord: (prefix) ->
p = new Perf "Finding matches for '#{prefix}'", {@debug}
p.start()
Expand Down Expand Up @@ -308,19 +322,10 @@ class AutocompleteView extends SimpleSelectListView
###
setCurrentBuffer: (@currentBuffer) ->
@buildWordList()
@currentBuffer.on "saved", =>
@buildWordList()
@cancel()
@currentBuffer.on "saved", @onSaved

if atom.config.get('autocomplete-plus.liveCompletion')
@currentBuffer.on "changed", (e) =>
if e.newText in ["\n", " "]
@addLastWordToList e.newText is "\n"

if e.newText.length is 1
@contentsModified()
else
@cancel()
@currentBuffer.on "changed", @onChanged

###
* Adds the last typed word to the wordList
Expand All @@ -338,3 +343,11 @@ class AutocompleteView extends SimpleSelectListView
getFilterKey: -> 'word'

getModel: -> null

dispose: ->
@editor.off "contents-modified", @contentsModified
@currentBuffer?.off "changed", @onChanged
@currentBuffer?.off "saved", @onSaved
@editor.off "contents-modified", @contentsModified
@editor.off "title-changed-subscription-removed", @cancel
@editor.off "cursor-moved", @cursorMoved
1 change: 1 addition & 0 deletions lib/autocomplete.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports =
autocompleteView = new AutocompleteView(editor)
editor.on 'editor:will-be-removed', =>
autocompleteView.remove() unless autocompleteView.hasParent()
autocompleteView.dispose()
_.remove(@autocompleteViews, autocompleteView)
@autocompleteViews.push(autocompleteView)

Expand Down

0 comments on commit 9f41b2f

Please sign in to comment.