]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/fixers/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:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / fixers / black.vim
1 " Author: w0rp <devw0rp@gmail.com>
2 " Description: Fixing Python files with black.
3 "
4 call ale#Set('python_black_executable', 'black')
5 call ale#Set('python_black_use_global', get(g:, 'ale_use_global_executables', 0))
6 call ale#Set('python_black_options', '')
7 call ale#Set('python_black_auto_pipenv', 0)
8 call ale#Set('python_black_auto_poetry', 0)
9 call ale#Set('python_black_auto_uv', 0)
10 call ale#Set('python_black_change_directory', 1)
11
12 function! ale#fixers#black#GetExecutable(buffer) abort
13     if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_black_auto_pipenv'))
14     \ && ale#python#PipenvPresent(a:buffer)
15         return 'pipenv'
16     endif
17
18     if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_black_auto_poetry'))
19     \ && ale#python#PoetryPresent(a:buffer)
20         return 'poetry'
21     endif
22
23     if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_black_auto_uv'))
24     \ && ale#python#UvPresent(a:buffer)
25         return 'uv'
26     endif
27
28     return ale#python#FindExecutable(a:buffer, 'python_black', ['black'])
29 endfunction
30
31 function! ale#fixers#black#Fix(buffer) abort
32     let l:executable = ale#fixers#black#GetExecutable(a:buffer)
33     let l:cmd = [ale#Escape(l:executable)]
34
35     if l:executable =~? '\(pipenv\|poetry\|uv\)$'
36         call extend(l:cmd, ['run', 'black'])
37     endif
38
39     let l:options = ale#Var(a:buffer, 'python_black_options')
40
41     if !empty(l:options)
42         call add(l:cmd, l:options)
43     endif
44
45     let l:fname = expand('#' . a:buffer . '...')
46     call add(l:cmd, '--stdin-filename '.ale#Escape(ale#path#Simplify(l:fname)))
47
48     if expand('#' . a:buffer . ':e') is? 'pyi'
49         call add(l:cmd, '--pyi')
50     endif
51
52     call add(l:cmd, '-')
53
54     let l:result = {'command': join(l:cmd, ' ')}
55
56     if ale#Var(a:buffer, 'python_black_change_directory')
57         let l:result.cwd = '%s:h'
58     endif
59
60     return l:result
61 endfunction