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.
8 if text.lower() in ['y', 'yes', 't', 'true' 'on', '1']:
10 if text.lower() in ['n', 'no', 'f', 'false' 'off', '0']:
12 raise ValueError(f"{text} is not convertable to boolean")
14 class Flag(collections.namedtuple("FlagBase", "name, cast")):
17 return self.name.replace("-", "_")
20 def vim_rc_name(self):
22 if name == "line_length":
23 name = name.replace("_", "")
24 return "g:black_" + name
28 Flag(name="line_length", cast=int),
29 Flag(name="fast", cast=strtobool),
30 Flag(name="skip_string_normalization", cast=strtobool),
31 Flag(name="quiet", cast=strtobool),
35 def _get_python_binary(exec_prefix):
37 default = vim.eval("g:pymode_python").strip()
40 if default and os.path.exists(default):
42 if sys.platform[:3] == "win":
43 return exec_prefix / 'python.exe'
44 return exec_prefix / 'bin' / 'python3'
46 def _get_pip(venv_path):
47 if sys.platform[:3] == "win":
48 return venv_path / 'Scripts' / 'pip.exe'
49 return venv_path / 'bin' / 'pip'
51 def _get_virtualenv_site_packages(venv_path, pyver):
52 if sys.platform[:3] == "win":
53 return venv_path / 'Lib' / 'site-packages'
54 return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages'
56 def _initialize_black_env(upgrade=False):
57 pyver = sys.version_info[:3]
59 print("Sorry, Black requires Python 3.6.2+ to run.")
62 from pathlib import Path
65 virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser()
66 virtualenv_site_packages = str(_get_virtualenv_site_packages(virtualenv_path, pyver))
68 if not virtualenv_path.is_dir():
69 print('Please wait, one time setup for Black.')
70 _executable = sys.executable
71 _base_executable = getattr(sys, "_base_executable", _executable)
73 executable = str(_get_python_binary(Path(sys.exec_prefix)))
74 sys.executable = executable
75 sys._base_executable = executable
76 print(f'Creating a virtualenv in {virtualenv_path}...')
77 print('(this path can be customized in .vimrc by setting g:black_virtualenv)')
78 venv.create(virtualenv_path, with_pip=True)
80 print('Encountered exception while creating virtualenv (see traceback below).')
81 print(f'Removing {virtualenv_path}...')
83 shutil.rmtree(virtualenv_path)
86 sys.executable = _executable
87 sys._base_executable = _base_executable
90 print('Installing Black with pip...')
92 print('Upgrading Black with pip...')
93 if first_install or upgrade:
94 subprocess.run([str(_get_pip(virtualenv_path)), 'install', '-U', 'black'], stdout=subprocess.PIPE)
95 print('DONE! You are all set, thanks for waiting ✨ 🍰 ✨')
97 print('Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.\n')
98 if virtualenv_site_packages not in sys.path:
99 sys.path.insert(0, virtualenv_site_packages)
102 if _initialize_black_env():
106 def get_target_version(tv):
107 if isinstance(tv, black.TargetVersion):
111 ret = black.TargetVersion[tv.upper()]
113 print(f"WARNING: Target version {tv!r} not recognized by Black, using default target")
118 kwargs allows you to override ``target_versions`` argument of
121 ``target_version`` needs to be cleaned because ``black.FileMode``
122 expects the ``target_versions`` argument to be a set of TargetVersion enums.
124 Allow kwargs["target_version"] to be a string to allow
125 to type it more quickly.
127 Using also target_version instead of target_versions to remain
128 consistent to Black's documentation of the structure of pyproject.toml.
131 configs = get_configs()
134 if "target_version" in kwargs:
135 target_version = kwargs["target_version"]
137 if not isinstance(target_version, (list, set)):
138 target_version = [target_version]
139 target_version = set(filter(lambda x: x, map(lambda tv: get_target_version(tv), target_version)))
140 black_kwargs["target_versions"] = target_version
142 mode = black.FileMode(
143 line_length=configs["line_length"],
144 string_normalization=not configs["skip_string_normalization"],
145 is_pyi=vim.current.buffer.name.endswith('.pyi'),
148 quiet = configs["quiet"]
150 buffer_str = '\n'.join(vim.current.buffer) + '\n'
152 new_buffer_str = black.format_file_contents(
154 fast=configs["fast"],
157 except black.NothingChanged:
159 print(f'Already well formatted, good job. (took {time.time() - start:.4f}s)')
160 except Exception as exc:
163 current_buffer = vim.current.window.buffer
165 for i, tabpage in enumerate(vim.tabpages):
167 for j, window in enumerate(tabpage.windows):
168 if window.valid and window.buffer == current_buffer:
169 cursors.append((i, j, window.cursor))
170 vim.current.buffer[:] = new_buffer_str.split('\n')[:-1]
171 for i, j, cursor in cursors:
172 window = vim.tabpages[i].windows[j]
174 window.cursor = cursor
176 window.cursor = (len(window.buffer), 0)
178 print(f'Reformatted in {time.time() - start:.4f}s.')
181 filename = vim.eval("@%")
182 path_pyproject_toml = black.find_pyproject_toml((filename,))
183 if path_pyproject_toml:
184 toml_config = black.parse_pyproject_toml(path_pyproject_toml)
189 flag.var_name: toml_config.get(flag.name, flag.cast(vim.eval(flag.vim_rc_name)))
195 _initialize_black_env(upgrade=True)
198 print(f'Black, version {black.__version__} on Python {sys.version}.')
202 function black#Black(...)
205 let arg_list = split(arg, '=')
206 let kwargs[arg_list[0]] = arg_list[1]
210 kwargs = vim.eval("kwargs")
215 function black#BlackUpgrade()
219 function black#BlackVersion()