]> git.madduck.net Git - etc/vim.git/blob - autoload/flake8.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:

Prevent terminal from blinking
[etc/vim.git] / autoload / flake8.vim
1 "
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
7
8 let s:save_cpo = &cpo
9 set cpo&vim
10
11 "" ** external ** {{{
12
13 function! flake8#Flake8()
14     call s:Flake8()
15     call s:Warnings()
16 endfunction
17
18 function! flake8#Flake8UnplaceMarkers()
19     call s:UnplaceMarkers()
20     call s:Warnings()
21 endfunction
22
23 "" }}}
24
25 "" ** internal ** {{{
26
27 "" warnings 
28
29 let s:displayed_warnings = 0
30 function s:Warnings()
31   if !s:displayed_warnings
32     let l:show_website_url = 0
33
34     let l:msg = "has been deprecated in favour of flake8 config files"
35     for setting_name in ['g:flake8_ignore', 'g:flake8_builtins', 'g:flake8_max_line_length', 'g:flake8_max_complexity']
36       if exists(setting_name)
37         echohl WarningMsg | echom setting_name l:msg | echohl None
38         let l:show_website_url = 1
39       endif
40     endfor
41
42     if l:show_website_url
43       let l:url = "http://flake8.readthedocs.org/en/latest/config.html"
44       echohl WarningMsg | echom l:url | echohl None
45     endif
46     let s:displayed_warnings = 1
47   endif
48 endfunction
49
50 "" config
51
52 function! s:DeclareOption(name, globalPrefix, default)  " {{{
53     if !exists('g:'.a:name)
54         if a:default != ''
55             execute 'let s:'.a:name.'='.a:default
56         else
57             execute 'let s:'.a:name.'=""'
58         endif
59     else
60         execute 'let l:global="g:".a:name'
61         if l:global != ''
62             execute 'let s:'.a:name.'="'.a:globalPrefix.'".g:'.a:name
63         else
64             execute 'let s:'.a:name.'=""'
65         endif
66     endif
67 endfunction  " }}}
68
69 function! s:Setup()  " {{{
70     "" read options
71
72     " flake8 command
73     call s:DeclareOption('flake8_cmd', '', '"flake8"')
74     " quickfix
75     call s:DeclareOption('flake8_quickfix_location', '', '"belowright"')
76     call s:DeclareOption('flake8_quickfix_height', '', 5)
77     call s:DeclareOption('flake8_show_quickfix', '', 1)
78     " markers to show
79     call s:DeclareOption('flake8_show_in_gutter', '',   0)
80     call s:DeclareOption('flake8_show_in_file', '',   0)
81     call s:DeclareOption('flake8_max_markers', '', 500)
82     " marker signs
83     call s:DeclareOption('flake8_error_marker', '', '"E>"')
84     call s:DeclareOption('flake8_warning_marker', '', '"W>"')
85     call s:DeclareOption('flake8_pyflake_marker', '', '"F>"')
86     call s:DeclareOption('flake8_complexity_marker', '', '"C>"')
87     call s:DeclareOption('flake8_naming_marker', '', '"N>"')
88
89     "" setup markerdata
90
91     if !exists('s:markerdata')
92         let s:markerdata = {}
93         let s:markerdata['E'] = {'name': 'Flake8_Error'}
94         let s:markerdata['W'] = {'name': 'Flake8_Warning'}
95         let s:markerdata['F'] = {'name': 'Flake8_PyFlake'}
96         let s:markerdata['C'] = {'name': 'Flake8_Complexity'}
97         let s:markerdata['N'] = {'name': 'Flake8_Nameing'}
98     endif
99     let s:markerdata['E'].marker = s:flake8_error_marker
100     let s:markerdata['W'].marker = s:flake8_warning_marker
101     let s:markerdata['F'].marker = s:flake8_pyflake_marker
102     let s:markerdata['C'].marker = s:flake8_complexity_marker
103     let s:markerdata['N'].marker = s:flake8_naming_marker
104 endfunction  " }}}
105
106 "" do flake8
107
108 function! s:Flake8()  " {{{
109     " read config
110     call s:Setup()
111
112     let l:executable = split(s:flake8_cmd)[0]
113
114     if !executable(l:executable)
115         echoerr "File " . l:executable . " not found. Please install it first."
116         return
117     endif
118
119     " clear old
120     call s:UnplaceMarkers()
121     let s:matchids = []
122     let s:signids  = []
123
124     " store old grep settings (to restore later)
125     let l:old_gfm=&grepformat
126     let l:old_gp=&grepprg
127     let l:old_shellpipe=&shellpipe
128     let l:old_t_ti=&t_ti
129     let l:old_t_te=&t_te
130
131     " write any changes before continuing
132     if &readonly == 0
133         update
134     endif
135
136     set lazyredraw   " delay redrawing
137     cclose           " close any existing cwindows
138
139     " prevent terminal from blinking
140     set shellpipe=>
141     set t_ti=
142     set t_te=
143
144     " perform the grep itself
145     let &grepformat="%f:%l:%c: %m\,%f:%l: %m"
146     let &grepprg=s:flake8_cmd
147     silent! grep! "%"
148
149     " restore grep settings
150     let &grepformat=l:old_gfm
151     let &grepprg=l:old_gp
152     let &shellpipe=l:old_shellpipe
153     let &t_ti=l:old_t_ti
154     let &t_te=l:old_t_te
155
156     " process results
157     let l:results=getqflist()
158     let l:has_results=results != []
159     if l:has_results
160         " markers
161         if !s:flake8_show_in_gutter == 0 || !s:flake8_show_in_file == 0
162             call s:PlaceMarkers(l:results)
163         endif
164         " quickfix
165         if !s:flake8_show_quickfix == 0
166             " open cwindow
167             execute s:flake8_quickfix_location." copen".s:flake8_quickfix_height
168             setlocal wrap
169             nnoremap <buffer> <silent> c :cclose<CR>
170             nnoremap <buffer> <silent> q :cclose<CR>
171         endif
172     endif
173
174     set nolazyredraw
175     redraw!
176
177     " Show status
178     if l:has_results == 0
179         echon "Flake8 check OK"
180     else
181         echon "Flake8 found issues"
182     endif
183 endfunction  " }}}
184
185 "" markers
186
187 function! s:PlaceMarkers(results)  " {{{
188     " in gutter?
189     if !s:flake8_show_in_gutter == 0
190         " define signs
191         for val in values(s:markerdata)
192             if val.marker != ''
193                 execute "sign define ".val.name." text=".val.marker." texthl=".val.name
194             endif
195         endfor
196     endif
197
198     " place
199     let l:index0 = 100
200     let l:index  = l:index0
201     for result in a:results
202         if l:index >= (s:flake8_max_markers+l:index0)
203             break
204         endif
205         let l:type = strpart(result.text, 0, 1)
206         if has_key(s:markerdata, l:type) && s:markerdata[l:type].marker != ''
207             " file markers
208             if !s:flake8_show_in_file == 0
209                 if !has_key(s:markerdata[l:type], 'matchstr')
210                     let s:markerdata[l:type].matchstr = '\%('
211                 else
212                     let s:markerdata[l:type].matchstr .= '\|'
213                 endif
214                 let s:markerdata[l:type].matchstr .= '\%'.result.lnum.'l\%'.result.col.'c'
215             endif
216             " gutter markers
217             if !s:flake8_show_in_gutter == 0
218                 execute ":sign place ".index." name=".s:markerdata[l:type].name
219                             \ . " line=".result.lnum." file=".expand("%:p")
220                 let s:signids += [l:index]
221             endif
222             let l:index += 1
223         endif
224     endfor
225
226     " in file?
227     if !s:flake8_show_in_file == 0
228         for l:val in values(s:markerdata)
229             if l:val.marker != '' && has_key(l:val, 'matchstr')
230                 let l:val.matchid = matchadd(l:val.name, l:val.matchstr.'\)')
231             endif
232         endfor
233     endif
234 endfunction  " }}}
235
236 function! s:UnplaceMarkers()  " {{{
237     " gutter markers
238     if exists('s:signids')
239         for i in s:signids
240             execute ":sign unplace ".i
241         endfor
242         unlet s:signids
243     endif
244     " file markers
245     for l:val in values(s:markerdata)
246         if has_key(l:val, 'matchid')
247             call matchdelete(l:val.matchid)
248             unlet l:val.matchid
249             unlet l:val.matchstr
250         endif
251     endfor
252 endfunction  " }}}
253
254 "" }}}
255
256 let &cpo = s:save_cpo
257 unlet s:save_cpo
258