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

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