├── .gitignore ├── LICENSE ├── README.md ├── keymaps └── python-autopep8.cson ├── lib ├── index.coffee └── python-autopep8.coffee ├── menus └── python-autopep8.cson └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | npm-debug.log 2 | node_modules 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mark Baas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Atom Python Autopep8 2 | 3 | Uses [autopep8](https://github.com/hhatto/autopep8) installed to format python code according to pep8 guidelines. 4 | 5 | 6 | ### Install 7 | 8 | Make sure to have [autopep8](https://github.com/hhatto/autopep8) installed. Install it by running `pip install autopep8` 9 | -------------------------------------------------------------------------------- /keymaps/python-autopep8.cson: -------------------------------------------------------------------------------- 1 | # For more detailed documentation see 2 | # https://atom.io/docs/latest/advanced/keymaps 3 | 'atom-text-editor[data-grammar="source python"]': 4 | 'ctrl-alt-s': 'python-autopep8:format' 5 | -------------------------------------------------------------------------------- /lib/index.coffee: -------------------------------------------------------------------------------- 1 | PythonAutopep8 = require './python-autopep8' 2 | 3 | module.exports = 4 | config: 5 | autopep8Path: 6 | type: 'string' 7 | default: 'autopep8' 8 | formatOnSave: 9 | type: 'boolean' 10 | default: false 11 | maxLineLength: 12 | type: 'integer' 13 | default: 100 14 | cmdLineOptions: 15 | type: 'array' 16 | default: [] 17 | description: 18 | "Cmd line switches passed to autopep8 delimited by commas 19 | (e.g. '-a, -a, --experimental')" 20 | 21 | activate: -> 22 | pi = new PythonAutopep8() 23 | 24 | atom.commands.add 'atom-workspace', 'pane:active-item-changed', -> 25 | pi.removeStatusbarItem() 26 | 27 | atom.commands.add 'atom-workspace', 'python-autopep8:format', -> 28 | pi.format() 29 | 30 | atom.config.observe 'python-autopep8.formatOnSave', (value) -> 31 | atom.workspace.observeTextEditors (editor) -> 32 | if value == true 33 | editor._autopep8Format = editor.onDidSave -> pi.format() 34 | else 35 | editor._autopep8Format?.dispose() 36 | -------------------------------------------------------------------------------- /lib/python-autopep8.coffee: -------------------------------------------------------------------------------- 1 | $ = require 'jquery' 2 | process = require 'child_process' 3 | 4 | module.exports = 5 | class PythonAutopep8 6 | 7 | checkForPythonContext: -> 8 | editor = atom.workspace.getActiveTextEditor() 9 | if not editor? 10 | return false 11 | grammar = editor.getGrammar().name 12 | return grammar == 'Python' or grammar == 'MagicPython' 13 | 14 | removeStatusbarItem: => 15 | @statusBarTile?.destroy() 16 | @statusBarTile = null 17 | 18 | updateStatusbarText: (message, isError) => 19 | if not @statusBarTile 20 | statusBar = document.querySelector("status-bar") 21 | return unless statusBar? 22 | @statusBarTile = statusBar 23 | .addLeftTile( 24 | item: $('
25 | Autopep8: 26 | 27 |
'), priority: 100) 28 | 29 | statusBarElement = @statusBarTile.getItem() 30 | .find('#python-autopep8-status-message') 31 | 32 | if isError == true 33 | statusBarElement.addClass("text-error") 34 | else 35 | statusBarElement.removeClass("text-error") 36 | 37 | statusBarElement.text(message) 38 | 39 | getFilePath: -> 40 | editor = atom.workspace.getActiveTextEditor() 41 | return editor.getPath() 42 | 43 | format: -> 44 | if not @checkForPythonContext() 45 | return 46 | 47 | cmd = atom.config.get "python-autopep8.autopep8Path" 48 | maxLineLength = atom.config.get "python-autopep8.maxLineLength" 49 | cmdLineOptions = atom.config.get "python-autopep8.cmdLineOptions" 50 | params = cmdLineOptions.concat [ 51 | "--max-line-length", maxLineLength, "-i", @getFilePath() 52 | ] 53 | 54 | returnCode = process.spawnSync(cmd, params).status 55 | if returnCode != 0 56 | @updateStatusbarText("x", true) 57 | else 58 | @updateStatusbarText("√", false) 59 | @reload 60 | -------------------------------------------------------------------------------- /menus/python-autopep8.cson: -------------------------------------------------------------------------------- 1 | 'menu': [ 2 | { 3 | 'label': 'Packages' 4 | 'submenu': [ 5 | { 6 | 'label': 'Python Autopep8' 7 | 'submenu': [ 8 | { 9 | 'label': 'Format python code' 10 | 'command': 'python-autopep8:format' 11 | } 12 | ] 13 | } 14 | ] 15 | } 16 | ] 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "python-autopep8", 3 | "main": "./lib/index", 4 | "version": "0.1.4", 5 | "description": "Format python code using autopep8", 6 | "repository": "https://github.com/markbaas/atom-python-autopep8", 7 | "license": "MIT", 8 | "engines": { 9 | "atom": ">=0.185.0 <2.0.0" 10 | }, 11 | "dependencies": { 12 | "jquery": "^2.1.4" 13 | } 14 | } 15 | --------------------------------------------------------------------------------