From 74c7fe14873dc89184c01379c8b31c0abac5f2ae Mon Sep 17 00:00:00 2001 From: Shaoran Date: Fri, 27 Mar 2020 01:35:31 +0100 Subject: [PATCH 1/8] Allow to pass the FileMode options in the vim plugin The vim plugin takes now into consideration the `target-version` value of the `pyproject.toml` file. You can also specify a default target version with the configuration variable `g:black_target_version`. Vim's `:Black` command accepts now the following optional arguments: - `line_length` - `string_normalization` - `black_target_version` Those allow you to override any settings that come either from vim's configuration or the `pyproject.toml` file. It also supports supports a simple tab completion to allow to pass the arguments quicky. --- README.md | 10 ++++++++- plugin/black.vim | 53 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1e974e94630..6b073e33fbe 100644 --- a/README.md +++ b/README.md @@ -724,7 +724,13 @@ $ black --help Commands and shortcuts: -- `:Black` to format the entire file (ranges not supported); +- `:Black [target_version=TV],[line_length=int],[string_normalization=int]` to format + the entire file (ranges not supported), all arguments are optional and `TV` can be one + of: + - `"py27"` + - `"py36"` + - `"py37"` + - `"py38"` - `:BlackUpgrade` to upgrade _Black_ inside the virtualenv; - `:BlackVersion` to get the current version of _Black_ inside the virtualenv. @@ -734,6 +740,8 @@ Configuration: - `g:black_linelength` (defaults to `88`) - `g:black_skip_string_normalization` (defaults to `0`) - `g:black_virtualenv` (defaults to `~/.vim/black` or `~/.local/share/nvim/black`) +- `g:black_target_version` (defaults to empty string, same result as executing _Black_ + without the `-t` parameter) To install with [vim-plug](https://github.com/junegunn/vim-plug): diff --git a/plugin/black.vim b/plugin/black.vim index 8106ea16a39..d5dc28868eb 100644 --- a/plugin/black.vim +++ b/plugin/black.vim @@ -44,6 +44,9 @@ endif if !exists("g:black_skip_string_normalization") let g:black_skip_string_normalization = 0 endif +if !exists("g:black_target_version") + let g:black_target_version = "" +endif python3 << endpython3 import collections @@ -71,6 +74,7 @@ FLAGS = [ Flag(name="line_length", cast=int), Flag(name="fast", cast=bool), Flag(name="string_normalization", cast=bool), + Flag(name="target_version", cast=str), ] @@ -135,12 +139,42 @@ if _initialize_black_env(): import black import time -def Black(): +def get_target_version(tv): + if isinstance(tv, black.TargetVersion): + return tv + ret = None + try: + ret = black.TargetVersion[tv.upper()] + except: + pass + return ret + +def Black(**kwargs): + """ + kwargs allows you to override the values for + line_length, string_normalization and target_version. + target_version needs to be cleaned because black.FileMode + expects the target_versions argument to be a set of TargetVersion enums. + + Allow kwargs["target_version"] to be a string to allow + to type it more quickly. + + Using also target_version instead of target_versions to remain + consistent to Black's documentation of the structure of pyproject.toml. + """ start = time.time() configs = get_configs() + line_length = kwargs.get("line_length") or configs["line_length"] + string_normalization = kwargs.get("string_normalization") or configs["string_normalization"] + target_version = kwargs.get("target_version") or configs["target_version"] + if not isinstance(target_version, (list, set)): + target_version = [target_version] + target_version = set(filter(lambda x: x, map(lambda tv: get_target_version(tv), target_version))) + mode = black.FileMode( - line_length=configs["line_length"], - string_normalization=configs["string_normalization"], + target_versions = target_version, + line_length=line_length, + string_normalization=string_normalization, is_pyi=vim.current.buffer.name.endswith('.pyi'), ) @@ -193,6 +227,17 @@ def BlackVersion(): endpython3 -command! Black :py3 Black() +function BlackComplete(ArgLead, CmdLine, CursorPos) + return [ +\ 'target_version="py27"', +\ 'target_version="py36"', +\ 'target_version="py37"', +\ 'target_version="py38"', +\ 'line_length=', +\ 'string_normalization=', +\ ] +endfunction + +command! -nargs=* -complete=customlist,BlackComplete Black :py3 Black() command! BlackUpgrade :py3 BlackUpgrade() command! BlackVersion :py3 BlackVersion() From dffcfe1ebcf30a747470130522daf3608c62b4d4 Mon Sep 17 00:00:00 2001 From: Shaoran Date: Fri, 27 Mar 2020 12:46:47 +0100 Subject: [PATCH 2/8] chore: also allowing to pass the fast flag --- README.md | 2 +- plugin/black.vim | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6b073e33fbe..4aea10e9df0 100644 --- a/README.md +++ b/README.md @@ -724,7 +724,7 @@ $ black --help Commands and shortcuts: -- `:Black [target_version=TV],[line_length=int],[string_normalization=int]` to format +- `:Black [target_version=TV],[line_length=int],[string_normalization=int],[fast=int/bool]` to format the entire file (ranges not supported), all arguments are optional and `TV` can be one of: - `"py27"` diff --git a/plugin/black.vim b/plugin/black.vim index d5dc28868eb..b16b58d0515 100644 --- a/plugin/black.vim +++ b/plugin/black.vim @@ -166,6 +166,10 @@ def Black(**kwargs): configs = get_configs() line_length = kwargs.get("line_length") or configs["line_length"] string_normalization = kwargs.get("string_normalization") or configs["string_normalization"] + try: + fast = bool(kwargs["fast"]) + except: + fast = configs["fast"] target_version = kwargs.get("target_version") or configs["target_version"] if not isinstance(target_version, (list, set)): target_version = [target_version] @@ -182,7 +186,7 @@ def Black(**kwargs): try: new_buffer_str = black.format_file_contents( buffer_str, - fast=configs["fast"], + fast=fast, mode=mode, ) except black.NothingChanged: @@ -235,6 +239,7 @@ function BlackComplete(ArgLead, CmdLine, CursorPos) \ 'target_version="py38"', \ 'line_length=', \ 'string_normalization=', +\ 'fast=', \ ] endfunction From d1b4242bffa58b861f16f7ac66155759b2ddaa26 Mon Sep 17 00:00:00 2001 From: Shaoran Date: Fri, 27 Mar 2020 12:49:32 +0100 Subject: [PATCH 3/8] Fixing how to handle string_normalization argument I didn't realize that it was a boolan value. --- README.md | 6 +++--- plugin/black.vim | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4aea10e9df0..dd2e5088ff0 100644 --- a/README.md +++ b/README.md @@ -724,9 +724,9 @@ $ black --help Commands and shortcuts: -- `:Black [target_version=TV],[line_length=int],[string_normalization=int],[fast=int/bool]` to format - the entire file (ranges not supported), all arguments are optional and `TV` can be one - of: +- `:Black [target_version=TV],[line_length=int],[string_normalization=int/bool],[fast=int/bool]` + to format the entire file (ranges not supported), all arguments are optional and `TV` + can be one of: - `"py27"` - `"py36"` - `"py37"` diff --git a/plugin/black.vim b/plugin/black.vim index b16b58d0515..bc686e63c6a 100644 --- a/plugin/black.vim +++ b/plugin/black.vim @@ -165,7 +165,10 @@ def Black(**kwargs): start = time.time() configs = get_configs() line_length = kwargs.get("line_length") or configs["line_length"] - string_normalization = kwargs.get("string_normalization") or configs["string_normalization"] + try: + string_normalization = bool(kwargs["string_normalization"]) + except: + string_normalization = configs["string_normalization"] try: fast = bool(kwargs["fast"]) except: From f9c7f77201f31ae16e0a9e63896c70d7bb2f0393 Mon Sep 17 00:00:00 2001 From: Shaoran Date: Wed, 22 Sep 2021 21:18:53 +0200 Subject: [PATCH 4/8] doc: updating vim integration docs --- docs/integrations/editors.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/integrations/editors.md b/docs/integrations/editors.md index 6098631e2a0..32c395fadc8 100644 --- a/docs/integrations/editors.md +++ b/docs/integrations/editors.md @@ -116,6 +116,8 @@ Wing supports black via the OS Commands tool, as explained in the Wing documenta Commands and shortcuts: - `:Black` to format the entire file (ranges not supported); + - you can optionally pass `target_version=` where `` is + one of `[py27, py36, py37, py38, py39]` to set the target version. - `:BlackUpgrade` to upgrade _Black_ inside the virtualenv; - `:BlackVersion` to get the current version of _Black_ inside the virtualenv. From 7c27c10a63b7cdfa379adece69902e4852e763d7 Mon Sep 17 00:00:00 2001 From: Shaoran Date: Wed, 22 Sep 2021 21:24:26 +0200 Subject: [PATCH 5/8] chore: running pre-commit run --all-files --- docs/integrations/editors.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/integrations/editors.md b/docs/integrations/editors.md index 32c395fadc8..542c85b552f 100644 --- a/docs/integrations/editors.md +++ b/docs/integrations/editors.md @@ -116,8 +116,8 @@ Wing supports black via the OS Commands tool, as explained in the Wing documenta Commands and shortcuts: - `:Black` to format the entire file (ranges not supported); - - you can optionally pass `target_version=` where `` is - one of `[py27, py36, py37, py38, py39]` to set the target version. + - you can optionally pass `target_version=` where `` is one of + `[py27, py36, py37, py38, py39]` to set the target version. - `:BlackUpgrade` to upgrade _Black_ inside the virtualenv; - `:BlackVersion` to get the current version of _Black_ inside the virtualenv. From 9427edc3688e5937f69ce5e6117f2b2b9346d20e Mon Sep 17 00:00:00 2001 From: Shaoran Date: Thu, 23 Sep 2021 23:08:28 +0200 Subject: [PATCH 6/8] doc: mentioning that target_version is the same as in the command line --- docs/integrations/editors.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/integrations/editors.md b/docs/integrations/editors.md index 542c85b552f..d3be7c0ea84 100644 --- a/docs/integrations/editors.md +++ b/docs/integrations/editors.md @@ -116,8 +116,8 @@ Wing supports black via the OS Commands tool, as explained in the Wing documenta Commands and shortcuts: - `:Black` to format the entire file (ranges not supported); - - you can optionally pass `target_version=` where `` is one of - `[py27, py36, py37, py38, py39]` to set the target version. + - you can optionally pass `target_version=` with the same values as in the + command line. - `:BlackUpgrade` to upgrade _Black_ inside the virtualenv; - `:BlackVersion` to get the current version of _Black_ inside the virtualenv. From d983f740e92327131b1425a67e1d69dd09c15e27 Mon Sep 17 00:00:00 2001 From: Shaoran Date: Thu, 23 Sep 2021 23:11:42 +0200 Subject: [PATCH 7/8] chore: add this pull request to the changelog to make CI happy --- CHANGES.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 6ff488ba74e..ad8cd859f87 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,11 @@ # Change Log +## Unreleased + +### Integrations + +- Allow to pass `target_version` in the vim plugin (#1319) + ## 21.9b0 ### Packaging From 956d1909c3881f4ba4a7aa16e2dd4218c8102db0 Mon Sep 17 00:00:00 2001 From: Shaoran Date: Thu, 23 Sep 2021 23:23:03 +0200 Subject: [PATCH 8/8] chore: catch KeyError and print warning if no target verison is found --- autoload/black.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/black.vim b/autoload/black.vim index 9a8b64ace30..9ff5c2341fe 100644 --- a/autoload/black.vim +++ b/autoload/black.vim @@ -104,8 +104,8 @@ def get_target_version(tv): ret = None try: ret = black.TargetVersion[tv.upper()] - except: - pass + except KeyError: + print(f"WARNING: Target version {tv!r} not recognized by Black, using default target") return ret def Black(**kwargs):