Browse Source

Added flake8 checker to vim (press F7)

[Flake8](http://pypi.python.org/pypi/flake8/) is a wrapper around
PyFlakes (static syntax checker), PEP8 (style checker) and Ned's
MacCabe script (complexity checker).
Alois Mahdal 11 years ago
parent
commit
686594be92

+ 88
- 0
dotfiles/vim/bundle/vim-flake8/README.mdown View File

@@ -0,0 +1,88 @@
1
+vim-flake8
2
+==========
3
+`vim-flake8` is a Vim plugin that runs the currently open file through Flake8,
4
+a static syntax and style checker for Python source code.  It supersedes both
5
+[vim-pyflakes](https://github.com/nvie/vim-pyflakes) and
6
+[vim-pep8](https://github.com/nvie/vim-pep8).
7
+
8
+[Flake8](http://pypi.python.org/pypi/flake8/) is a wrapper around PyFlakes 
9
+(static syntax checker), PEP8 (style checker) 
10
+and Ned's MacCabe script (complexity checker).
11
+
12
+
13
+Installation
14
+------------
15
+Use [vim-pathogen](https://github.com/tpope/vim-pathogen) if you're not using
16
+it already.  Make sure you've installed the [flake8](http://pypi.python.org/pypi/flake8/) package.
17
+Then, simply put the contents of this repository in your
18
+`~/.vim/bundle` directory.
19
+
20
+Usage
21
+-----
22
+1. Open a Python file
23
+2. Press `<F7>` to run `flake8` on it
24
+
25
+It shows the errors inside a quickfix window, which will allow your to quickly
26
+jump to the error locations by simply pressing [Enter].
27
+
28
+
29
+Customization
30
+-------------
31
+If you don't want to use the `<F7>` key for flake8-checking, simply remap it to
32
+another key.  It autodetects whether it has been remapped and won't register
33
+the `<F7>` key if so.  For example, to remap it to `<F3>` instead, use:
34
+
35
+    autocmd FileType python map <buffer> <F3> :call Flake8()<CR>
36
+
37
+To add builtins, in your .vimrc:
38
+
39
+    let g:flake8_builtins="_,apply"
40
+
41
+To ignore errors, in your .vimrc:
42
+
43
+    let g:flake8_ignore="E501,W293"
44
+
45
+If you want to change the max line length for PEP8:
46
+
47
+    let g:flake8_max_line_length=99
48
+
49
+To set the maximum [McCabe complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity) before a warning is issued:
50
+
51
+    let g:flake8_max_complexity=10
52
+
53
+To customize the location of your flake8 binary, set `g:flake8_cmd`:
54
+
55
+    let g:flake8_cmd="/opt/strangebin/flake8000"
56
+
57
+
58
+Tips
59
+----
60
+A tip might be to run the Flake8 check every time you write a Python file, to
61
+enable this, add the following line to your `.vimrc` file (thanks
62
+[Godefroid](http://github.com/gotcha)!):
63
+
64
+    autocmd BufWritePost *.py call Flake8()
65
+
66
+This plugin goes well together with the following plugin:
67
+
68
+- [PyUnit](http://github.com/nvie/vim-pyunit) (unit test helper under `<F8>`
69
+  and `<F9>`)
70
+
71
+
72
+History
73
+-------
74
+
75
+1.3: Added the following options:
76
+
77
+     - `g:flake8_builtins="_,apply"`
78
+     - `g:flake8_max_complexity=10`
79
+
80
+1.2: Added the following options:
81
+
82
+     - `g:flake8_cmd="/opt/strangebin/flake8000"`
83
+     - `g:flake8_max_line_length=120`
84
+     - `g:flake8_ignore="E501,W293"`
85
+
86
+1.1: Added `g:flake8_ignore` option.
87
+
88
+1.0: Initial version.

+ 102
- 0
dotfiles/vim/bundle/vim-flake8/ftplugin/python_flake8.vim View File

@@ -0,0 +1,102 @@
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
+" Only do this when not done yet for this buffer
9
+if exists("b:loaded_flake8_ftplugin")
10
+    finish
11
+endif
12
+let b:loaded_flake8_ftplugin=1
13
+
14
+if !exists("*Flake8()")
15
+    function Flake8()
16
+        if exists("g:flake8_cmd")
17
+            let s:flake8_cmd=g:flake8_cmd
18
+        else
19
+            let s:flake8_cmd="flake8"
20
+        endif
21
+
22
+        if !executable(s:flake8_cmd)
23
+            echoerr "File " . s:flake8_cmd . " not found. Please install it first."
24
+            return
25
+        endif
26
+
27
+        set lazyredraw   " delay redrawing
28
+        cclose           " close any existing cwindows
29
+
30
+        " store old grep settings (to restore later)
31
+        let l:old_gfm=&grepformat
32
+        let l:old_gp=&grepprg
33
+
34
+        " write any changes before continuing
35
+        if &readonly == 0
36
+            update
37
+        endif
38
+
39
+        " read config
40
+        if exists("g:flake8_builtins")
41
+            let s:flake8_builtins_opt=" --builtins=".g:flake8_builtins
42
+        else
43
+            let s:flake8_builtins_opt=""
44
+        endif
45
+
46
+        if exists("g:flake8_ignore")
47
+            let s:flake8_ignores=" --ignore=".g:flake8_ignore
48
+        else
49
+            let s:flake8_ignores=""
50
+        endif
51
+
52
+        if exists("g:flake8_max_line_length")
53
+            let s:flake8_max_line_length=" --max-line-length=".g:flake8_max_line_length
54
+        else
55
+            let s:flake8_max_line_length=""
56
+        endif
57
+
58
+        if exists("g:flake8_max_complexity")
59
+            let s:flake8_max_complexity=" --max-complexity=".g:flake8_max_complexity
60
+        else
61
+            let s:flake8_max_complexity=""
62
+        endif
63
+
64
+        " perform the grep itself
65
+        let &grepformat="%f:%l:%c: %m\,%f:%l: %m"
66
+        let &grepprg=s:flake8_cmd.s:flake8_builtins_opt.s:flake8_ignores.s:flake8_max_line_length.s:flake8_max_complexity
67
+        silent! grep! %
68
+
69
+        " restore grep settings
70
+        let &grepformat=l:old_gfm
71
+        let &grepprg=l:old_gp
72
+
73
+        " open cwindow
74
+        let has_results=getqflist() != []
75
+        if has_results
76
+            execute 'belowright copen'
77
+            setlocal wrap
78
+            nnoremap <buffer> <silent> c :cclose<CR>
79
+            nnoremap <buffer> <silent> q :cclose<CR>
80
+        endif
81
+
82
+        set nolazyredraw
83
+        redraw!
84
+
85
+        if has_results == 0
86
+            " Show OK status
87
+            hi Green ctermfg=green
88
+            echohl Green
89
+            echon "Flake8 check OK"
90
+            echohl
91
+        endif
92
+    endfunction
93
+endif
94
+
95
+" Add mappings, unless the user didn't want this.
96
+" The default mapping is registered under to <F7> by default, unless the user
97
+" remapped it already (or a mapping exists already for <F7>)
98
+if !exists("no_plugin_maps") && !exists("no_flake8_maps")
99
+    if !hasmapto('Flake8(')
100
+        noremap <buffer> <F7> :call Flake8()<CR>
101
+    endif
102
+endif