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 "The black.vim plugin 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")
28 let g:black_virtualenv = "~/.local/share/nvim/black"
30 let g:black_virtualenv = "~/.vim/black"
33 if !exists("g:black_fast")
36 if !exists("g:black_linelength")
37 let g:black_linelength = 88
39 if !exists("g:black_skip_string_normalization")
40 let g:black_skip_string_normalization = 0
50 class Flag(collections.namedtuple("FlagBase", "name, cast")):
53 return self.name.replace("-", "_")
56 def vim_rc_name(self):
58 if name == "line_length":
59 name = name.replace("_", "")
60 if name == "string_normalization":
62 return "g:black_" + name
66 Flag(name="line_length", cast=int),
67 Flag(name="fast", cast=bool),
68 Flag(name="string_normalization", cast=bool),
72 def _get_python_binary(exec_prefix):
74 default = vim.eval("g:pymode_python").strip()
77 if default and os.path.exists(default):
79 if sys.platform[:3] == "win":
80 return exec_prefix / 'python.exe'
81 return exec_prefix / 'bin' / 'python3'
83 def _get_pip(venv_path):
84 if sys.platform[:3] == "win":
85 return venv_path / 'Scripts' / 'pip.exe'
86 return venv_path / 'bin' / 'pip'
88 def _get_virtualenv_site_packages(venv_path, pyver):
89 if sys.platform[:3] == "win":
90 return venv_path / 'Lib' / 'site-packages'
91 return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages'
93 def _initialize_black_env(upgrade=False):
94 pyver = sys.version_info[:2]
96 print("Sorry, Black requires Python 3.6+ to run.")
99 from pathlib import Path
102 virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser()
103 virtualenv_site_packages = str(_get_virtualenv_site_packages(virtualenv_path, pyver))
104 first_install = False
105 if not virtualenv_path.is_dir():
106 print('Please wait, one time setup for Black.')
107 _executable = sys.executable
109 sys.executable = str(_get_python_binary(Path(sys.exec_prefix)))
110 print(f'Creating a virtualenv in {virtualenv_path}...')
111 print('(this path can be customized in .vimrc by setting g:black_virtualenv)')
112 venv.create(virtualenv_path, with_pip=True)
114 sys.executable = _executable
117 print('Installing Black with pip...')
119 print('Upgrading Black with pip...')
120 if first_install or upgrade:
121 subprocess.run([str(_get_pip(virtualenv_path)), 'install', '-U', 'black'], stdout=subprocess.PIPE)
122 print('DONE! You are all set, thanks for waiting ✨ 🍰 ✨')
124 print('Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.\n')
125 if virtualenv_site_packages not in sys.path:
126 sys.path.append(virtualenv_site_packages)
129 if _initialize_black_env():
135 configs = get_configs()
136 mode = black.FileMode(
137 line_length=configs["line_length"],
138 string_normalization=configs["string_normalization"],
139 is_pyi=vim.current.buffer.name.endswith('.pyi'),
142 buffer_str = '\n'.join(vim.current.buffer) + '\n'
144 new_buffer_str = black.format_file_contents(
146 fast=configs["fast"],
149 except black.NothingChanged:
150 print(f'Already well formatted, good job. (took {time.time() - start:.4f}s)')
151 except Exception as exc:
154 current_buffer = vim.current.window.buffer
156 for i, tabpage in enumerate(vim.tabpages):
158 for j, window in enumerate(tabpage.windows):
159 if window.valid and window.buffer == current_buffer:
160 cursors.append((i, j, window.cursor))
161 vim.current.buffer[:] = new_buffer_str.split('\n')[:-1]
162 for i, j, cursor in cursors:
163 window = vim.tabpages[i].windows[j]
165 window.cursor = cursor
167 window.cursor = (len(window.buffer), 0)
168 print(f'Reformatted in {time.time() - start:.4f}s.')
171 path_pyproject_toml = black.find_pyproject_toml(vim.eval("fnamemodify(getcwd(), ':t')"))
172 if path_pyproject_toml:
173 toml_config = black.parse_pyproject_toml(path_pyproject_toml)
178 flag.var_name: toml_config.get(flag.name, flag.cast(vim.eval(flag.vim_rc_name)))
184 _initialize_black_env(upgrade=True)
187 print(f'Black, version {black.__version__} on Python {sys.version}.')
191 command! Black :py3 Black()
192 command! BlackUpgrade :py3 BlackUpgrade()
193 command! BlackVersion :py3 BlackVersion()