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 " - restore cursor/window position after formatting
16 if v:version < 700 || !has('python3')
17 echo "This script requires vim7.0+ with Python 3.6 support."
21 if exists("g:load_black")
25 let g:load_black = "py1.0"
26 if !exists("g:black_virtualenv")
27 let g:black_virtualenv = "~/.vim/black"
29 if !exists("g:black_fast")
32 if !exists("g:black_linelength")
33 let g:black_linelength = 88
35 if !exists("g:black_skip_string_normalization")
36 let g:black_skip_string_normalization = 0
44 def _get_python_binary(exec_prefix):
46 default = vim.eval("g:pymode_python").strip()
49 if default and os.path.exists(default):
51 if sys.platform[:3] == "win":
52 return exec_prefix / 'python.exe'
53 return exec_prefix / 'bin' / 'python3'
55 def _get_pip(venv_path):
56 if sys.platform[:3] == "win":
57 return venv_path / 'Scripts' / 'pip.exe'
58 return venv_path / 'bin' / 'pip'
60 def _get_virtualenv_site_packages(venv_path, pyver):
61 if sys.platform[:3] == "win":
62 return venv_path / 'Lib' / 'site-packages'
63 return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages'
65 def _initialize_black_env(upgrade=False):
66 pyver = sys.version_info[:2]
68 print("Sorry, Black requires Python 3.6+ to run.")
71 from pathlib import Path
74 virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser()
75 virtualenv_site_packages = str(_get_virtualenv_site_packages(virtualenv_path, pyver))
77 if not virtualenv_path.is_dir():
78 print('Please wait, one time setup for Black.')
79 _executable = sys.executable
81 sys.executable = str(_get_python_binary(Path(sys.exec_prefix)))
82 print(f'Creating a virtualenv in {virtualenv_path}...')
83 print('(this path can be customized in .vimrc by setting g:black_virtualenv)')
84 venv.create(virtualenv_path, with_pip=True)
86 sys.executable = _executable
89 print('Installing Black with pip...')
91 print('Upgrading Black with pip...')
92 if first_install or upgrade:
93 subprocess.run([str(_get_pip(virtualenv_path)), 'install', '-U', 'black'], stdout=subprocess.PIPE)
94 print('DONE! You are all set, thanks for waiting ✨ 🍰 ✨')
96 print('Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.\n')
97 if sys.path[0] != virtualenv_site_packages:
98 sys.path.insert(0, virtualenv_site_packages)
101 if _initialize_black_env():
107 fast = bool(int(vim.eval("g:black_fast")))
108 mode = black.FileMode(
109 line_length=int(vim.eval("g:black_linelength")),
110 string_normalization=not bool(int(vim.eval("g:black_skip_string_normalization"))),
111 is_pyi=vim.current.buffer.name.endswith('.pyi'),
113 buffer_str = '\n'.join(vim.current.buffer) + '\n'
115 new_buffer_str = black.format_file_contents(buffer_str, fast=fast, mode=mode)
116 except black.NothingChanged:
117 print(f'Already well formatted, good job. (took {time.time() - start:.4f}s)')
118 except Exception as exc:
121 cursor = vim.current.window.cursor
122 vim.current.buffer[:] = new_buffer_str.split('\n')[:-1]
124 vim.current.window.cursor = cursor
126 vim.current.window.cursor = (len(vim.current.buffer), 0)
127 print(f'Reformatted in {time.time() - start:.4f}s.')
130 _initialize_black_env(upgrade=True)
133 print(f'Black, version {black.__version__} on Python {sys.version}.')
137 command! Black :py3 Black()
138 command! BlackUpgrade :py3 BlackUpgrade()
139 command! BlackVersion :py3 BlackVersion()