]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/ale_linters/python/refurb.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:

Merge commit '76265755a1add77121c8f9dabb3e9bb70fe9a972' as '.vim/bundle/ale'
[etc/vim.git] / .vim / bundle / ale / ale_linters / python / refurb.vim
1 " Author: Yining <zhang.yining@gmail.com>
2 " Description: refurb as linter for python files
3
4 call ale#Set('python_refurb_executable', 'refurb')
5 call ale#Set('python_refurb_options', '')
6 call ale#Set('python_refurb_use_global', get(g:, 'ale_use_global_executables', 0))
7 call ale#Set('python_refurb_change_directory', 1)
8 call ale#Set('python_refurb_auto_pipenv', 0)
9 call ale#Set('python_refurb_auto_poetry', 0)
10 call ale#Set('python_refurb_auto_uv', 0)
11
12 function! ale_linters#python#refurb#GetExecutable(buffer) abort
13     if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_refurb_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_refurb_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_refurb_auto_uv'))
24     \ && ale#python#UvPresent(a:buffer)
25         return 'uv'
26     endif
27
28     return ale#python#FindExecutable(a:buffer, 'python_refurb', ['refurb'])
29 endfunction
30
31 function! ale_linters#python#refurb#GetCwd(buffer) abort
32     if ale#Var(a:buffer, 'python_refurb_change_directory')
33         " Run from project root if found, else from buffer dir.
34         let l:project_root = ale#python#FindProjectRoot(a:buffer)
35
36         return !empty(l:project_root) ? l:project_root : '%s:h'
37     endif
38
39     return ''
40 endfunction
41
42 function! ale_linters#python#refurb#GetCommand(buffer) abort
43     let l:executable = ale_linters#python#refurb#GetExecutable(a:buffer)
44     let l:exec_args = l:executable =~? '\(pipenv\|poetry\|uv\)$'
45     \   ? ' run refurb'
46     \   : ''
47
48     return ale#Escape(l:executable) . l:exec_args
49     \   . ale#Pad(ale#Var(a:buffer, 'python_refurb_options'))
50     \   . ' %s'
51 endfunction
52
53 function! ale_linters#python#refurb#Handle(buffer, lines) abort
54     "Example: path/to/file.py:3:17 [FURB109]: Replace `in [x, y, z]` with `in (x, y, z)`
55     let l:pattern = '\v^[a-zA-Z]?:?[^:]+:(\d+):(\d+)?:?\s*\[FURB(\d+)\]:\s*(.+)$'
56     let l:output = []
57
58     for l:match in ale#util#GetMatches(a:lines, l:pattern)
59         call add(l:output, {
60         \   'lnum': l:match[1] + 0,
61         \   'col': l:match[2] + 0,
62         \   'code': l:match[3] + 0,
63         \   'text': l:match[4],
64         \   'type': 'W',
65         \})
66     endfor
67
68     return l:output
69 endfunction
70
71 call ale#linter#Define('python', {
72 \   'name': 'refurb',
73 \   'executable': function('ale_linters#python#refurb#GetExecutable'),
74 \   'cwd': function('ale_linters#python#refurb#GetCwd'),
75 \   'command':  function('ale_linters#python#refurb#GetCommand'),
76 \   'callback': 'ale_linters#python#refurb#Handle',
77 \   'output_stream': 'both',
78 \   'read_buffer': 0,
79 \})