All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
3 " Created: Mon Mar 26 23:27:53 2018 -0700
4 " Requires: Vim Ver7.0+
8 " This plugin formats Python files.
14 if v:version < 700 || !has('python3')
15 echo "This script requires vim7.0+ with Python 3.6 support."
19 if exists("g:load_black")
23 let g:load_black = "py1.0"
24 if !exists("g:black_virtualenv")
25 let g:black_virtualenv = "~/.vim/black"
27 if !exists("g:black_fast")
30 if !exists("g:black_linelength")
31 let g:black_linelength = 88
38 def _find_python_binary(exec_prefix):
39 if sys.platform[:3] == "win":
40 return exec_prefix / 'python.exe'
41 return exec_prefix / 'bin' / 'python3'
43 def _find_pip(venv_path):
44 if sys.platform[:3] == "win":
45 return venv_path / 'Scripts' / 'pip.exe'
46 return venv_path / 'bin' / 'pip'
48 def _find_virtualenv_site_packages(venv_path):
49 if sys.platform[:3] == "win":
50 return venv_path / 'Lib' / 'site-packages'
51 return venv_path / 'Lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages'
53 def _initialize_black_env(upgrade=False):
54 pyver = sys.version_info[:2]
56 print("Sorry, Black requires Python 3.6+ to run.")
59 from pathlib import Path
62 virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser()
63 virtualenv_site_packages = str(_find_virtualenv_site_packages(virtualenv_path))
65 if not virtualenv_path.is_dir():
66 print('Please wait, one time setup for Black.')
67 _executable = sys.executable
69 sys.executable = str(_find_python_binary(Path(sys.exec_prefix)))
70 print(f'Creating a virtualenv in {virtualenv_path}...')
71 print('(this path can be customized in .vimrc by setting g:black_virtualenv)')
72 venv.create(virtualenv_path, with_pip=True)
74 sys.executable = _executable
77 print('Installing Black with pip...')
79 print('Upgrading Black with pip...')
80 if first_install or upgrade:
81 subprocess.run([str(_find_pip(virtualenv_path)), 'install', '-U', 'black'])
82 print('DONE! You are all set, thanks for waiting ✨ 🍰 ✨')
84 print('Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.\n')
85 if sys.path[0] != virtualenv_site_packages:
86 sys.path.insert(0, virtualenv_site_packages)
89 if _initialize_black_env():
95 fast = bool(int(vim.eval("g:black_fast")))
96 line_length = int(vim.eval("g:black_linelength"))
97 buffer_str = '\n'.join(vim.current.buffer) + '\n'
99 new_buffer_str = black.format_file_contents(buffer_str, line_length=line_length, fast=fast)
100 except black.NothingChanged:
101 print(f'Already well formatted, good job. (took {time.time() - start:.4f}s)')
102 except Exception as exc:
105 vim.current.buffer[:] = new_buffer_str.split('\n')[:-1]
106 print(f'Reformatted in {time.time() - start:.4f}s.')
109 _initialize_black_env(upgrade=True)
112 print(f'Black, version {black.__version__} on Python {sys.version}.')
116 command! Black :py3 Black()
117 command! BlackUpgrade :py3 BlackUpgrade()
118 command! BlackVersion :py3 BlackVersion()