From 36b0277783a7e8eb5fdc205e30dcd0b9305224d6 Mon Sep 17 00:00:00 2001 From: lexicalunit Date: Sat, 23 Sep 2017 18:14:30 -0500 Subject: [PATCH] Fixes sort-on-save to properly ignore non-python files. Resolves #15. --- index.js => lib/index.js | 33 +++++++++++++++++++++++++-------- lib/linter.js | 36 ------------------------------------ package.json | 12 +++++++----- 3 files changed, 32 insertions(+), 49 deletions(-) rename index.js => lib/index.js (76%) delete mode 100644 lib/linter.js diff --git a/index.js b/lib/index.js similarity index 76% rename from index.js rename to lib/index.js index 43d4a8d..ab4d00a 100644 --- a/index.js +++ b/lib/index.js @@ -40,14 +40,13 @@ function getPythonEnvironment () { } export default { - config: require('./lib/config.coffee').config, + config: require('./config.coffee').config, activate () { if (atom.inDevMode()) console.log('activate atom-isort') require('atom-package-deps').install('atom-isort') - const Sorter = require('./lib/sorter') + const Sorter = require('./sorter') this.sorter = new Sorter(getPythonEnvironment()) - this.linter = null this.handleEvents() }, @@ -65,7 +64,10 @@ export default { this.editorSubs.add(atom.workspace.observeTextEditors((textEditor) => { if (sortOnSave) { textEditor._isortSortOnWillSave = - textEditor.buffer.onWillSave(() => this.sorter.sortImports(textEditor, true)) + textEditor.buffer.onWillSave(() => { + if (textEditor.getGrammar().scopeName !== 'source.python') return + this.sorter.sortImports(textEditor, true) + }) } else { __guard__(textEditor._isortSortOnWillSave, x => x.dispose()) } @@ -74,9 +76,25 @@ export default { }, provideLinter () { - const Linter = require('./lib/linter') - if (!this.linter) this.linter = new Linter(this.sorter) - return this.linter + let getSorter = () => this.sorter + return { + name: 'isort', + scope: 'file', + lintsOnChange: false, + grammarScopes: ['source.python'], + async lint (textEditor) { + const sorted = getSorter().checkImports(textEditor) + if (sorted) return [] + return [{ + severity: 'warning', + location: { + file: textEditor.getPath(), + position: [[0, 0], [0, 0]] + }, + excerpt: 'Imports not sorted' + }] + } + } }, deactivate () { @@ -90,7 +108,6 @@ export default { __guard__(this.subs, x => x.dispose()) this.subs = null - this.linter = null this.sorter = null } } diff --git a/lib/linter.js b/lib/linter.js deleted file mode 100644 index 2d50c18..0000000 --- a/lib/linter.js +++ /dev/null @@ -1,36 +0,0 @@ -/** @babel */ - -let Linter - -export default ( - Linter = (() => { - Linter = class Linter { - static initClass () { - this.prototype.name = 'isort' - this.prototype.scope = 'file' - this.prototype.lintsOnChange = false - this.prototype.grammarScopes = ['source.python'] - } - - constructor (sorter) { - this.sorter = sorter - } - - lint (textEditor) { - const sorted = this.sorter.checkImports(textEditor) - if (sorted) return [] - return [{ - severity: 'warning', - location: { - file: textEditor.getPath(), - position: [[0, 0], [0, 0]] - }, - excerpt: 'Imports not sorted' - }] - } - } - - Linter.initClass() - return Linter - })() -) diff --git a/package.json b/package.json index b868186..8a77fbc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "atom-isort", - "main": "./index", + "main": "./lib/index", "version": "2.5.0", "description": "Organize python imports using isort", "repository": "https://github.com/lexicalunit/atom-isort", @@ -9,7 +9,7 @@ "atom": ">=1.4.0 <2.0.0" }, "package-deps": [ - "linter" + "linter:2.0.0" ], "providedServices": { "linter": { @@ -19,17 +19,19 @@ } }, "dependencies": { - "atom-package-deps": "^4.2.0" + "atom-linter": "^10.0.0", + "atom-package-deps": "^4.6.0" }, "keywords": [ "python", "style", "isort", "formatting", - "productivity" + "productivity", + "linter" ], "devDependencies": { - "eslint": "^4.3.0", + "eslint": "^4.5.0", "eslint-config-standard": "^10.2.1", "eslint-plugin-import": "^2.7.0", "eslint-plugin-node": "^5.1.1",