From dd9aacffb0332acc2c3b7e4089e25eb5a49a0a3c Mon Sep 17 00:00:00 2001 From: Arlo Shallit Date: Mon, 30 Jul 2018 16:53:59 -0400 Subject: [PATCH] Add Black python beautifier --- CHANGELOG.md | 1 + README.md | 3 ++- docs/options.md | 45 +++++++++++++++++++++++++----------- package.json | 7 +++++- src/beautifiers/black.coffee | 39 +++++++++++++++++++++++++++++++ src/beautifiers/index.coffee | 1 + src/options.json | 18 +++++++++++++++ 7 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 src/beautifiers/black.coffee diff --git a/CHANGELOG.md b/CHANGELOG.md index a09ae82..af77d9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Next - Fixes an issue where Atom Beautify would display a Docker error instead of an executable error ([#2146](https://github.com/Glavin001/atom-beautify/issues/2146)) - Fixes Rubocop not excluding files with an exact pattern match (`db/schema.rb` vs `db/**/*`) +- Add [Black](http://github.com/ambv/black) beautifier for Python. # v0.32.5 (2018-05-28) - Fixes an issue with Rubocop not working on Windows ([#2092](https://github.com/Glavin001/atom-beautify/issues/2092)) diff --git a/README.md b/README.md index 7f7eacf..c71635b 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ Some of the supported beautifiers are developed for Node.js and are automaticall | align-yaml | :white_check_mark: | :ok_hand: Not necessary | :smiley: Nothing! | | autopep8 | :warning: 2 executables | :warning: Only 1 of 2 executables | :whale: With [Docker](https://www.docker.com/):
1. Install [autopep8 (`autopep8`)](https://github.com/hhatto/autopep8) with `docker pull unibeautify/autopep8`

:bookmark_tabs: Manually:
1. Install [autopep8 (`autopep8`)](https://github.com/hhatto/autopep8) by following https://github.com/hhatto/autopep8#installation
2. Install [isort (`isort`)](https://github.com/timothycrosley/isort) by following https://github.com/timothycrosley/isort#installing-isort
| | beautysh | :warning: 1 executable | :white_check_mark: :100:% of executables | :whale: With [Docker](https://www.docker.com/):
1. Install [beautysh (`beautysh`)](https://github.com/bemeurer/beautysh) with `docker pull unibeautify/beautysh`

:bookmark_tabs: Manually:
1. Install [beautysh (`beautysh`)](https://github.com/bemeurer/beautysh) by following https://github.com/bemeurer/beautysh#installation
| +| black | :warning: 1 executable | :x: No Docker support | :bookmark_tabs: Manually:
1. Install [black (`black`)](https://github.com/ambv/black) by following https://github.com/ambv/black#installation
| | brittany | :warning: Manual installation | :construction: Not an executable | :page_facing_up: Go to https://github.com/lspitzner/brittany and follow the instructions. | | clang-format | :warning: 1 executable | :white_check_mark: :100:% of executables | :whale: With [Docker](https://www.docker.com/):
1. Install [ClangFormat (`clang-format`)](https://clang.llvm.org/docs/ClangFormat.html) with `docker pull unibeautify/clang-format`

:bookmark_tabs: Manually:
1. Install [ClangFormat (`clang-format`)](https://clang.llvm.org/docs/ClangFormat.html) by following https://clang.llvm.org/docs/ClangFormat.html
| | cljfmt | :white_check_mark: | :ok_hand: Not necessary | :smiley: Nothing! | @@ -196,7 +197,7 @@ See [all supported options in the documentation at `docs/options.md`](docs/opti | Perl | `Perl`, `Perl 6` |`.pl`, `.PL`, `.pm`, `.pod`, `.t` | **[`Perltidy`](http://perltidy.sourceforge.net/)** | | PHP | `PHP` |`.php`, `.module`, `.inc` | **[`PHP-CS-Fixer`](https://github.com/FriendsOfPHP/PHP-CS-Fixer)**, [`PHPCBF`](http://php.net/manual/en/install.php), [`hh_format`](http://hhvm.com/) | | Puppet | `Puppet` |`.pp` | **[`puppet-lint`](http://puppet-lint.com/)** | -| Python | `Python`, `MagicPython` |`.py` | **[`autopep8`](https://github.com/hhatto/autopep8)**, [`pybeautifier`](https://github.com/guyskk/pybeautifier), [`yapf`](https://github.com/google/yapf) | +| Python | `Python`, `MagicPython` |`.py` | **[`autopep8`](https://github.com/hhatto/autopep8)**, [`black`](https://github.com/ambv/black), [`pybeautifier`](https://github.com/guyskk/pybeautifier), [`yapf`](https://github.com/google/yapf) | | R | `R` |`.r`, `.R` | **[`formatR`](https://github.com/yihui/formatR)** | | Riot.js | `Riot.js`, `HTML (Riot Tag)` |`.tag` | **[`Pretty Diff`](https://github.com/prettydiff/prettydiff)** | | Ruby | `Ruby`, `Ruby on Rails` |`.rb` | **[`Rubocop`](https://github.com/bbatsov/rubocop)**, [`Ruby Beautify`](https://github.com/erniebrodeur/ruby-beautify) | diff --git a/docs/options.md b/docs/options.md index 99cb129..1bc271f 100644 --- a/docs/options.md +++ b/docs/options.md @@ -164,6 +164,23 @@ Options for beautysh executable. 2. Go into *Packages* and search for "*Atom Beautify*" package. 3. Find the option "*beautysh*" and change it to your desired configuration. +##### [black](#black) + +**Important**: This option is only configurable from within Atom Beautify's setting panel. + +**Type**: `object` + +**Description**: + +Options for black executable. + +**How to Configure** + +1. You can open the [Settings View](https://github.com/atom/settings-view) by navigating to +*Edit > Preferences (Linux)*, *Atom > Preferences (OS X)*, or *File > Preferences (Windows)*. +2. Go into *Packages* and search for "*Atom Beautify*" package. +3. Find the option "*black*" and change it to your desired configuration. + ##### [ClangFormat](#clangformat) **Important**: This option is only configurable from within Atom Beautify's setting panel. @@ -10644,20 +10661,20 @@ Automatically beautify Puppet files on save #### [Python](#python) -**Supported Beautifiers**: [`autopep8`](#autopep8) [`pybeautifier`](#pybeautifier) [`yapf`](#yapf) +**Supported Beautifiers**: [`autopep8`](#autopep8) [`black`](#black) [`pybeautifier`](#pybeautifier) [`yapf`](#yapf) -| Option | autopep8 | pybeautifier | yapf | -| --- | --- | --- | --- | -| `disabled` | :white_check_mark: | :white_check_mark: | :white_check_mark: | -| `default_beautifier` | :white_check_mark: | :white_check_mark: | :white_check_mark: | -| `beautify_on_save` | :white_check_mark: | :white_check_mark: | :white_check_mark: | -| `formatter` | :white_check_mark: | :white_check_mark: | :x: | -| `ignore` | :white_check_mark: | :white_check_mark: | :x: | -| `indent_size` | :white_check_mark: | :white_check_mark: | :x: | -| `max_line_length` | :white_check_mark: | :white_check_mark: | :x: | -| `multi_line_output` | :white_check_mark: | :white_check_mark: | :x: | -| `sort_imports` | :white_check_mark: | :white_check_mark: | :x: | -| `style_config` | :white_check_mark: | :white_check_mark: | :x: | +| Option | autopep8 | black | pybeautifier | yapf | +| --- | --- | --- | --- | --- | +| `disabled` | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | +| `default_beautifier` | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | +| `beautify_on_save` | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | +| `formatter` | :white_check_mark: | :x: | :white_check_mark: | :x: | +| `ignore` | :white_check_mark: | :x: | :white_check_mark: | :x: | +| `indent_size` | :white_check_mark: | :x: | :white_check_mark: | :x: | +| `max_line_length` | :white_check_mark: | :x: | :white_check_mark: | :x: | +| `multi_line_output` | :white_check_mark: | :x: | :white_check_mark: | :x: | +| `sort_imports` | :white_check_mark: | :x: | :white_check_mark: | :x: | +| `style_config` | :white_check_mark: | :x: | :white_check_mark: | :x: | **Description**: @@ -10688,7 +10705,7 @@ Disable Python Beautification **Type**: `string` -**Enum**: `autopep8` `pybeautifier` `yapf` +**Enum**: `autopep8` `black` `pybeautifier` `yapf` **Description**: diff --git a/package.json b/package.json index 1209e2e..aaf5937 100644 --- a/package.json +++ b/package.json @@ -168,6 +168,10 @@ { "name": "Kohei Hiraga", "url": "https://github.com/range3" + }, + { + "name": "Arlo Shallit", + "url": "https://github.com/arl-o" } ], "engines": { @@ -446,7 +450,8 @@ "vhdl", "vhdl beautifier", "gn", - "blade" + "blade", + "black" ], "devDependencies": { "codo": "^2.1.2", diff --git a/src/beautifiers/black.coffee b/src/beautifiers/black.coffee new file mode 100644 index 0000000..efda8a6 --- /dev/null +++ b/src/beautifiers/black.coffee @@ -0,0 +1,39 @@ +### +Requires [black](https://github.com/ambv/black) +### +"use strict" +Beautifier = require('./beautifier') +path = require('path') + +module.exports = class Black extends Beautifier + name: "black" + link: "https://github.com/ambv/black" + executables: [ + { + name: "black" + cmd: "black" + homepage: "https://github.com/ambv/black" + installation: "https://github.com/ambv/black#installation" + version: { + parse: (text) -> + # Try to read beta values, eg "black, version 18.6b4" -> 18.6.4 + try + text.match(/black, version (\d+\.\d+)/)[1] + "." + text.match(/b(\d+)$/)[1] + catch + text.match(/black, version (\d+\.\d+)/)[1] + ".0" + } + } + ] + + options: { + Python: false + } + + beautify: (text, language, options, context) -> + cwd = context.filePath and path.dirname context.filePath + # `-` as filename reads from stdin + @exe("black").run(["-"], { + cwd: cwd + onStdin: (stdin) -> + stdin.end text + }) diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index b6d3db1..c77cbed 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -43,6 +43,7 @@ module.exports = class Beautifiers extends EventEmitter 'cljfmt' 'clang-format' 'crystal' + 'black' 'dfmt' 'elm-format' 'hh_format' diff --git a/src/options.json b/src/options.json index 60476a0..9f17bd0 100644 --- a/src/options.json +++ b/src/options.json @@ -6274,6 +6274,7 @@ ], "beautifiers": [ "autopep8", + "black", "pybeautifier", "yapf" ], @@ -6424,6 +6425,7 @@ "description": "Default Beautifier to be used for Python", "enum": [ "autopep8", + "black", "pybeautifier", "yapf" ] @@ -9439,6 +9441,22 @@ } } }, + "black": { + "key": "black", + "title": "black", + "type": "object", + "collapsed": true, + "description": "Options for black executable.", + "properties": { + "path": { + "key": "path", + "title": "Binary/Script Path", + "type": "string", + "default": "", + "description": "Absolute path to the \"black\" executable's binary/script." + } + } + }, "dfmt": { "key": "dfmt", "title": "Dfmt",