From e51f30bedba39d028431f0ac71bb4f7aed6db5cf Mon Sep 17 00:00:00 2001 From: Jakob Krigovsky Date: Thu, 27 Jul 2017 21:48:03 +0200 Subject: [PATCH] Only observe text editors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When opening a file in a pending tab from the tree view, the active pane will be the tree view (instead of the text editor). This causes us to throw the “Something went wrong internally” warning if there haven’t been any other active text editors (because the `text-editor` cache is still empty at that point). To avoid this, we use observeTextEditors() to only observe text editors (instead of every active pane change). This further allows us to remove __checkIfTextEditor() and the initial storeSettings() call on startup (since observeTextEditors() will call back anyway). However, it requires us to pass the text editor to checkStyleSettings() since it used getActiveTextEditor() on its own. Fixes #179 and closes #190. --- lib/init.js | 27 ++++++--------------------- lib/utils/style-settings.js | 17 +++++------------ 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/lib/init.js b/lib/init.js index 5db3572..292f48c 100644 --- a/lib/init.js +++ b/lib/init.js @@ -50,17 +50,12 @@ module.exports = { var config = atom.config.get('linter-js-standard') self.cache.set('config', config) - var storeSettings = function (paneItem) { - // Check if the pane is a file - if (!self.__checkIfTextEditor(paneItem)) { - return - } - + var storeSettings = function (textEditor) { // Get config var config = self.cache.get('config') // Check if this file is inside our grammar scope - var grammar = paneItem.getGrammar() || { scopeName: null } + var grammar = textEditor.getGrammar() || { scopeName: null } // Check if this file is inside any kind of html scope (such as text.html.basic among others) if (config.lintHtmlFiles && /^text.html/.test(grammar.scopeName) && self.scope.indexOf(grammar.scopeName) < 0) { @@ -75,20 +70,14 @@ module.exports = { return } - // Cache active pane - self.__cacheTextEditor(config, paneItem) + // Cache text editor + self.__cacheTextEditor(config, textEditor) } - // On startup get active pane - // check if it's a text editor - // if it is cache it's settings - var paneItem = atom.workspace.getActivePaneItem() - storeSettings(paneItem) - // Create some subscriptions self.subscriptions = new CompositeDisposable() - self.subscriptions.add(atom.workspace.onDidChangeActivePaneItem(storeSettings)) + self.subscriptions.add(atom.workspace.observeTextEditors(storeSettings)) // on package settings change self.subscriptions.add(atom.config.observe('linter-js-standard', function (config) { @@ -105,7 +94,7 @@ module.exports = { __cacheTextEditor: function (config, textEditor) { var filePath = textEditor.getPath() - var opts = config.honorStyleSettings ? styleSettings.checkStyleSettings(filePath) : {} + var opts = config.honorStyleSettings ? styleSettings.checkStyleSettings(filePath, textEditor) : {} var style = selectStyle(filePath, { style: opts.style || config.style, @@ -116,9 +105,5 @@ module.exports = { this.cache.set('text-editor', { style: style, opts: opts }) }, - __checkIfTextEditor: function (paneItem) { - return (paneItem && typeof paneItem.getGrammar === 'function' && typeof paneItem.getPath === 'function') - }, - provideLinter: require('./linter-js-standard') } diff --git a/lib/utils/style-settings.js b/lib/utils/style-settings.js index d4eae86..c1fd752 100644 --- a/lib/utils/style-settings.js +++ b/lib/utils/style-settings.js @@ -11,7 +11,7 @@ var styleOptions = [ 'uber-standard' ] -function checkStyleSettings (filePath) { +function checkStyleSettings (filePath, textEditor) { var settings = {} var projectPath = null @@ -19,29 +19,22 @@ function checkStyleSettings (filePath) { // NOTE: the project's path returned will be // from the nearest package.json (direction upward) try { - var activePane = atom.workspace.getActiveTextEditor() + var textEditorPath = textEditor.getPath() - if (!activePane || !activePane.getPath) { - settings.style = 'no-style' - return settings - } - - var activePanePath = activePane.getPath() - - if (!activePanePath) { + if (!textEditorPath) { settings.style = 'no-style' return settings } try { - activePanePath = fs.realpathSync(activePanePath) + textEditorPath = fs.realpathSync(textEditorPath) } catch (e) { if (e.code !== 'ENOENT') throw e } var projectPaths = atom.project.getPaths() - projectPath = projectPaths.find((p) => activePanePath.indexOf(fs.realpathSync(p)) >= 0) + projectPath = projectPaths.find((p) => textEditorPath.indexOf(fs.realpathSync(p)) >= 0) } catch (e) { console.error('Could not get project path', e) return