From 24f8537990f172b10b1b568cf9b799286809d9d0 Mon Sep 17 00:00:00 2001 From: Winston Liu <50Wliu@users.noreply.github.com> Date: Sat, 2 Mar 2019 21:53:36 -0500 Subject: [PATCH 1/8] Dispose of all subscriptions on deactivation --- lib/bracket-matcher-view.js | 5 ++--- lib/bracket-matcher.js | 4 ++-- lib/main.js | 27 ++++++++++++++++++++------- lib/match-manager.js | 7 +++---- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/bracket-matcher-view.js b/lib/bracket-matcher-view.js index 75e2083..cb1deae 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 @@ -59,13 +58,13 @@ class BracketMatcherView { this.selectMatchingBrackets() ), - this.editor.onDidDestroy(this.destroy) + this.editor.onDidDestroy(() => this.dispose()) ) this.updateMatch() } - destroy () { + dispose () { this.subscriptions.dispose() } diff --git a/lib/bracket-matcher.js b/lib/bracket-matcher.js index c6e84ee..6c0360f 100644 --- a/lib/bracket-matcher.js +++ b/lib/bracket-matcher.js @@ -23,7 +23,7 @@ class BracketMatcher { if (!this.removeBrackets()) event.abortKeyBinding() }), - this.editor.onDidDestroy(() => this.unsubscribe()) + this.editor.onDidDestroy(() => this.dispose()) ) } @@ -270,7 +270,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..f30e7ee 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,20 +1,33 @@ +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() + this.watchedEditors = new WeakMap() atom.workspace.observeTextEditors(editor => { - if (watchedEditors.has(editor)) return + if (this.watchedEditors.has(editor)) return 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.add(editor, subscriptions) + editor.onDidDestroy(() => { + this.watchedEditors.get(editor).dispose() + this.watchedEditors.delete(editor) + }) }) + }, + + deactivate () { + 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..d4113b0 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,13 @@ 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.editor.onDidDestroy(() => this.dispose()) ) this.changeBracketsMode = false } - destroy () { + dispose () { this.subscriptions.dispose() } } From 66a92690ab9d40575a5da7304362325372835133 Mon Sep 17 00:00:00 2001 From: Winston Liu <50Wliu@users.noreply.github.com> Date: Sun, 3 Mar 2019 02:45:51 -0500 Subject: [PATCH 2/8] Initial pass at proper package deactivation Highlights do not go away after deactivating --- lib/main.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/main.js b/lib/main.js index f30e7ee..ce6a939 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,4 +1,4 @@ -const CompositeDisposable = require('atom') +const {CompositeDisposable} = require('atom') const MatchManager = require('./match-manager') const BracketMatcherView = require('./bracket-matcher-view') @@ -15,19 +15,23 @@ module.exports = { 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.add(editor, subscriptions) + const subscriptions = new CompositeDisposable(matchManager, bracketMatcherView, bracketMatcher) + this.watchedEditors.set(editor, subscriptions) editor.onDidDestroy(() => { - this.watchedEditors.get(editor).dispose() - this.watchedEditors.delete(editor) + if (this.watchedEditors.has(editor)) { + this.watchedEditors.get(editor).dispose() + this.watchedEditors.delete(editor) + } }) }) }, deactivate () { for (const editor of atom.workspace.getTextEditors()) { - this.watchedEditors.get(editor).dispose() - this.watchedEditors.delete(editor) + if (this.watchedEditors.has(editor)) { + this.watchedEditors.get(editor).dispose() + this.watchedEditors.delete(editor) + } } } } From 9dd16d45e9c2fb95a4bb57d259f82b42d9f3ca32 Mon Sep 17 00:00:00 2001 From: Winston Liu <50Wliu@users.noreply.github.com> Date: Mon, 4 Mar 2019 01:19:20 -0500 Subject: [PATCH 3/8] Dispose of editor-watching subscriptions on deactivate --- lib/main.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/main.js b/lib/main.js index ce6a939..5869589 100644 --- a/lib/main.js +++ b/lib/main.js @@ -7,8 +7,9 @@ const BracketMatcher = require('./bracket-matcher') module.exports = { activate () { this.watchedEditors = new WeakMap() + this.subscriptions = new CompositeDisposable() - atom.workspace.observeTextEditors(editor => { + this.subscriptions.add(atom.workspace.observeTextEditors(editor => { if (this.watchedEditors.has(editor)) return const editorElement = atom.views.getView(editor) @@ -17,16 +18,15 @@ module.exports = { const bracketMatcher = new BracketMatcher(editor, editorElement, matchManager) const subscriptions = new CompositeDisposable(matchManager, bracketMatcherView, bracketMatcher) this.watchedEditors.set(editor, subscriptions) - editor.onDidDestroy(() => { - if (this.watchedEditors.has(editor)) { - this.watchedEditors.get(editor).dispose() - this.watchedEditors.delete(editor) - } - }) - }) + 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()) { if (this.watchedEditors.has(editor)) { this.watchedEditors.get(editor).dispose() From 1faa6ea8f530f6fb6b51727b5897f7885b14faf7 Mon Sep 17 00:00:00 2001 From: Winston Liu <50Wliu@users.noreply.github.com> Date: Mon, 4 Mar 2019 01:22:52 -0500 Subject: [PATCH 4/8] Dispose of pair highlighting on deactivation --- lib/bracket-matcher-view.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/bracket-matcher-view.js b/lib/bracket-matcher-view.js index cb1deae..e2e692f 100644 --- a/lib/bracket-matcher-view.js +++ b/lib/bracket-matcher-view.js @@ -66,6 +66,10 @@ class BracketMatcherView { dispose () { this.subscriptions.dispose() + if (this.pairHighlighted) { + this.editor.destroyMarker(this.startMarker.id) + this.editor.destroyMarker(this.endMarker.id) + } } updateMatch () { From 68b39855d957c21497edc8181c248c9357a9e040 Mon Sep 17 00:00:00 2001 From: Winston Liu <50Wliu@users.noreply.github.com> Date: Mon, 4 Mar 2019 01:24:20 -0500 Subject: [PATCH 5/8] :fire: un-needed conditional --- lib/main.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/main.js b/lib/main.js index 5869589..c20b35f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -28,10 +28,8 @@ module.exports = { deactivate () { this.subscriptions.dispose() for (const editor of atom.workspace.getTextEditors()) { - if (this.watchedEditors.has(editor)) { - this.watchedEditors.get(editor).dispose() - this.watchedEditors.delete(editor) - } + this.watchedEditors.get(editor).dispose() + this.watchedEditors.delete(editor) } } } From bf3214056a67f78af29cb74dee4b96c1d52b3f27 Mon Sep 17 00:00:00 2001 From: Winston Liu <50Wliu@users.noreply.github.com> Date: Mon, 4 Mar 2019 01:27:54 -0500 Subject: [PATCH 6/8] Disposal is now handled in main --- lib/bracket-matcher-view.js | 4 +--- lib/bracket-matcher.js | 4 +--- lib/match-manager.js | 1 - 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/bracket-matcher-view.js b/lib/bracket-matcher-view.js index e2e692f..cb3dfed 100644 --- a/lib/bracket-matcher-view.js +++ b/lib/bracket-matcher-view.js @@ -56,9 +56,7 @@ class BracketMatcherView { atom.commands.add(editorElement, 'bracket-matcher:select-matching-brackets', () => this.selectMatchingBrackets() - ), - - this.editor.onDidDestroy(() => this.dispose()) + ) ) this.updateMatch() diff --git a/lib/bracket-matcher.js b/lib/bracket-matcher.js index 6c0360f..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.dispose()) + }) ) } diff --git a/lib/match-manager.js b/lib/match-manager.js index d4113b0..1fbf87c 100644 --- a/lib/match-manager.js +++ b/lib/match-manager.js @@ -47,7 +47,6 @@ 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.dispose()) ) this.changeBracketsMode = false From e4083c23a910c6a57d5a22da13ca6b3ef664b7c1 Mon Sep 17 00:00:00 2001 From: Winston Liu <50Wliu@users.noreply.github.com> Date: Tue, 5 Mar 2019 19:19:07 -0500 Subject: [PATCH 7/8] :fire: unnecessary has check --- lib/main.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index c20b35f..710501a 100644 --- a/lib/main.js +++ b/lib/main.js @@ -10,8 +10,6 @@ module.exports = { this.subscriptions = new CompositeDisposable() this.subscriptions.add(atom.workspace.observeTextEditors(editor => { - if (this.watchedEditors.has(editor)) return - const editorElement = atom.views.getView(editor) const matchManager = new MatchManager(editor) const bracketMatcherView = new BracketMatcherView(editor, editorElement, matchManager) From 12dd49f6f7447f4570121e60f6b1b87e0b9c7457 Mon Sep 17 00:00:00 2001 From: Winston Liu <50Wliu@users.noreply.github.com> Date: Tue, 5 Mar 2019 19:19:51 -0500 Subject: [PATCH 8/8] :art: --- lib/main.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/main.js b/lib/main.js index 710501a..0d52a09 100644 --- a/lib/main.js +++ b/lib/main.js @@ -11,11 +11,14 @@ module.exports = { this.subscriptions.add(atom.workspace.observeTextEditors(editor => { const editorElement = atom.views.getView(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)