]> 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:

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