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