Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions keymaps/python-isort.cson
Original file line number Diff line number Diff line change
@@ -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'
30 changes: 13 additions & 17 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()
46 changes: 27 additions & 19 deletions lib/python-isort.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fs = require 'fs'
jquery = require('atom').$
$ = require('atom').$
process = require 'child_process'

module.exports =
Expand All @@ -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('<div id="python-isort-status" class="inline-block">
<span style="font-weight: bold">Isort: </span>
<span id="python-isort-status-message"></span>
</div>')
@statusBarTile = statusBar
.addLeftTile(
item: $('<div id="status-bar-python-isort" class="inline-block">
<span style="font-weight: bold">Isort: </span>
<span id="python-isort-status-message"></span>
</div>'), priority: 100)

statusBarElement = jquery("#python-isort-status-message")
statusBarElement = @statusBarTile.getItem()
.find('#python-isort-status-message')

if isError == true
statusBarElement.addClass("text-error")
Expand All @@ -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

Expand All @@ -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
4 changes: 4 additions & 0 deletions menus/python-isort.cson
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
'label': 'Sort Python Imports'
'command': 'python-isort:sortImports'
}
{
'label': 'Check Python Imports'
'command': 'python-isort:checkImports'
}
]
}
]
Expand Down