Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit b1d729b

Browse files
committed
Merge pull request #6 from jmfederico/master
Fix deprecation and call binary name from PATH
2 parents 2d677b7 + 7e61d31 commit b1d729b

File tree

4 files changed

+46
-38
lines changed

4 files changed

+46
-38
lines changed

keymaps/python-isort.cson

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# For more detailed documentation see
22
# https://atom.io/docs/latest/advanced/keymaps
3-
'.editor.python':
4-
'ctrl-alt-s': 'python-isort:sortImports'
3+
'atom-text-editor[data-grammar="source python"]':
4+
'ctrl-alt-s': 'python-isort:sortImports'

lib/index.coffee

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,32 @@ PythonIsort = require './python-isort'
22

33
module.exports =
44
configDefaults:
5-
isortPath: "/usr/bin/isort"
5+
isortPath: "isort"
66
sortOnSave: false
77
checkOnSave: true
88

9-
activate: (state) ->
9+
activate: ->
1010
pi = new PythonIsort()
1111

12-
atom.workspaceView.command 'pane:active-item-changed', ->
12+
atom.commands.add 'atom-workspace', 'pane:active-item-changed', ->
1313
pi.removeStatusbarItem()
1414

15-
atom.workspaceView.command 'python-isort:sortImports', ->
15+
atom.commands.add 'atom-workspace', 'python-isort:sortImports', ->
1616
pi.sortImports()
1717

18-
atom.workspaceView.command 'python-isort:checkImports', ->
18+
atom.commands.add 'atom-workspace', 'python-isort:checkImports', ->
1919
pi.checkImports()
2020

21-
atom.config.observe 'python-isort.sortOnSave', {callNow: true}, (value) ->
22-
atom.workspace.eachEditor (editor) ->
21+
atom.config.observe 'python-isort.sortOnSave', (value) ->
22+
atom.workspace.observeTextEditors (editor) ->
2323
if value == true
24-
editor.buffer.on "saved", ->
25-
pi.sortImports()
24+
editor._isortSort = editor.onDidSave -> pi.sortImports()
2625
else
27-
editor.buffer.off "saved", ->
28-
pi.sortImports()
26+
editor._isortSort?.dispose()
2927

30-
atom.config.observe 'python-isort.checkOnSave', {callNow: true}, (value) ->
31-
atom.workspace.eachEditor (editor) ->
28+
atom.config.observe 'python-isort.checkOnSave', (value) ->
29+
atom.workspace.observeTextEditors (editor) ->
3230
if value == true
33-
editor.buffer.on "saved", ->
34-
pi.checkForUnsortedImports()
31+
editor._isortCheck = editor.onDidSave -> pi.checkImports()
3532
else
36-
editor.buffer.off "saved", ->
37-
pi.checkForUnsortedImports()
33+
editor._isortCheck?.dispose()

lib/python-isort.coffee

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fs = require 'fs'
2-
jquery = require('atom').$
2+
$ = require('atom').$
33
process = require 'child_process'
44

55
module.exports =
@@ -11,20 +11,23 @@ class PythonIsort
1111
return false
1212
return editor.getGrammar().name == 'Python'
1313

14-
removeStatusbarItem: ->
15-
if not @checkForPythonContext()
16-
jquery("#python-isort-status").remove()
14+
removeStatusbarItem: =>
15+
@statusBarTile?.destroy()
16+
@statusBarTile = null
1717

18-
updateStatusbarText: (message, isError) ->
19-
if jquery("#python-isort-status").length == 0
20-
statusBar = atom.workspaceView.statusBar
18+
updateStatusbarText: (message, isError) =>
19+
if not @statusBarTile
20+
statusBar = document.querySelector("status-bar")
2121
return unless statusBar?
22-
statusBar.appendLeft('<div id="python-isort-status" class="inline-block">
23-
<span style="font-weight: bold">Isort: </span>
24-
<span id="python-isort-status-message"></span>
25-
</div>')
22+
@statusBarTile = statusBar
23+
.addLeftTile(
24+
item: $('<div id="status-bar-python-isort" class="inline-block">
25+
<span style="font-weight: bold">Isort: </span>
26+
<span id="python-isort-status-message"></span>
27+
</div>'), priority: 100)
2628

27-
statusBarElement = jquery("#python-isort-status-message")
29+
statusBarElement = @statusBarTile.getItem()
30+
.find('#python-isort-status-message')
2831

2932
if isError == true
3033
statusBarElement.addClass("text-error")
@@ -37,11 +40,15 @@ class PythonIsort
3740
editor = atom.workspace.getActiveEditor()
3841
return editor.getPath()
3942

40-
checkForUnsortedImports: ->
43+
checkImports: ->
44+
if not @checkForPythonContext()
45+
return
46+
4147
params = [@getFilePath(), "-c", "-vb"]
4248
isortpath = atom.config.get "python-isort.isortPath"
4349

44-
if not fs.existsSync(isortpath)
50+
which = process.spawnSync('which', ['isort']).status
51+
if which == 1 and not fs.existsSync(isortpath)
4552
@updateStatusbarText("unable to open " + isortpath, false)
4653
return
4754

@@ -50,21 +57,22 @@ class PythonIsort
5057
updateStatusbarText = @updateStatusbarText
5158
proc.on 'exit', (exit_code, signal) ->
5259
if exit_code == 0
53-
updateStatusbarText(" all python imports are fine", false)
60+
updateStatusbarText("", false)
5461
else
55-
updateStatusbarText("python imports are unsorted", true)
62+
updateStatusbarText("x", true)
5663

5764
sortImports: ->
58-
if not @checkForPythonContext
65+
if not @checkForPythonContext()
5966
return
6067

6168
params = [@getFilePath(), "-vb"]
6269
isortpath = atom.config.get "python-isort.isortPath"
6370

64-
if not fs.existsSync(isortpath)
71+
which = process.spawnSync('which', ['isort']).status
72+
if which == 1 and not fs.existsSync(isortpath)
6573
@updateStatusbarText("unable to open " + isortpath, false)
6674
return
6775

6876
proc = process.spawn isortpath, params
69-
@updateStatusbarText(" all python imports are fine", false)
77+
@updateStatusbarText("", false)
7078
@reload

menus/python-isort.cson

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
'label': 'Sort Python Imports'
1010
'command': 'python-isort:sortImports'
1111
}
12+
{
13+
'label': 'Check Python Imports'
14+
'command': 'python-isort:checkImports'
15+
}
1216
]
1317
}
1418
]

0 commit comments

Comments
 (0)