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

Add a new option target_version while allows you to set your python verion (analogous to --target-version) #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion doc/black.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ For example:
```vim
let g:black#settings = {
\ 'fast': 1,
\ 'line_length': 100
\ 'line_length': 100,
\ 'target_version': 'py310'
\}
```

- `fast` (default: 0)
Set to a non-zero number to skip the AST check. This makes formatting a lot faster.
- `line_length` (default: 88)
Set to an integer to tell Black where to wrap lines.
- `target_version` (default: '')
Sets the `--target-version` flag in the commandline. Useful if you use the new match
syntax, as Black can only handle it when you set the version to 3.10 or above.
6 changes: 5 additions & 1 deletion doc/black.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ For example:
>
let g:black#settings = {
\ 'fast': 1,
\ 'line_length': 100
\ 'line_length': 100,
\ 'target_version': 'py310'
\}
<
* `fast` (default: 0)
Set to a non-zero number to skip the AST check. This makes formatting a lot
faster.
* `line_length` (default: 88)
Set to an integer to tell Black where to wrap lines.
* `target_version` (default: '')
Sets the `--target-version` flag in the commandline. Useful if you use the new match
syntax, as Black can only handle it when you set the version to 3.10 or above.

vim:ft=help:tw=80:ts=4:et:
50 changes: 44 additions & 6 deletions rplugin/python3/blacken.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

import time
import sys
from typing import List, Dict, Union
from typing import List, Dict, Union, Set

import pynvim

try:
import black
from black import TargetVersion
except ImportError:
print(
"[*] Black is not installed in your g:python3_host_prog, "
Expand All @@ -25,7 +26,7 @@ def __init__(self, nvim: pynvim.api.Nvim):

@pynvim.function("Black")
# args is not used but needs to be there to avoid an error.
def black(self, args: List[str]) -> None:
def black_(self, args: List[str]) -> None:
if self.n.current.buffer.options.get("filetype") != "python":
self.n.err_write("Not in a python file.\n")
return
Expand All @@ -37,23 +38,60 @@ def black(self, args: List[str]) -> None:

@pynvim.function("BlackSync", sync=True)
def blacksync(self, args: List[str]) -> None:
return self.black(args)
return self.black_(args)

def get_opts(self) -> Dict[str, Union[int, bool]]:
def get_opts(self) -> Dict[str, Union[int, bool, str]]:
options = {
"fast": False,
"line_length": 88,
"is_pyi": self.n.current.buffer.name.endswith(".pyi"),
"target_version": "",
}
user_options = self.n.vars.get("black#settings")
if user_options is not None:
options.update(user_options)
return options

def parse_target_version(self, target_version: str) -> Set[black.TargetVersion]:
self.n.err_write(repr(black))
if target_version.lower() == "":
return set()
elif target_version.lower() == "py33":
return {black.TargetVersion.PY33}
elif target_version.lower() == "py34":
return {black.TargetVersion.PY34}
elif target_version.lower() == "py35":
return {black.TargetVersion.PY35}
elif target_version.lower() == "py36":
return {black.TargetVersion.PY36}
elif target_version.lower() == "py37":
return {black.TargetVersion.PY37}
elif target_version.lower() == "py38":
return {black.TargetVersion.PY38}
elif target_version.lower() == "py39":
return {black.TargetVersion.PY39}
elif target_version.lower() == "py310":
return {black.TargetVersion.PY310}
elif target_version.lower() == "py311":
return {black.TargetVersion.PY311}
else:
self.n.err_write(
f"{target_version!r} is an invalid target_version. "
"Use 'py38' for python 3.8 for example.\n"
)
return set()

def format_buff(
self, to_format: str, opts: Dict[str, Union[int, bool]], start: float
self,
to_format: str,
opts: Dict[str, Union[int, bool, str]],
start: float,
) -> None:
mode = black.FileMode(line_length=opts["line_length"], is_pyi=opts["is_pyi"])
mode = black.FileMode(
line_length=opts["line_length"],
is_pyi=opts["is_pyi"],
target_versions=self.parse_target_version(opts["target_version"]),
)
try:
new_buffer = black.format_file_contents(
to_format, fast=opts["fast"], mode=mode
Expand Down