]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/python.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 / python.vim
1 " Author: w0rp <dev@w0rp.com>
2 " Description: Functions for integrating with Python linters.
3
4 call ale#Set('python_auto_pipenv', '0')
5 call ale#Set('python_auto_poetry', '0')
6 call ale#Set('python_auto_uv', '0')
7
8 let s:sep = has('win32') ? '\' : '/'
9 " bin is used for Unix virtualenv directories, and Scripts is for Windows.
10 let s:bin_dir = has('unix') ? 'bin' : 'Scripts'
11 " The default virtualenv directory names are ordered from the likely most
12 " common names down to the least common. `.env` might be more common, but it's
13 " also likely to conflict with a `.env` file for environment variables, so we
14 " search for it last. (People really shouldn't use that name.)
15 let g:ale_virtualenv_dir_names = get(g:, 'ale_virtualenv_dir_names', [
16 \   '.venv',
17 \   'env',
18 \   've',
19 \   'venv',
20 \   'virtualenv',
21 \   '.env',
22 \])
23
24 function! ale#python#FindProjectRootIni(buffer) abort
25     for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
26         " If you change this, update ale-python-root documentation.
27         if filereadable(l:path . '/MANIFEST.in')
28         \|| filereadable(l:path . '/setup.cfg')
29         \|| filereadable(l:path . '/pytest.ini')
30         \|| filereadable(l:path . '/tox.ini')
31         \|| filereadable(l:path . '/.pyre_configuration.local')
32         \|| filereadable(l:path . '/mypy.ini')
33         \|| filereadable(l:path . '/.mypy.ini')
34         \|| filereadable(l:path . '/pycodestyle.cfg')
35         \|| filereadable(l:path . '/.flake8')
36         \|| filereadable(l:path . '/.flake8rc')
37         \|| filereadable(l:path . '/pylama.ini')
38         \|| filereadable(l:path . '/pylintrc')
39         \|| filereadable(l:path . '/.pylintrc')
40         \|| filereadable(l:path . '/pyrightconfig.json')
41         \|| filereadable(l:path . '/pyrightconfig.toml')
42         \|| filereadable(l:path . '/Pipfile')
43         \|| filereadable(l:path . '/Pipfile.lock')
44         \|| filereadable(l:path . '/poetry.lock')
45         \|| filereadable(l:path . '/pyproject.toml')
46         \|| filereadable(l:path . '/.tool-versions')
47         \|| filereadable(l:path . '/uv.lock')
48             return l:path
49         endif
50     endfor
51
52     return ''
53 endfunction
54
55 " Given a buffer number, find the project root directory for Python.
56 " The root directory is defined as the first directory found while searching
57 " upwards through paths, including the current directory, until a path
58 " containing an init file (one from MANIFEST.in, setup.cfg, pytest.ini,
59 " tox.ini) is found. If it is not possible to find the project root directory
60 " via init file, then it will be defined as the first directory found
61 " searching upwards through paths, including the current directory, until no
62 " __init__.py files is found.
63 function! ale#python#FindProjectRoot(buffer) abort
64     let l:ini_root = ale#python#FindProjectRootIni(a:buffer)
65
66     if !empty(l:ini_root)
67         return l:ini_root
68     endif
69
70     for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
71         if !filereadable(l:path . '/__init__.py')
72             return l:path
73         endif
74     endfor
75
76     return ''
77 endfunction
78
79 " Given a buffer number, find a virtualenv path for Python.
80 function! ale#python#FindVirtualenv(buffer) abort
81     for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
82         " Skip empty path components returned in MSYS.
83         if empty(l:path)
84             continue
85         endif
86
87         for l:dirname in ale#Var(a:buffer, 'virtualenv_dir_names')
88             let l:venv_dir = ale#path#Simplify(
89             \   join([l:path, l:dirname], s:sep)
90             \)
91             let l:script_filename = ale#path#Simplify(
92             \   join([l:venv_dir, s:bin_dir, 'activate'], s:sep)
93             \)
94
95             if filereadable(l:script_filename)
96                 return l:venv_dir
97             endif
98         endfor
99     endfor
100
101     return $VIRTUAL_ENV
102 endfunction
103
104 " Automatically determine virtualenv environment variables and build
105 " a string of them to prefix linter commands with.
106 function! ale#python#AutoVirtualenvEnvString(buffer) abort
107     let l:venv_dir = ale#python#FindVirtualenv(a:buffer)
108
109     if !empty(l:venv_dir)
110         let l:strs = [ ]
111         " venv/bin directory
112         let l:pathdir = join([l:venv_dir, s:bin_dir], s:sep)
113
114         " expand PATH correctly inside of the appropriate shell.
115         " set VIRTUAL_ENV to point to venv
116         if has('win32')
117             call add(l:strs, 'set PATH=' . ale#Escape(l:pathdir) . ';%PATH% && ')
118             call add(l:strs, 'set VIRTUAL_ENV=' . ale#Escape(l:venv_dir) . ' && ')
119         else
120             call add(l:strs, 'PATH=' . ale#Escape(l:pathdir) . '":$PATH" ')
121             call add(l:strs, 'VIRTUAL_ENV=' . ale#Escape(l:venv_dir) . ' ')
122         endif
123
124         return join(l:strs, '')
125     endif
126
127     return ''
128 endfunction
129
130 " Given a buffer number and a command name, find the path to the executable.
131 " First search on a virtualenv for Python, if nothing is found, try the global
132 " command. Returns an empty string if cannot find the executable
133 function! ale#python#FindExecutable(buffer, base_var_name, path_list) abort
134     if ale#Var(a:buffer, a:base_var_name . '_use_global')
135         return ale#Var(a:buffer, a:base_var_name . '_executable')
136     endif
137
138     let l:virtualenv = ale#python#FindVirtualenv(a:buffer)
139
140     if !empty(l:virtualenv)
141         for l:path in a:path_list
142             let l:ve_executable = ale#path#Simplify(
143             \   join([l:virtualenv, s:bin_dir, l:path], s:sep)
144             \)
145
146             if executable(l:ve_executable)
147                 return l:ve_executable
148             endif
149         endfor
150     endif
151
152     return ale#Var(a:buffer, a:base_var_name . '_executable')
153 endfunction
154
155 " Handle traceback.print_exception() output starting in the first a:limit lines.
156 function! ale#python#HandleTraceback(lines, limit) abort
157     let l:nlines = len(a:lines)
158     let l:limit = a:limit > l:nlines ? l:nlines : a:limit
159     let l:start = 0
160
161     while l:start < l:limit
162         if a:lines[l:start] is# 'Traceback (most recent call last):'
163             break
164         endif
165
166         let l:start += 1
167     endwhile
168
169     if l:start >= l:limit
170         return []
171     endif
172
173     let l:end = l:start + 1
174
175     " Traceback entries are always prefixed with 2 spaces.
176     " SyntaxError marker (if present) is prefixed with at least 4 spaces.
177     " Final exc line starts with exception class name (never a space).
178     while l:end < l:nlines && a:lines[l:end][0] is# ' '
179         let l:end += 1
180     endwhile
181
182     let l:exc_line = l:end < l:nlines
183     \   ? a:lines[l:end]
184     \   : 'An exception was thrown.'
185
186     return [{
187     \   'lnum': 1,
188     \   'text': l:exc_line . ' (See :ALEDetail)',
189     \   'detail': join(a:lines[(l:start):(l:end)], "\n"),
190     \}]
191 endfunction
192
193 " Detects whether a pipenv environment is present.
194 function! ale#python#PipenvPresent(buffer) abort
195     return findfile('Pipfile.lock', expand('#' . a:buffer . ':p:h') . ';') isnot# ''
196 endfunction
197
198 " Detects whether a poetry environment is present.
199 function! ale#python#PoetryPresent(buffer) abort
200     return findfile('poetry.lock', expand('#' . a:buffer . ':p:h') . ';') isnot# ''
201 endfunction
202
203 " Detects whether a poetry environment is present.
204 function! ale#python#UvPresent(buffer) abort
205     return findfile('uv.lock', expand('#' . a:buffer . ':p:h') . ';') isnot# ''
206 endfunction