diff --git a/lib/bracket-matcher-view.js b/lib/bracket-matcher-view.js index 75e2083..cb3dfed 100644 --- a/lib/bracket-matcher-view.js +++ b/lib/bracket-matcher-view.js @@ -13,7 +13,6 @@ const MAX_ROWS_TO_SCAN_BACKWARD_TRAVERSAL = Object.freeze(Point(-MAX_ROWS_TO_SCA module.exports = class BracketMatcherView { constructor (editor, editorElement, matchManager) { - this.destroy = this.destroy.bind(this) this.updateMatch = this.updateMatch.bind(this) this.editor = editor this.matchManager = matchManager @@ -57,16 +56,18 @@ class BracketMatcherView { atom.commands.add(editorElement, 'bracket-matcher:select-matching-brackets', () => this.selectMatchingBrackets() - ), - - this.editor.onDidDestroy(this.destroy) + ) ) this.updateMatch() } - destroy () { + dispose () { this.subscriptions.dispose() + if (this.pairHighlighted) { + this.editor.destroyMarker(this.startMarker.id) + this.editor.destroyMarker(this.endMarker.id) + } } updateMatch () { diff --git a/lib/bracket-matcher.js b/lib/bracket-matcher.js index c6e84ee..92c452c 100644 --- a/lib/bracket-matcher.js +++ b/lib/bracket-matcher.js @@ -21,9 +21,7 @@ class BracketMatcher { this.subscriptions.add( atom.commands.add(editorElement, 'bracket-matcher:remove-brackets-from-selection', event => { if (!this.removeBrackets()) event.abortKeyBinding() - }), - - this.editor.onDidDestroy(() => this.unsubscribe()) + }) ) } @@ -270,7 +268,7 @@ class BracketMatcher { return this.matchManager.pairedCharacters[firstCharacter] === lastCharacter } - unsubscribe () { + dispose () { this.subscriptions.dispose() } diff --git a/lib/main.js b/lib/main.js index 91f9ea9..0d52a09 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,20 +1,36 @@ +const {CompositeDisposable} = require('atom') + const MatchManager = require('./match-manager') const BracketMatcherView = require('./bracket-matcher-view') const BracketMatcher = require('./bracket-matcher') module.exports = { activate () { - const watchedEditors = new WeakSet() - - atom.workspace.observeTextEditors(editor => { - if (watchedEditors.has(editor)) return + this.watchedEditors = new WeakMap() + this.subscriptions = new CompositeDisposable() + this.subscriptions.add(atom.workspace.observeTextEditors(editor => { const editorElement = atom.views.getView(editor) - const matchManager = new MatchManager(editor, editorElement) - new BracketMatcherView(editor, editorElement, matchManager) - new BracketMatcher(editor, editorElement, matchManager) - watchedEditors.add(editor) - editor.onDidDestroy(() => watchedEditors.delete(editor)) - }) + + const matchManager = new MatchManager(editor) + const bracketMatcherView = new BracketMatcherView(editor, editorElement, matchManager) + const bracketMatcher = new BracketMatcher(editor, editorElement, matchManager) + + const subscriptions = new CompositeDisposable(matchManager, bracketMatcherView, bracketMatcher) + this.watchedEditors.set(editor, subscriptions) + + this.subscriptions.add(editor.onDidDestroy(() => { + this.watchedEditors.get(editor).dispose() + this.watchedEditors.delete(editor) + })) + })) + }, + + deactivate () { + this.subscriptions.dispose() + for (const editor of atom.workspace.getTextEditors()) { + this.watchedEditors.get(editor).dispose() + this.watchedEditors.delete(editor) + } } } diff --git a/lib/match-manager.js b/lib/match-manager.js index a052c1d..1fbf87c 100644 --- a/lib/match-manager.js +++ b/lib/match-manager.js @@ -36,8 +36,7 @@ class MatchManager { return atom.config.get(key, {scope: this.editor.getRootScopeDescriptor()}) } - constructor (editor, editorElement) { - this.destroy = this.destroy.bind(this) + constructor (editor) { this.editor = editor this.subscriptions = new CompositeDisposable() @@ -48,13 +47,12 @@ class MatchManager { this.subscriptions.add( atom.config.observe('bracket-matcher.autocompleteCharacters', {scope}, () => this.updateConfig()), atom.config.observe('bracket-matcher.pairsWithExtraNewline', {scope}, () => this.updateConfig()), - this.editor.onDidDestroy(this.destroy) ) this.changeBracketsMode = false } - destroy () { + dispose () { this.subscriptions.dispose() } }