Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix black vim plugin for 19.3b0
This patch is from psf/black@9fe4051
  • Loading branch information
goerz committed Aug 27, 2019
1 parent 1f85a4f commit 38cbd73
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions autoload/black.vim
Expand Up @@ -97,28 +97,32 @@ if _initialize_black_env():
def Black():
start = time.time()
fast = bool(int(vim.eval("g:black_fast")))
line_length = int(vim.eval("g:black_linelength"))
mode = black.FileMode.AUTO_DETECT
if bool(int(vim.eval("g:black_skip_string_normalization"))):
mode |= black.FileMode.NO_STRING_NORMALIZATION
if bool(int(vim.eval("g:black_skip_numeric_underscore_normalization"))):
mode |= black.FileMode.NO_NUMERIC_UNDERSCORE_NORMALIZATION
if vim.current.buffer.name.endswith(".pyi"):
mode |= black.FileMode.PYI
buffer_str = "\n".join(vim.current.buffer) + "\n"
mode = black.FileMode(
line_length=int(vim.eval("g:black_linelength")),
string_normalization=not bool(
int(vim.eval("g:black_skip_string_normalization"))
),
is_pyi=vim.current.buffer.name.endswith('.pyi'),
)
buffer_str = '\n'.join(vim.current.buffer) + '\n'
try:
new_buffer_str = black.format_file_contents(
buffer_str, line_length=line_length, fast=fast, mode=mode
buffer_str, fast=fast, mode=mode
)
except black.NothingChanged:
print(f"Already well formatted, good job. (took {time.time() - start:.4f}s)")
print(
f'Already well formatted, good job. (took {time.time() - start:.4f}s)'
)
except Exception as exc:
print(exc)
else:
cursor = vim.current.window.cursor
vim.current.buffer[:] = new_buffer_str.split("\n")[:-1]
vim.current.window.cursor = cursor
print(f"Reformatted in {time.time() - start:.4f}s.")
vim.current.buffer[:] = new_buffer_str.split('\n')[:-1]
try:
vim.current.window.cursor = cursor
except vim.error:
vim.current.window.cursor = (len(vim.current.buffer), 0)
print(f'Reformatted in {time.time() - start:.4f}s.')


def BlackUpgrade():
Expand Down

0 comments on commit 38cbd73

Please sign in to comment.