mirror of
https://github.com/gryf/pylint-vim.git
synced 2025-12-19 12:28:11 +01:00
Sepearate python and vimscript files.
Also added function which can utilize either +python or +python3 depending on what is available on vim.
This commit is contained in:
71
ftplugin/python/pylint_fn.py
Normal file
71
ftplugin/python/pylint_fn.py
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
"""
|
||||||
|
Get pylint oputput on current buffer
|
||||||
|
"""
|
||||||
|
import sys
|
||||||
|
import re
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
|
try:
|
||||||
|
from pylint import lint
|
||||||
|
from pylint.reporters.text import TextReporter
|
||||||
|
except ImportError:
|
||||||
|
raise AssertionError('Error: pylint_fm.vim requires module pylint')
|
||||||
|
|
||||||
|
import vim
|
||||||
|
|
||||||
|
|
||||||
|
class VImPylint(object):
|
||||||
|
"""Vim pylint interface"""
|
||||||
|
|
||||||
|
sys_stderr = sys.stderr
|
||||||
|
dummy_stderr = StringIO()
|
||||||
|
conf_msg = 'No config file found, using default configuration\n'
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def run(self):
|
||||||
|
"""execute pylint and fill the quickfix"""
|
||||||
|
|
||||||
|
# clear QF window
|
||||||
|
vim.command('call setqflist([])')
|
||||||
|
|
||||||
|
# args
|
||||||
|
args = ['-rn', # display only the messages instead of full report
|
||||||
|
vim.current.buffer.name]
|
||||||
|
|
||||||
|
buf = StringIO() # file-like buffer, instead of stdout
|
||||||
|
reporter = TextReporter(buf)
|
||||||
|
|
||||||
|
sys.stderr = self.dummy_stderr
|
||||||
|
lint.Run(args, reporter=reporter, exit=False)
|
||||||
|
sys.stderr = self.sys_stderr
|
||||||
|
|
||||||
|
self.dummy_stderr.seek(0)
|
||||||
|
error_list = self.dummy_stderr.readlines()
|
||||||
|
self.dummy_stderr.truncate(0)
|
||||||
|
if error_list and self.conf_msg in error_list:
|
||||||
|
error_list.remove(self.conf_msg)
|
||||||
|
if error_list:
|
||||||
|
raise Exception(''.join(error_list))
|
||||||
|
|
||||||
|
buf.seek(0)
|
||||||
|
|
||||||
|
bufnr = vim.current.buffer.number
|
||||||
|
error_list = []
|
||||||
|
|
||||||
|
error_re = re.compile(r'^(?P<type>[C,R,W,E,F]):\s+?'
|
||||||
|
r'(?P<lnum>[0-9]+),\s+?'
|
||||||
|
r'(?P<col>[0-9]+):\s'
|
||||||
|
r'(?P<text>.*)$')
|
||||||
|
for line in buf:
|
||||||
|
line = line.rstrip() # remove trailing newline character
|
||||||
|
|
||||||
|
error_line = error_re.match(line)
|
||||||
|
if error_line:
|
||||||
|
error_line = error_line.groupdict()
|
||||||
|
error_line["bufnr"] = bufnr
|
||||||
|
|
||||||
|
error_list.append(error_line)
|
||||||
|
|
||||||
|
vim.command('call setqflist(%s)' % str(error_list))
|
||||||
|
if error_list:
|
||||||
|
vim.command('copen')
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
"
|
"
|
||||||
" This script uses python, therefore VIm should be compiled with python
|
" This script uses python, therefore VIm should be compiled with python
|
||||||
" support. You can check it by issuing ":version" command, and search for
|
" support. You can check it by issuing ":version" command, and search for
|
||||||
" "+python" inside features list.
|
" "+python" or "+python3" inside features list.
|
||||||
"
|
"
|
||||||
" Couple of ideas was taken from pyflakes.vim[2] plugin.
|
" Couple of ideas was taken from pyflakes.vim[2] plugin.
|
||||||
"
|
"
|
||||||
@@ -44,111 +44,40 @@
|
|||||||
" [3] http://pypi.python.org/pypi/setuptools
|
" [3] http://pypi.python.org/pypi/setuptools
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
|
let s:plugin_path = expand('<sfile>:p:h', 1)
|
||||||
|
|
||||||
if exists("b:did_pylint_plugin")
|
if exists("b:did_pylint_plugin")
|
||||||
finish " only load once
|
finish " only load once
|
||||||
else
|
else
|
||||||
let b:did_pylint_plugin = 1
|
let b:did_pylint_plugin = 1
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if !exists('*s:CheckPylint')
|
function! s:SetPython(msg)
|
||||||
function s:CheckPylint()
|
if !exists('g:_python')
|
||||||
python << EOF
|
if has('python')
|
||||||
try:
|
let g:_python = {'exec': 'python', 'file': 'pyfile'}
|
||||||
import vim
|
elseif has('python3')
|
||||||
from pylint import lint
|
let g:_python = {'exec': 'python3', 'file': 'py3file'}
|
||||||
from pylint.reporters.text import TextReporter
|
else
|
||||||
except ImportError:
|
echohl WarningMsg|echomsg a:msg|echohl None
|
||||||
vim.command("return 0")
|
finish
|
||||||
vim.command("return 1")
|
endif
|
||||||
EOF
|
|
||||||
endfunction
|
|
||||||
endif
|
|
||||||
|
|
||||||
if s:CheckPylint() == 0
|
|
||||||
finish
|
|
||||||
endif
|
|
||||||
|
|
||||||
if !exists("b:did_pylint_init")
|
|
||||||
let b:did_pylint_init = 0
|
|
||||||
|
|
||||||
if !has('python')
|
|
||||||
echoerr "Error: pylint_fn.vim requires Vim to be compiled with +python"
|
|
||||||
finish
|
|
||||||
endif
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
python << EOF
|
function! s:LoadPylint()
|
||||||
import sys
|
let plugin_path = expand('<sfile>:p:h', 1)
|
||||||
import re
|
if !exists('g:pylint_fn_initialized ')
|
||||||
from StringIO import StringIO
|
call s:SetPython("Pylint command cannot be initialized - "
|
||||||
|
\ . "no python support compiled in vim.")
|
||||||
|
execute g:_python['file'] . ' ' . s:plugin_path . '/pylint_fn.py'
|
||||||
|
let g:pylint_fn_initialized = 1
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
class VImPylint(object):
|
call s:LoadPylint()
|
||||||
|
|
||||||
sys_stderr = sys.stderr
|
function s:Pylint()
|
||||||
dummy_stderr = StringIO()
|
execute g:_python['exec'] . ' VImPylint.run()'
|
||||||
conf_msg = 'No config file found, using default configuration\n'
|
endfunction
|
||||||
|
command Pylint call s:Pylint()
|
||||||
@classmethod
|
|
||||||
def run(self):
|
|
||||||
"""execute pylint and fill the quickfix"""
|
|
||||||
|
|
||||||
# clear QF window
|
|
||||||
vim.command('call setqflist([])')
|
|
||||||
|
|
||||||
# args
|
|
||||||
args = ['-rn', # display only the messages instead of full report
|
|
||||||
vim.current.buffer.name]
|
|
||||||
|
|
||||||
buf = StringIO() # file-like buffer, instead of stdout
|
|
||||||
reporter = TextReporter(buf)
|
|
||||||
|
|
||||||
sys.stderr = self.dummy_stderr
|
|
||||||
lint.Run(args, reporter=reporter, exit=False)
|
|
||||||
sys.stderr = self.sys_stderr
|
|
||||||
|
|
||||||
self.dummy_stderr.seek(0)
|
|
||||||
error_list = self.dummy_stderr.readlines()
|
|
||||||
self.dummy_stderr.truncate(0)
|
|
||||||
if error_list and self.conf_msg in error_list:
|
|
||||||
error_list.remove(self.conf_msg)
|
|
||||||
if error_list:
|
|
||||||
raise Exception(''.join(error_list))
|
|
||||||
|
|
||||||
buf.seek(0)
|
|
||||||
|
|
||||||
bufnr = vim.current.buffer.number
|
|
||||||
code_line = {}
|
|
||||||
error_list = []
|
|
||||||
|
|
||||||
error_re = re.compile(r'^(?P<type>[C,R,W,E,F]):\s+?'
|
|
||||||
r'(?P<lnum>[0-9]+),\s+?'
|
|
||||||
r'(?P<col>[0-9]+):\s'
|
|
||||||
r'(?P<text>.*)$')
|
|
||||||
for line in buf:
|
|
||||||
line = line.rstrip() # remove trailing newline character
|
|
||||||
|
|
||||||
error_line = error_re.match(line)
|
|
||||||
if error_line:
|
|
||||||
error_line = error_line.groupdict()
|
|
||||||
error_line["bufnr"] = bufnr
|
|
||||||
|
|
||||||
error_list.append(error_line)
|
|
||||||
|
|
||||||
vim.command('call setqflist(%s)' % str(error_list))
|
|
||||||
if error_list:
|
|
||||||
vim.command('copen')
|
|
||||||
|
|
||||||
EOF
|
|
||||||
let b:did_pylint_init = 1
|
|
||||||
endif
|
|
||||||
|
|
||||||
if !exists('*s:Pylint')
|
|
||||||
function s:Pylint()
|
|
||||||
python << EOF
|
|
||||||
VImPylint.run()
|
|
||||||
EOF
|
|
||||||
endfunction
|
|
||||||
endif
|
|
||||||
|
|
||||||
if !exists(":Pylint")
|
|
||||||
command Pylint call s:Pylint()
|
|
||||||
endif
|
|
||||||
|
|||||||
Reference in New Issue
Block a user