]> git.madduck.net Git - etc/vim.git/blob - ale_linters/python/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] / ale_linters / python / pycln.vim
1 " Author: Yining <zhang.yining@gmail.com>
2 " Description: pycln as linter 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_linters#python#pycln#GetExecutable(buffer) abort
14     if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_pycln_auto_pipenv'))
15     \ && ale#python#PipenvPresent(a:buffer)
16         return 'pipenv'
17     endif
18
19     if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_pycln_auto_poetry'))
20     \ && ale#python#PoetryPresent(a:buffer)
21         return 'poetry'
22     endif
23
24     if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_pycln_auto_uv'))
25     \ && ale#python#UvPresent(a:buffer)
26         return 'uv'
27     endif
28
29     return ale#python#FindExecutable(a:buffer, 'python_pycln', ['pycln'])
30 endfunction
31
32 function! ale_linters#python#pycln#GetCwd(buffer) abort
33     if ale#Var(a:buffer, 'python_pycln_change_directory')
34         " Run from project root if found, else from buffer dir.
35         let l:project_root = ale#python#FindProjectRoot(a:buffer)
36
37         return !empty(l:project_root) ? l:project_root : '%s:h'
38     endif
39
40     return ''
41 endfunction
42
43 function! ale_linters#python#pycln#GetCommand(buffer, version) abort
44     let l:executable = ale_linters#python#pycln#GetExecutable(a:buffer)
45     let l:exec_args = l:executable =~? '\(pipenv\|poetry\|uv\)$'
46     \   ? ' run pycln'
47     \   : ''
48
49     let l:options = ale#Var(a:buffer, 'python_pycln_options')
50     let l:config_file = ale#Var(a:buffer, 'python_pycln_config_file')
51     let l:config_file = l:options !~# '\v(^| )--config ' && !empty(l:config_file)
52     \   ? ale#Escape(ale#path#Simplify(l:config_file))
53     \   : ''
54
55     " NOTE: pycln version `1.3.0` supports liniting input from stdin
56     return ale#Escape(l:executable) . l:exec_args
57     \   . ale#Pad(ale#Var(a:buffer, 'python_pycln_options'))
58     \   . (empty(l:config_file) ? '' : ' --config ' . l:config_file)
59     \   . ' --check'
60     \   . (ale#semver#GTE(a:version, [1, 3, 0]) ? ' -' : ' %s')
61 endfunction
62
63 function! ale_linters#python#pycln#Handle(buffer, lines) abort
64     " Example: tmp/test.py:3:0 'import os' would be removed!
65     let l:pattern = '\v^[a-zA-Z]?:?[^:]+:(\d+):(\d+):? (.+)$'
66     let l:output = []
67
68     for l:match in ale#util#GetMatches(a:lines, l:pattern)
69         call add(l:output, {
70         \   'lnum': l:match[1] + 0,
71         \   'col': l:match[2] + 0,
72         \   'text': l:match[3],
73         \})
74     endfor
75
76     return l:output
77 endfunction
78
79 call ale#linter#Define('python', {
80 \   'name': 'pycln',
81 \   'executable': function('ale_linters#python#pycln#GetExecutable'),
82 \   'cwd': function('ale_linters#python#pycln#GetCwd'),
83 \   'command': {buffer -> ale#semver#RunWithVersionCheck(
84 \       buffer,
85 \       ale_linters#python#pycln#GetExecutable(buffer),
86 \       '%e --version',
87 \       function('ale_linters#python#pycln#GetCommand'),
88 \   )},
89 \   'callback': 'ale_linters#python#pycln#Handle',
90 \   'output_stream': 'both',
91 \   'read_buffer': 1,
92 \})