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

Deactivate cleanly #384

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/bracket-matcher-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () {
Expand Down
6 changes: 2 additions & 4 deletions lib/bracket-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
)
}

Expand Down Expand Up @@ -270,7 +268,7 @@ class BracketMatcher {
return this.matchManager.pairedCharacters[firstCharacter] === lastCharacter
}

unsubscribe () {
dispose () {
this.subscriptions.dispose()
}

Expand Down
36 changes: 26 additions & 10 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
6 changes: 2 additions & 4 deletions lib/match-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()
}
}