]> git.madduck.net Git - etc/vim.git/blob - vim/plugin/black.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Add integration for pre-commit.com (#104)
[etc/vim.git] / vim / plugin / black.vim
1 " black.vim
2 " Author: Łukasz Langa
3 " Created: Mon Mar 26 23:27:53 2018 -0700
4 " Requires: Vim Ver7.0+
5 " Version:  1.0
6 "
7 " Documentation:
8 "   This plugin formats Python files.
9 "
10 " History:
11 "  1.0:
12 "    - initial version
13
14 if v:version < 700 || !has('python3')
15     echo "This script requires vim7.0+ with Python 3.6 support."
16     finish
17 endif
18
19 if exists("g:load_black")
20    finish
21 endif
22
23 let g:load_black = "py1.0"
24 if !exists("g:black_virtualenv")
25   let g:black_virtualenv = "~/.vim/black"
26 endif
27 if !exists("g:black_fast")
28   let g:black_fast = 0
29 endif
30 if !exists("g:black_linelength")
31   let g:black_linelength = 88
32 endif
33
34 python3 << endpython3
35 import sys
36 import vim
37
38 def _initialize_black_env(upgrade=False):
39   pyver = sys.version_info[:2]
40   if pyver < (3, 6):
41     print("Sorry, Black requires Python 3.6+ to run.")
42     return False
43
44   from pathlib import Path
45   import subprocess
46   import venv
47   virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser()
48   virtualenv_site_packages = str(
49     virtualenv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages'
50   )
51   first_install = False
52   if not virtualenv_path.is_dir():
53     print('Please wait, one time setup for Black.')
54     _executable = sys.executable
55     try:
56       sys.executable = str(Path(sys.exec_prefix) / 'bin' / 'python3')
57       print(f'Creating a virtualenv in {virtualenv_path}...')
58       print('(this path can be customized in .vimrc by setting g:black_virtualenv)')
59       venv.create(virtualenv_path, with_pip=True)
60     finally:
61       sys.executable = _executable
62     first_install = True
63   if first_install:
64     print('Installing Black with pip...')
65   if upgrade:
66     print('Upgrading Black with pip...')
67   if first_install or upgrade:
68     subprocess.run([str(virtualenv_path / 'bin' / 'pip'), 'install', '-U', 'black'])
69     print('DONE! You are all set, thanks for waiting ✨ 🍰 ✨')
70   if first_install:
71     print('Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.\n')
72   if sys.path[0] != virtualenv_site_packages:
73     sys.path.insert(0, virtualenv_site_packages)
74   return True
75
76 if _initialize_black_env():
77   import black
78   import time
79
80 def Black():
81   start = time.time()
82   fast = bool(int(vim.eval("g:black_fast")))
83   line_length = int(vim.eval("g:black_linelength"))
84   buffer_str = '\n'.join(vim.current.buffer) + '\n'
85   try:
86     new_buffer_str = black.format_file_contents(buffer_str, line_length=line_length, fast=fast)
87   except black.NothingChanged:
88     print(f'Already well formatted, good job. (took {time.time() - start:.4f}s)')
89   except Exception as exc:
90     print(exc)
91   else:
92     vim.current.buffer[:] = new_buffer_str.split('\n')[:-1]
93     print(f'Reformatted in {time.time() - start:.4f}s.')
94
95 def BlackUpgrade():
96   _initialize_black_env(upgrade=True)
97
98 def BlackVersion():
99   print(f'Black, version {black.__version__} on Python {sys.version}.')
100
101 endpython3
102
103 command! Black :py3 Black()
104 command! BlackUpgrade :py3 BlackUpgrade()
105 command! BlackVersion :py3 BlackVersion()
106
107 nmap ,= :Black<CR>
108 vmap ,= :Black<CR>