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: flakehell for python files
4 call ale#Set('python_flakehell_executable', 'flakehell')
5 call ale#Set('python_flakehell_options', '')
6 call ale#Set('python_flakehell_use_global', get(g:, 'ale_use_global_executables', 0))
7 call ale#Set('python_flakehell_change_directory', 'project')
8 call ale#Set('python_flakehell_auto_pipenv', 0)
9 call ale#Set('python_flakehell_auto_poetry', 0)
10 call ale#Set('python_flakehell_auto_uv', 0)
12 function! s:UsingModule(buffer) abort
13 return ale#Var(a:buffer, 'python_flakehell_executable') is? 'python'
16 function! ale_linters#python#flakehell#GetExecutable(buffer) abort
17 if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_flakehell_auto_pipenv'))
18 \ && ale#python#PipenvPresent(a:buffer)
22 if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_flakehell_auto_poetry'))
23 \ && ale#python#PoetryPresent(a:buffer)
27 if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_flakehell_auto_uv'))
28 \ && ale#python#UvPresent(a:buffer)
32 if !s:UsingModule(a:buffer)
33 return ale#python#FindExecutable(a:buffer, 'python_flakehell', ['flakehell'])
36 return ale#Var(a:buffer, 'python_flakehell_executable')
39 function! ale_linters#python#flakehell#RunWithVersionCheck(buffer) abort
40 let l:executable = ale_linters#python#flakehell#GetExecutable(a:buffer)
42 let l:module_string = s:UsingModule(a:buffer) ? ' -m flakehell' : ''
43 let l:command = ale#Escape(l:executable) . l:module_string . ' --version'
45 return ale#semver#RunWithVersionCheck(
49 \ function('ale_linters#python#flakehell#GetCommand'),
53 function! ale_linters#python#flakehell#GetCwd(buffer) abort
54 let l:change_directory = ale#Var(a:buffer, 'python_flakehell_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#flakehell#GetCommand(buffer, version) abort
75 let l:executable = ale_linters#python#flakehell#GetExecutable(a:buffer)
77 if (l:executable =~? '\(pipenv\|poetry\|uv\)$')
78 let l:exec_args = ' run flakehell'
79 elseif (l:executable is? 'python')
80 let l:exec_args = ' -m flakehell'
85 " Only include the --stdin-display-name argument if we can parse the
86 " flakehell version, and it is recent enough to support it.
87 let l:display_name_args = ale#semver#GTE(a:version, [0, 8, 0])
88 \ ? ' --stdin-display-name %s'
91 let l:options = ale#Var(a:buffer, 'python_flakehell_options')
93 return ale#Escape(l:executable)
95 \ . (!empty(l:options) ? ' lint ' . l:options : ' lint')
96 \ . ' --format=default'
97 \ . l:display_name_args . ' -'
100 let s:end_col_pattern_map = {
101 \ 'F405': '\(.\+\) may be undefined',
102 \ 'F821': 'undefined name ''\([^'']\+\)''',
103 \ 'F999': '^''\([^'']\+\)''',
104 \ 'F841': 'local variable ''\([^'']\+\)''',
107 function! ale_linters#python#flakehell#Handle(buffer, lines) abort
108 let l:output = ale#python#HandleTraceback(a:lines, 10)
114 " Matches patterns line the following:
116 " Matches patterns line the following:
118 " stdin:6:6: E111 indentation is not a multiple of four
119 let l:pattern = '\v^[a-zA-Z]?:?[^:]+:(\d+):?(\d+)?: ([[:alnum:]]+):? (.*)$'
121 for l:match in ale#util#GetMatches(a:lines, l:pattern)
122 let l:code = l:match[3]
124 if (l:code is# 'W291' || l:code is# 'W293')
125 \ && !ale#Var(a:buffer, 'warn_about_trailing_whitespace')
126 " Skip warnings for trailing whitespace if the option is off.
131 \&& !ale#Var(a:buffer, 'warn_about_trailing_blank_lines')
132 " Skip warnings for trailing blank lines if the option is off
137 \ 'lnum': l:match[1] + 0,
138 \ 'col': l:match[2] + 0,
140 \ 'text': l:match[4],
145 if l:code[:0] is# 'F'
146 if l:code isnot# 'F401'
147 let l:item.type = 'E'
149 elseif l:code[:0] is# 'E'
150 let l:item.type = 'E'
152 if l:code isnot# 'E999' && l:code isnot# 'E112'
153 let l:item.sub_type = 'style'
155 elseif l:code[:0] is# 'W'
156 let l:item.sub_type = 'style'
159 let l:end_col_pattern = get(s:end_col_pattern_map, l:code, '')
161 if !empty(l:end_col_pattern)
162 let l:end_col_match = matchlist(l:match[4], l:end_col_pattern)
164 if !empty(l:end_col_match)
165 let l:item.end_col = l:item.col + len(l:end_col_match[1]) - 1
169 call add(l:output, l:item)
175 call ale#linter#Define('python', {
176 \ 'name': 'flakehell',
177 \ 'executable': function('ale_linters#python#flakehell#GetExecutable'),
178 \ 'cwd': function('ale_linters#python#flakehell#GetCwd'),
179 \ 'command': function('ale_linters#python#flakehell#RunWithVersionCheck'),
180 \ 'callback': 'ale_linters#python#flakehell#Handle',