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.
2 " Python filetype plugin for running flake8
3 " Language: Python (ft=python)
4 " Maintainer: Vincent Driessen <vincent@3rdcloud.com>
5 " Version: Vim 7 (may work with lower Vim versions, but not tested)
6 " URL: http://github.com/nvie/vim-flake8
13 function! flake8#Flake8()
18 function! flake8#Flake8UnplaceMarkers()
19 call s:UnplaceMarkers()
23 function! flake8#ShowErrorMessage()
24 call s:ShowErrorMessage()
33 let s:displayed_warnings = 0
34 function! s:Warnings()
35 if !s:displayed_warnings
36 let l:show_website_url = 0
38 let l:msg = "has been deprecated in favour of flake8 config files"
39 for setting_name in ['g:flake8_ignore', 'g:flake8_builtins', 'g:flake8_max_line_length', 'g:flake8_max_complexity']
40 if exists(setting_name)
41 echohl WarningMsg | echom setting_name l:msg | echohl None
42 let l:show_website_url = 1
47 let l:url = "http://flake8.readthedocs.org/en/latest/config.html"
48 echohl WarningMsg | echom l:url | echohl None
50 let s:displayed_warnings = 1
56 function! s:DeclareOption(name, globalPrefix, default) " {{{
57 if !exists('g:'.a:name)
59 execute 'let s:'.a:name.'='.a:default
61 execute 'let s:'.a:name.'=""'
64 execute 'let l:global="g:".a:name'
66 execute 'let s:'.a:name.'="'.a:globalPrefix.'".g:'.a:name
68 execute 'let s:'.a:name.'=""'
73 function! s:Setup() " {{{
77 call s:DeclareOption('flake8_cmd', '', '"flake8"')
79 call s:DeclareOption('flake8_quickfix_location', '', '"belowright"')
80 call s:DeclareOption('flake8_quickfix_height', '', 5)
81 call s:DeclareOption('flake8_show_quickfix', '', 1)
83 call s:DeclareOption('flake8_show_in_gutter', '', 0)
84 call s:DeclareOption('flake8_show_in_file', '', 0)
85 call s:DeclareOption('flake8_max_markers', '', 500)
87 call s:DeclareOption('flake8_error_marker', '', '"E>"')
88 call s:DeclareOption('flake8_warning_marker', '', '"W>"')
89 call s:DeclareOption('flake8_pyflake_marker', '', '"F>"')
90 call s:DeclareOption('flake8_complexity_marker', '', '"C>"')
91 call s:DeclareOption('flake8_naming_marker', '', '"N>"')
95 if !exists('s:markerdata')
97 let s:markerdata['E'] = {'name': 'Flake8_Error'}
98 let s:markerdata['W'] = {'name': 'Flake8_Warning'}
99 let s:markerdata['F'] = {'name': 'Flake8_PyFlake'}
100 let s:markerdata['C'] = {'name': 'Flake8_Complexity'}
101 let s:markerdata['N'] = {'name': 'Flake8_Nameing'}
103 let s:markerdata['E'].marker = s:flake8_error_marker
104 let s:markerdata['W'].marker = s:flake8_warning_marker
105 let s:markerdata['F'].marker = s:flake8_pyflake_marker
106 let s:markerdata['C'].marker = s:flake8_complexity_marker
107 let s:markerdata['N'].marker = s:flake8_naming_marker
115 function! s:Flake8() " {{{
119 let l:executable = split(s:flake8_cmd)[0]
121 if !executable(l:executable)
122 echoerr "File " . l:executable . " not found. Please install it first."
127 call s:UnplaceMarkers()
131 " store old grep settings (to restore later)
132 let l:old_gfm=&grepformat
133 let l:old_gp=&grepprg
134 let l:old_shellpipe=&shellpipe
138 " write any changes before continuing
143 set lazyredraw " delay redrawing
145 " prevent terminal from blinking
150 " perform the grep itself
151 let &grepformat="%f:%l:%c: %m\,%f:%l: %m"
152 let &grepprg=s:flake8_cmd
154 " close any existing cwindows,
155 " placed after 'grep' in case quickfix is open on autocmd QuickFixCmdPost
158 " restore grep settings
159 let &grepformat=l:old_gfm
160 let &grepprg=l:old_gp
161 let &shellpipe=l:old_shellpipe
164 " store mapping of line number to error string
167 let s:resultDict = {}
169 let l:results=getqflist()
170 let l:has_results=results != []
172 " save line number of each error message
173 for result in a:results:
174 s:resultDict[result.lnum] = result.text
177 if !s:flake8_show_in_gutter == 0 || !s:flake8_show_in_file == 0
178 call s:PlaceMarkers(l:results)
181 if !s:flake8_show_quickfix == 0
183 execute s:flake8_quickfix_location." copen".s:flake8_quickfix_height
185 nnoremap <buffer> <silent> c :cclose<CR>
186 nnoremap <buffer> <silent> q :cclose<CR>
194 if l:has_results == 0
195 echon "Flake8 check OK"
197 echon "Flake8 found issues"
202 function! s:PlaceMarkers(results) " {{{
204 if !s:flake8_show_in_gutter == 0
206 for val in values(s:markerdata)
208 execute "sign define ".val.name." text=".val.marker." texthl=".val.name
215 let l:index = l:index0
216 for result in a:results
217 if l:index >= (s:flake8_max_markers+l:index0)
220 let l:type = strpart(result.text, 0, 1)
221 if has_key(s:markerdata, l:type) && s:markerdata[l:type].marker != ''
223 if !s:flake8_show_in_file == 0
224 if !has_key(s:markerdata[l:type], 'matchstr')
225 let s:markerdata[l:type].matchstr = '\%('
227 let s:markerdata[l:type].matchstr .= '\|'
229 let s:markerdata[l:type].matchstr .= '\%'.result.lnum.'l\%'.result.col.'c'
232 if !s:flake8_show_in_gutter == 0
233 execute ":sign place ".index." name=".s:markerdata[l:type].name
234 \ . " line=".result.lnum." file=".expand("%:p")
235 let s:signids += [l:index]
242 if !s:flake8_show_in_file == 0
243 for l:val in values(s:markerdata)
244 if l:val.marker != '' && has_key(l:val, 'matchstr')
245 let l:val.matchid = matchadd(l:val.name, l:val.matchstr.'\)')
251 function! s:UnplaceMarkers() " {{{
253 if exists('s:signids')
255 execute ":sign unplace ".i
260 for l:val in values(s:markerdata)
261 if has_key(l:val, 'matchid')
262 call matchdelete(l:val.matchid)
269 function! s:ShowErrorMessage() " {{{
270 let l:cursorPos = getpos(".")
271 if !exists('s:resultDict')
275 " if there is a message on the current line,
277 if has_key(s:matchDict, s:cursorPos[1])
278 let l:errorText = get(s:matchDict, l:cursorPos[1])
279 echo strpart(l:errorText, 0, &columns-1)
280 let b:showing_message = 1
283 " if a message is already being shown,
285 if !b:showing_message == 0
287 let b:showing_message = 0
294 let &cpo = s:save_cpo