]> git.madduck.net Git - etc/vim.git/blob - ale_linters/python/unimport.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 / unimport.vim
1 " Author: Author: Jon Parise <jon@indelible.org>
2
3 call ale#Set('python_unimport_executable', 'unimport')
4 call ale#Set('python_unimport_options', '')
5 call ale#Set('python_unimport_use_global', get(g:, 'ale_use_global_executables', 0))
6 call ale#Set('python_unimport_auto_pipenv', 0)
7 call ale#Set('python_unimport_auto_poetry', 0)
8 call ale#Set('python_unimport_auto_uv', 0)
9
10 function! ale_linters#python#unimport#GetExecutable(buffer) abort
11     if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_unimport_auto_pipenv'))
12     \ && ale#python#PipenvPresent(a:buffer)
13         return 'pipenv'
14     endif
15
16     if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_unimport_auto_poetry'))
17     \ && ale#python#PoetryPresent(a:buffer)
18         return 'poetry'
19     endif
20
21     if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_unimport_auto_uv'))
22     \ && ale#python#UvPresent(a:buffer)
23         return 'uv'
24     endif
25
26     return ale#python#FindExecutable(a:buffer, 'python_unimport', ['unimport'])
27 endfunction
28
29 function! ale_linters#python#unimport#GetCommand(buffer) abort
30     let l:executable = ale_linters#python#unimport#GetExecutable(a:buffer)
31     let l:exec_args = l:executable =~? '\(pipenv\|poetry\|uv\)$'
32     \   ? ' run unimport'
33     \   : ''
34
35     return '%e' . l:exec_args
36     \   . ale#Pad(ale#Var(a:buffer, 'python_unimport_options'))
37     \   . ' --check'
38     \   . ' %t'
39 endfunction
40
41
42 function! ale_linters#python#unimport#GetCwd(buffer) abort
43     let l:project_root = ale#python#FindProjectRoot(a:buffer)
44
45     return !empty(l:project_root)
46     \   ? l:project_root
47     \   : expand('#' . a:buffer . ':p:h')
48 endfunction
49
50
51 function! ale_linters#python#unimport#Handle(buffer, lines) abort
52     let l:output = ale#python#HandleTraceback(a:lines, 10)
53
54     if !empty(l:output)
55         return l:output
56     endif
57
58     " Matches lines like:
59     "
60     " urllib.parse at path/to/file.py:9
61     let l:pattern = '\v(.+) at [^:]+:(\d+)$'
62
63     for l:match in ale#util#GetMatches(a:lines, l:pattern)
64         call add(l:output, {
65         \   'lnum': l:match[2] + 0,
66         \   'type': 'W',
67         \   'text': 'unused: ' . l:match[1],
68         \})
69     endfor
70
71     return l:output
72 endfunction
73
74
75 call ale#linter#Define('python', {
76 \   'name': 'unimport',
77 \   'executable': function('ale_linters#python#unimport#GetExecutable'),
78 \   'cwd': function('ale_linters#python#unimport#GetCwd'),
79 \   'command': function('ale_linters#python#unimport#GetCommand'),
80 \   'callback': 'ale_linters#python#unimport#Handle',
81 \})