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

67d696584dd30fc8760d1ffae3036a896be2f42b
[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 index = 100
185     for result in a:results
186         let type = strpart(result.text, 0, 1)
187         if has_key(s:markerdata, type)
188             " file markers
189             if !s:flake8_show_in_file == 0
190                 let s:matchids += [matchadd(s:markerdata[type]['color'],
191                             \ "\\%".result.lnum."l\\%".result.col."c")]
192             endif
193             " gutter markers
194             if !s:flake8_show_in_gutter == 0
195                 execute ":sign place ".index." name=".s:markerdata[type]['sign']
196                             \ . " line=".result.lnum." file=".expand("%:p")
197                 let s:signids += [index]
198                 let index += 1
199             endif
200         endif
201     endfor
202 endfunction  " }}}
203
204 function! s:UnplaceMarkers()  " {{{
205     " gutter markers
206     if exists('s:signids')
207         for i in s:signids
208             execute ":sign unplace ".i
209         endfor
210         unlet s:signids
211     endif
212     " file markers
213     if exists('s:matchids')
214         for i in s:matchids
215             call matchdelete(i)
216         endfor
217         unlet s:matchids
218     endif
219 endfunction  " }}}
220
221 "" }}}
222
223 let &cpo = s:save_cpo
224 unlet s:save_cpo
225