Skip to content

Commit

Permalink
Only observe text editors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sonicdoe committed Jul 27, 2017
1 parent 3c9ad13 commit e51f30b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 33 deletions.
27 changes: 6 additions & 21 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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,
Expand All @@ -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')
}
17 changes: 5 additions & 12 deletions lib/utils/style-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,30 @@ var styleOptions = [
'uber-standard'
]

function checkStyleSettings (filePath) {
function checkStyleSettings (filePath, textEditor) {
var settings = {}
var projectPath = null

// Try to get the project's absolute path
// 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
Expand Down

0 comments on commit e51f30b

Please sign in to comment.