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.
1 " Author: w0rp <devw0rp@gmail.com>
2 " Description: flake8 for python files
4 call ale#Set('python_flake8_executable', 'flake8')
5 call ale#Set('python_flake8_options', '')
6 call ale#Set('python_flake8_use_global', get(g:, 'ale_use_global_executables', 0))
7 call ale#Set('python_flake8_change_directory', 'project')
8 call ale#Set('python_flake8_auto_pipenv', 0)
9 call ale#Set('python_flake8_auto_poetry', 0)
10 call ale#Set('python_flake8_auto_uv', 0)
12 function! s:UsingModule(buffer) abort
13 return ale#Var(a:buffer, 'python_flake8_options') =~# ' *-m flake8'
16 function! ale_linters#python#flake8#GetExecutable(buffer) abort
17 if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_flake8_auto_pipenv'))
18 \ && ale#python#PipenvPresent(a:buffer)
22 if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_flake8_auto_poetry'))
23 \ && ale#python#PoetryPresent(a:buffer)
27 if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_flake8_auto_uv'))
28 \ && ale#python#UvPresent(a:buffer)
32 if !s:UsingModule(a:buffer)
33 return ale#python#FindExecutable(a:buffer, 'python_flake8', ['flake8'])
36 return ale#Var(a:buffer, 'python_flake8_executable')
39 function! ale_linters#python#flake8#RunWithVersionCheck(buffer) abort
40 let l:executable = ale_linters#python#flake8#GetExecutable(a:buffer)
42 let l:module_string = s:UsingModule(a:buffer) ? ' -m flake8' : ''
43 let l:command = ale#Escape(l:executable) . l:module_string . ' --version'
45 return ale#semver#RunWithVersionCheck(
49 \ function('ale_linters#python#flake8#GetCommand'),
53 function! ale_linters#python#flake8#GetCwd(buffer) abort
54 let l:change_directory = ale#Var(a:buffer, 'python_flake8_change_directory')
57 if l:change_directory is# 'project'
58 let l:project_root = ale#python#FindProjectRootIni(a:buffer)
60 if !empty(l:project_root)
61 let l:cwd = l:project_root
65 if (l:change_directory is# 'project' && empty(l:cwd))
66 \|| l:change_directory
67 \|| l:change_directory is# 'file'
74 function! ale_linters#python#flake8#GetCommand(buffer, version) abort
75 let l:executable = ale_linters#python#flake8#GetExecutable(a:buffer)
77 let l:exec_args = l:executable =~? '\(pipenv\|poetry\|uv\)$'
81 " Only include the --stdin-display-name argument if we can parse the
82 " flake8 version, and it is recent enough to support it.
83 let l:display_name_args = ale#semver#GTE(a:version, [3, 0, 0])
84 \ ? ' --stdin-display-name %s'
87 let l:options = ale#Var(a:buffer, 'python_flake8_options')
89 return ale#Escape(l:executable) . l:exec_args
90 \ . (!empty(l:options) ? ' ' . l:options : '')
91 \ . ' --format=default'
92 \ . l:display_name_args . ' -'
95 let s:end_col_pattern_map = {
96 \ 'F405': '\(.\+\) may be undefined',
97 \ 'F821': 'undefined name ''\([^'']\+\)''',
98 \ 'F999': '^''\([^'']\+\)''',
99 \ 'F841': 'local variable ''\([^'']\+\)''',
102 function! ale_linters#python#flake8#Handle(buffer, lines) abort
103 let l:output = ale#python#HandleTraceback(a:lines, 10)
109 " Matches patterns line the following:
111 " Matches patterns line the following:
113 " stdin:6:6: E111 indentation is not a multiple of four
114 let l:pattern = '\v^[a-zA-Z]?:?[^:]+:(\d+):?(\d+)?: ([[:alnum:]]+):? (.*)$'
116 for l:match in ale#util#GetMatches(a:lines, l:pattern)
117 let l:code = l:match[3]
119 if (l:code is# 'W291' || l:code is# 'W293')
120 \ && !ale#Var(a:buffer, 'warn_about_trailing_whitespace')
121 " Skip warnings for trailing whitespace if the option is off.
126 \&& !ale#Var(a:buffer, 'warn_about_trailing_blank_lines')
127 " Skip warnings for trailing blank lines if the option is off
132 \ 'lnum': l:match[1] + 0,
133 \ 'col': l:match[2] + 0,
135 \ 'text': l:match[4],
140 if l:code[:0] is# 'F'
141 if l:code isnot# 'F401'
142 let l:item.type = 'E'
144 elseif l:code[:0] is# 'E'
145 let l:item.type = 'E'
147 if l:code isnot# 'E999' && l:code isnot# 'E112'
148 let l:item.sub_type = 'style'
150 elseif l:code[:0] is# 'W'
151 let l:item.sub_type = 'style'
154 let l:end_col_pattern = get(s:end_col_pattern_map, l:code, '')
156 if !empty(l:end_col_pattern)
157 let l:end_col_match = matchlist(l:match[4], l:end_col_pattern)
159 if !empty(l:end_col_match)
160 let l:item.end_col = l:item.col + len(l:end_col_match[1]) - 1
164 call add(l:output, l:item)
170 call ale#linter#Define('python', {
172 \ 'executable': function('ale_linters#python#flake8#GetExecutable'),
173 \ 'cwd': function('ale_linters#python#flake8#GetCwd'),
174 \ 'command': function('ale_linters#python#flake8#RunWithVersionCheck'),
175 \ 'callback': 'ale_linters#python#flake8#Handle',