From ebcfe6a00457f501034471e875346f7e2d439f52 Mon Sep 17 00:00:00 2001 From: Mike Goggin Date: Mon, 12 Dec 2022 13:05:33 -0600 Subject: [PATCH 1/2] Fix `onDidSave` event handling When the extension is activated on an empty workspace, `nova.workspace.activeTextEditor` is `undefined` which raises an unhandled exception. Instead, add an `onDidAddTextEditor` event handler to the workspace and add `onDidSave` to the new `TextEditor` instance. --- Scripts/main.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/Scripts/main.js b/Scripts/main.js index b708f14..68b15b0 100644 --- a/Scripts/main.js +++ b/Scripts/main.js @@ -35,20 +35,22 @@ exports.activate = function() { } if (SETTINGS.rubocop.autocorrectOnSave()) { - nova.workspace.activeTextEditor.onDidSave((editor) => { - const rubocop = new COMMANDS.Rubocop() - - switch (SETTINGS.rubocop.autocorrectOnSave()) { - case "Layout (recommended)": - rubocop.autocorrectLayout(editor.document) - break - case "Safe Cops": - rubocop.autocorrect(editor.document) - break - case "Safe & Unsafe Cops": - rubocop.autocorrectAll(editor.document) - break - } + nova.workspace.onDidAddTextEditor((editor) => { + editor.onDidSave((editor) => { + const rubocop = new COMMANDS.Rubocop() + + switch (SETTINGS.rubocop.autocorrectOnSave()) { + case "Layout (recommended)": + rubocop.autocorrectLayout(editor.document) + break + case "Safe Cops": + rubocop.autocorrect(editor.document) + break + case "Safe & Unsafe Cops": + rubocop.autocorrectAll(editor.document) + break + } + }) }) } } From eedfa1e1b9b14b3efece8415037bb3e5b760d174 Mon Sep 17 00:00:00 2001 From: Mike Goggin Date: Mon, 12 Dec 2022 13:15:22 -0600 Subject: [PATCH 2/2] Clean up event handlers on extension deactivation When the extension is deactivated (or reloaded), we dispose of registered event handlers. This fixes an issue where `onDidSave` would be called twice when a file was reopened and saved after the extension was reloaded. --- Scripts/main.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/Scripts/main.js b/Scripts/main.js index 68b15b0..44c9663 100644 --- a/Scripts/main.js +++ b/Scripts/main.js @@ -8,6 +8,8 @@ const { isRailsInProject, showNotification } = require("./helpers") const versionChecker = new VersionChecker() versionChecker.check() +const eventHandlers = [] + let langserver = null let sidebar = null @@ -35,8 +37,8 @@ exports.activate = function() { } if (SETTINGS.rubocop.autocorrectOnSave()) { - nova.workspace.onDidAddTextEditor((editor) => { - editor.onDidSave((editor) => { + const didAddTextEditorHandler = nova.workspace.onDidAddTextEditor((editor) => { + const didSaveHandler = editor.onDidSave((editor) => { const rubocop = new COMMANDS.Rubocop() switch (SETTINGS.rubocop.autocorrectOnSave()) { @@ -51,7 +53,17 @@ exports.activate = function() { break } }) + + eventHandlers.push(didSaveHandler) + + const didDestroyHandler = editor.onDidDestroy(() => { + didSaveHandler.dispose() + }) + + eventHandlers.push(didDestroyHandler) }) + + eventHandlers.push(didAddTextEditorHandler) } } @@ -67,6 +79,14 @@ exports.deactivate = function() { sidebar = null } + if (eventHandlers && eventHandlers.length > 0) { + console.log("Removing bound event handlers.") + + while (handler = eventHandlers.pop()) { + handler.dispose() + } + } + console.log("Goodbye from Ruby on Rails 🚂") }