Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass the FileMode options in the vim plugin #1319

Merged
merged 11 commits into from Sep 29, 2021
49 changes: 46 additions & 3 deletions autoload/black.vim
Expand Up @@ -98,13 +98,47 @@ if _initialize_black_env():
import black
import time

def Black():
def get_target_version(tv):
shaoran marked this conversation as resolved.
Show resolved Hide resolved
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 ``target_versions`` argument of
``black.FileMode``.

``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()

black_kwargs = {}
if "target_version" in kwargs:
target_version = kwargs["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)))
black_kwargs["target_versions"] = target_version

mode = black.FileMode(
line_length=configs["line_length"],
string_normalization=not configs["skip_string_normalization"],
is_pyi=vim.current.buffer.name.endswith('.pyi'),
**black_kwargs,
)
quiet = configs["quiet"]

Expand Down Expand Up @@ -160,8 +194,17 @@ def BlackVersion():

EndPython3

function black#Black()
:py3 Black()
function black#Black(...)
let kwargs = {}
for arg in a:000
let arg_list = split(arg, '=')
let kwargs[arg_list[0]] = arg_list[1]
endfor
python3 << EOF
import vim
kwargs = vim.eval("kwargs")
EOF
:py3 Black(**kwargs)
endfunction

function black#BlackUpgrade()
Expand Down
2 changes: 2 additions & 0 deletions docs/integrations/editors.md
Expand Up @@ -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=<version>` where `<version>` is one of
`[py27, py36, py37, py38, py39]` to set the target version.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: we should probably not advertise specific versions here. I mean it's fine, but seems like another place to eventually get stale as we add support and drop support for older versions. Should be fine just to mention that it's the same argument as in the CLI, right? It would be a bit of an indirection though. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fine just to mention that it's the same argument as in the CLI, right?

Yes, that would be better, and because of TAB completion, you get the correct list anyway. I'll change that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice 👌

- `:BlackUpgrade` to upgrade _Black_ inside the virtualenv;
- `:BlackVersion` to get the current version of _Black_ inside the virtualenv.

Expand Down
14 changes: 13 additions & 1 deletion plugin/black.vim
Expand Up @@ -53,8 +53,20 @@ endif
if !exists("g:black_quiet")
let g:black_quiet = 0
endif
if !exists("g:black_target_version")
let g:black_target_version = ""
endif

function BlackComplete(ArgLead, CmdLine, CursorPos)
return [
\ 'target_version=py27',
\ 'target_version=py36',
\ 'target_version=py37',
\ 'target_version=py38',
\ 'target_version=py39',
\ ]
endfunction

command! Black :call black#Black()
command! -nargs=* -complete=customlist,BlackComplete Black :call black#Black(<f-args>)
command! BlackUpgrade :call black#BlackUpgrade()
command! BlackVersion :call black#BlackVersion()