]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/fixers/pycln.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 / pycln.vim
1 " Author: Yining <zhang.yining@gmail.com>
2 " Description: pycln as ALE fixer for python files
3
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', '')
12
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)
17
18         return !empty(l:project_root) ? l:project_root : '%s:h'
19     endif
20
21     return '%s:h'
22 endfunction
23
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)
27         return 'pipenv'
28     endif
29
30     if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_pycln_auto_poetry'))
31     \ && ale#python#PoetryPresent(a:buffer)
32         return 'poetry'
33     endif
34
35     if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_pycln_auto_uv'))
36     \ && ale#python#UvPresent(a:buffer)
37         return 'uv'
38     endif
39
40     return ale#python#FindExecutable(a:buffer, 'python_pycln', ['pycln'])
41 endfunction
42
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\)$'
46     \   ? ' run pycln'
47     \   : ''
48
49     return ale#Escape(l:executable) . l:exec_args
50 endfunction
51
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)]
55
56     if l:executable =~? '\(pipenv\|poetry\|uv\)$'
57         call extend(l:cmd, ['run', 'pycln'])
58     endif
59
60     let l:options = ale#Var(a:buffer, 'python_pycln_options')
61
62     if !empty(l:options)
63         call add(l:cmd, l:options)
64     endif
65
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))
69     \   : ''
70
71     if !empty(l:config_file)
72         call add(l:cmd, '--config ' . l:config_file)
73     endif
74
75     call add(l:cmd, '--silence')
76
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')
79
80     return {
81     \   'cwd': ale#fixers#pycln#GetCwd(a:buffer),
82     \   'command': join(l:cmd, ' '),
83     \}
84 endfunction
85
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')
89
90     return ale#semver#RunWithVersionCheck(
91     \     a:buffer,
92     \     l:executable,
93     \     l:command,
94     \     function('ale#fixers#pycln#FixForVersion'),
95     \)
96 endfunction