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.
1 " Author: Yining <zhang.yining@gmail.com>
2 " Description: pycln as ALE fixer for python files
4 call ale#Set('python_pycln_executable', 'pycln')
5 call ale#Set('python_pycln_options', '')
6 call ale#Set('python_pycln_use_global', get(g:, 'ale_use_global_executables', 0))
7 call ale#Set('python_pycln_change_directory', 1)
8 call ale#Set('python_pycln_auto_pipenv', 0)
9 call ale#Set('python_pycln_auto_poetry', 0)
10 call ale#Set('python_pycln_auto_uv', 0)
11 call ale#Set('python_pycln_config_file', '')
13 function! ale#fixers#pycln#GetCwd(buffer) abort
14 if ale#Var(a:buffer, 'python_pycln_change_directory')
15 " Run from project root if found, else from buffer dir.
16 let l:project_root = ale#python#FindProjectRoot(a:buffer)
18 return !empty(l:project_root) ? l:project_root : '%s:h'
24 function! ale#fixers#pycln#GetExecutable(buffer) abort
25 if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_pycln_auto_pipenv'))
26 \ && ale#python#PipenvPresent(a:buffer)
30 if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_pycln_auto_poetry'))
31 \ && ale#python#PoetryPresent(a:buffer)
35 if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_pycln_auto_uv'))
36 \ && ale#python#UvPresent(a:buffer)
40 return ale#python#FindExecutable(a:buffer, 'python_pycln', ['pycln'])
43 function! ale#fixers#pycln#GetCommand(buffer) abort
44 let l:executable = ale#fixers#pycln#GetExecutable(a:buffer)
45 let l:exec_args = l:executable =~? '\(pipenv\|poetry\|uv\)$'
49 return ale#Escape(l:executable) . l:exec_args
52 function! ale#fixers#pycln#FixForVersion(buffer, version) abort
53 let l:executable = ale#fixers#pycln#GetExecutable(a:buffer)
54 let l:cmd = [ale#Escape(l:executable)]
56 if l:executable =~? '\(pipenv\|poetry\|uv\)$'
57 call extend(l:cmd, ['run', 'pycln'])
60 let l:options = ale#Var(a:buffer, 'python_pycln_options')
63 call add(l:cmd, l:options)
66 let l:config_file = ale#Var(a:buffer, 'python_pycln_config_file')
67 let l:config_file = l:options !~# '\v(^| )--config ' && !empty(l:config_file)
68 \ ? ale#Escape(ale#path#Simplify(l:config_file))
71 if !empty(l:config_file)
72 call add(l:cmd, '--config ' . l:config_file)
75 call add(l:cmd, '--silence')
77 " NOTE: pycln version `1.3.0` support reading from stdin
78 call add(l:cmd, ale#semver#GTE(a:version, [1, 3, 0]) ? '-' : '%s')
81 \ 'cwd': ale#fixers#pycln#GetCwd(a:buffer),
82 \ 'command': join(l:cmd, ' '),
86 function! ale#fixers#pycln#Fix(buffer) abort
87 let l:executable = ale#fixers#pycln#GetExecutable(a:buffer)
88 let l:command = ale#fixers#pycln#GetCommand(a:buffer) . ale#Pad('--version')
90 return ale#semver#RunWithVersionCheck(
94 \ function('ale#fixers#pycln#FixForVersion'),