Skip to content

Commit 91b938c

Browse files
committed
Add pyflakes.
1 parent c3606a8 commit 91b938c

File tree

5 files changed

+795
-21
lines changed

5 files changed

+795
-21
lines changed

after/plugin/pymode_lint.vim

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ endif
1010
" OPTION: g:pymode_lint_write -- bool. Check code every save.
1111
call helpers#SafeVar("g:pymode_lint_write", 1)
1212

13+
" OPTION: g:pymode_lint_checker -- str. Use pylint of pyflakes for check.
14+
call helpers#SafeVar("g:pymode_lint_checker", "pylint")
15+
1316
" OPTION: g:pymode_lint_cwindow -- bool. Auto open cwindow if errors find
1417
call helpers#SafeVar("g:pymode_lint_cwindow", 1)
1518

@@ -46,24 +49,70 @@ endif
4649
python << EOF
4750
import os
4851
import StringIO
52+
import _ast
53+
import re
4954

5055
from logilab.astng.builder import MANAGER
5156
from pylint import lint, checkers
57+
from pyflakes import checker
58+
5259

60+
# Pylint setup
5361
linter = lint.PyLinter()
62+
pylint_re = re.compile('^[^:]+:(\d+): \[([EWRCI]+)[^\]]*\] (.*)$')
63+
5464
checkers.initialize(linter)
5565
linter.set_option("output-format", "parseable")
5666
linter.set_option("reports", 0)
57-
5867
linter.load_file_configuration(vim.eval("g:pymode_lint_config"))
5968

60-
def check():
61-
target = vim.eval("expand('%:p')")
69+
# Pyflakes setup
70+
71+
# Pylint check
72+
def pylint():
73+
filename = vim.current.buffer.name
6274
MANAGER.astng_cache.clear()
6375
linter.reporter.out = StringIO.StringIO()
64-
linter.check(target)
65-
pylint_output = linter.reporter.out.getvalue()
66-
vim.command('let pylint_output = "%s"' % pylint_output.replace('"', '\\"'))
76+
linter.check(filename)
77+
qf = []
78+
for w in linter.reporter.out.getvalue().split('\n'):
79+
test = pylint_re.match(w)
80+
test and qf.append(dict(
81+
filename = filename,
82+
bufnr = vim.current.buffer.number,
83+
lnum = test.group(1),
84+
type = test.group(2),
85+
text = test.group(3),
86+
))
87+
vim.command('let b:qf_list = %s' % repr(qf))
88+
89+
# Pyflakes check
90+
def pyflakes():
91+
filename = vim.current.buffer.name
92+
codeString = file(filename, 'U').read() + '\n'
93+
try:
94+
tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
95+
except SyntaxError, value:
96+
msg = value.args[0]
97+
if text is None:
98+
vim.command('echoerr "%s: problem decoding source"' % filename)
99+
else:
100+
lineno, _, text = value.lineno, value.offset, value.text
101+
line = text.splitlines()[-1]
102+
vim.command('echoerr "%s:%d: %s"' % (filename, lineno, msg))
103+
vim.command('echoerr "%s"' % line)
104+
else:
105+
w = checker.Checker(tree, filename)
106+
w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
107+
qf = [dict(
108+
filename = filename,
109+
bufnr = vim.current.buffer.number,
110+
lnum = str(w.lineno),
111+
text = w.message % w.message_args,
112+
type = 'E'
113+
) for w in w.messages]
114+
vim.command('let b:qf_list = %s' % repr(qf))
115+
67116
EOF
68117

69118
" DESC: Check code
@@ -76,21 +125,7 @@ function! pymode_lint#Lint()
76125

77126
let pylint_output = ""
78127
let bufnum = bufnr("%")
79-
py check()
80-
let b:qf_list = []
81-
for error in split(pylint_output, "\n")
82-
let b:parts = matchlist(error, '\v([A-Za-z\.]+):(\d+): \[([EWRCI]+)[^\]]*\] (.*)')
83-
if len(b:parts) > 3
84-
let l:qf_item = {}
85-
let l:qf_item.filename = expand('%')
86-
let l:qf_item.bufnr = bufnum
87-
let l:qf_item.lnum = b:parts[2]
88-
let l:qf_item.type = b:parts[3]
89-
let l:qf_item.text = b:parts[4]
90-
call add(b:qf_list, l:qf_item)
91-
endif
92-
endfor
93-
128+
exe "py ".g:pymode_lint_checker."()"
94129
call setqflist(b:qf_list, 'r')
95130

96131
" Open cwindow
@@ -118,3 +153,8 @@ fun! pymode_lint#Toggle() "{{{
118153
echomsg "PyLint disabled."
119154
endif
120155
endfunction "}}}
156+
157+
fun! pymode_lint#ToggleChecker() "{{{
158+
let g:pymode_lint_checker = g:pymode_lint_checker == "pylint" ? "pyflakes" : "pylint"
159+
echomsg "PyLint checker: " . g:pymode_lint_checker
160+
endfunction "}}}

ftplugin/python/pymode.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ if g:pymode_lint
6767

6868
" DESC: Set commands
6969
command! -buffer PyLintToggle :call pymode_lint#Toggle()
70+
command! -buffer PyLintCheckerToggle :call pymode_lint#ToggleChecker()
7071
command! -buffer PyLint :call pymode_lint#Lint()
7172

7273
endif

pylibs/pyflakes/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
__version__ = '0.4.0'

0 commit comments

Comments
 (0)