diff --git a/keymaps/python-isort.cson b/keymaps/python-isort.cson index 6b03ad7..30a09c5 100644 --- a/keymaps/python-isort.cson +++ b/keymaps/python-isort.cson @@ -1,4 +1,4 @@ # For more detailed documentation see # https://atom.io/docs/latest/advanced/keymaps -'.editor.python': - 'ctrl-alt-s': 'python-isort:sortImports' +'atom-text-editor[data-grammar="source python"]': + 'ctrl-alt-s': 'python-isort:sortImports' diff --git a/lib/index.coffee b/lib/index.coffee index 9df09e3..4d0b5ae 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -2,36 +2,32 @@ PythonIsort = require './python-isort' module.exports = configDefaults: - isortPath: "/usr/bin/isort" + isortPath: "isort" sortOnSave: false checkOnSave: true - activate: (state) -> + activate: -> pi = new PythonIsort() - atom.workspaceView.command 'pane:active-item-changed', -> + atom.commands.add 'atom-workspace', 'pane:active-item-changed', -> pi.removeStatusbarItem() - atom.workspaceView.command 'python-isort:sortImports', -> + atom.commands.add 'atom-workspace', 'python-isort:sortImports', -> pi.sortImports() - atom.workspaceView.command 'python-isort:checkImports', -> + atom.commands.add 'atom-workspace', 'python-isort:checkImports', -> pi.checkImports() - atom.config.observe 'python-isort.sortOnSave', {callNow: true}, (value) -> - atom.workspace.eachEditor (editor) -> + atom.config.observe 'python-isort.sortOnSave', (value) -> + atom.workspace.observeTextEditors (editor) -> if value == true - editor.buffer.on "saved", -> - pi.sortImports() + editor._isortSort = editor.onDidSave -> pi.sortImports() else - editor.buffer.off "saved", -> - pi.sortImports() + editor._isortSort?.dispose() - atom.config.observe 'python-isort.checkOnSave', {callNow: true}, (value) -> - atom.workspace.eachEditor (editor) -> + atom.config.observe 'python-isort.checkOnSave', (value) -> + atom.workspace.observeTextEditors (editor) -> if value == true - editor.buffer.on "saved", -> - pi.checkForUnsortedImports() + editor._isortCheck = editor.onDidSave -> pi.checkImports() else - editor.buffer.off "saved", -> - pi.checkForUnsortedImports() + editor._isortCheck?.dispose() diff --git a/lib/python-isort.coffee b/lib/python-isort.coffee index 9c7293d..166709d 100644 --- a/lib/python-isort.coffee +++ b/lib/python-isort.coffee @@ -1,5 +1,5 @@ fs = require 'fs' -jquery = require('atom').$ +$ = require('atom').$ process = require 'child_process' module.exports = @@ -11,20 +11,23 @@ class PythonIsort return false return editor.getGrammar().name == 'Python' - removeStatusbarItem: -> - if not @checkForPythonContext() - jquery("#python-isort-status").remove() + removeStatusbarItem: => + @statusBarTile?.destroy() + @statusBarTile = null - updateStatusbarText: (message, isError) -> - if jquery("#python-isort-status").length == 0 - statusBar = atom.workspaceView.statusBar + updateStatusbarText: (message, isError) => + if not @statusBarTile + statusBar = document.querySelector("status-bar") return unless statusBar? - statusBar.appendLeft('
- Isort: - -
') + @statusBarTile = statusBar + .addLeftTile( + item: $('
+ Isort: + +
'), priority: 100) - statusBarElement = jquery("#python-isort-status-message") + statusBarElement = @statusBarTile.getItem() + .find('#python-isort-status-message') if isError == true statusBarElement.addClass("text-error") @@ -37,11 +40,15 @@ class PythonIsort editor = atom.workspace.getActiveEditor() return editor.getPath() - checkForUnsortedImports: -> + checkImports: -> + if not @checkForPythonContext() + return + params = [@getFilePath(), "-c", "-vb"] isortpath = atom.config.get "python-isort.isortPath" - if not fs.existsSync(isortpath) + which = process.spawnSync('which', ['isort']).status + if which == 1 and not fs.existsSync(isortpath) @updateStatusbarText("unable to open " + isortpath, false) return @@ -50,21 +57,22 @@ class PythonIsort updateStatusbarText = @updateStatusbarText proc.on 'exit', (exit_code, signal) -> if exit_code == 0 - updateStatusbarText("√ all python imports are fine", false) + updateStatusbarText("√", false) else - updateStatusbarText("python imports are unsorted", true) + updateStatusbarText("x", true) sortImports: -> - if not @checkForPythonContext + if not @checkForPythonContext() return params = [@getFilePath(), "-vb"] isortpath = atom.config.get "python-isort.isortPath" - if not fs.existsSync(isortpath) + which = process.spawnSync('which', ['isort']).status + if which == 1 and not fs.existsSync(isortpath) @updateStatusbarText("unable to open " + isortpath, false) return proc = process.spawn isortpath, params - @updateStatusbarText("√ all python imports are fine", false) + @updateStatusbarText("√", false) @reload diff --git a/menus/python-isort.cson b/menus/python-isort.cson index 73e423f..2f113d8 100644 --- a/menus/python-isort.cson +++ b/menus/python-isort.cson @@ -9,6 +9,10 @@ 'label': 'Sort Python Imports' 'command': 'python-isort:sortImports' } + { + 'label': 'Check Python Imports' + 'command': 'python-isort:checkImports' + } ] } ]