mirror of
https://github.com/gryf/pep8-vim.git
synced 2025-12-19 12:37:59 +01:00
Added Python2/Python3 support
Separated Python file from vim script, added logic for selecting available python interpreter compiled in vim.
This commit is contained in:
58
ftplugin/python/pep8_fn.py
Normal file
58
ftplugin/python/pep8_fn.py
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
"""
|
||||||
|
Get pep8 output on current vim buffer using pep8.Checker object
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
import pep8
|
||||||
|
except ImportError:
|
||||||
|
raise AssertionError('Error: pep8_fn.vim requires module pep8')
|
||||||
|
|
||||||
|
import vim
|
||||||
|
|
||||||
|
|
||||||
|
class VImPep8(object):
|
||||||
|
"""Vim PEP8 checker"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.fname = vim.current.buffer.name
|
||||||
|
self.bufnr = vim.current.buffer.number
|
||||||
|
self.output = []
|
||||||
|
self.exclude_list = vim.eval("g:pep8_exclude")
|
||||||
|
|
||||||
|
def reporter(self, lnum, col, text, check):
|
||||||
|
"""Custom reporter for checker report_error method"""
|
||||||
|
self.output.append([lnum, col, text])
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
"""Run the checker"""
|
||||||
|
pep8.process_options(['-r', vim.current.buffer.name])
|
||||||
|
checker = pep8.Checker(vim.current.buffer.name)
|
||||||
|
checker.report_error = self.reporter
|
||||||
|
checker.check_all()
|
||||||
|
self.process_output()
|
||||||
|
|
||||||
|
def process_output(self):
|
||||||
|
"""Transform checker output into quickfix list"""
|
||||||
|
vim.command('call setqflist([])')
|
||||||
|
qf_list = []
|
||||||
|
qf_dict = {}
|
||||||
|
|
||||||
|
for line in self.output:
|
||||||
|
skip = False
|
||||||
|
for exclude_pattern in self.exclude_list:
|
||||||
|
if exclude_pattern in line[2]:
|
||||||
|
skip = True
|
||||||
|
break
|
||||||
|
if skip:
|
||||||
|
continue
|
||||||
|
qf_dict['bufnr'] = self.bufnr
|
||||||
|
qf_dict['lnum'] = line[0]
|
||||||
|
qf_dict['col'] = line[1]
|
||||||
|
qf_dict['text'] = line[2]
|
||||||
|
qf_dict['type'] = line[2][0]
|
||||||
|
qf_list.append(qf_dict)
|
||||||
|
qf_dict = {}
|
||||||
|
|
||||||
|
self.output = []
|
||||||
|
vim.command('call setqflist(%s)' % str(qf_list))
|
||||||
|
if qf_list:
|
||||||
|
vim.command('copen')
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
" File: pep8_fn.vim
|
" File: pep8_fn.vim
|
||||||
" Author: Roman 'gryf' Dobosz (gryf73 at gmail.com)
|
" Author: Roman 'gryf' Dobosz (gryf73 at gmail.com)
|
||||||
" Version: 1.1
|
" Version: 1.2
|
||||||
" Last Modified: 2010-09-12
|
" Last Modified: 2016-12-10
|
||||||
" Description: {{{
|
" Description: {{{
|
||||||
"
|
"
|
||||||
" Overview
|
" Overview
|
||||||
@@ -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[3] plugin.
|
" Couple of ideas was taken from pyflakes.vim[3] plugin.
|
||||||
"
|
"
|
||||||
@@ -58,96 +58,34 @@ if !exists("g:pep8_exclude")
|
|||||||
let g:pep8_exclude = []
|
let g:pep8_exclude = []
|
||||||
endif
|
endif
|
||||||
|
|
||||||
function! s:DiscoverPython()
|
|
||||||
if !exists('g:pep8_py_discovered')
|
function! s:SetPython(msg)
|
||||||
|
if !exists('g:_python')
|
||||||
if has('python')
|
if has('python')
|
||||||
let s:python_command = 'python'
|
let g:_python = {'exec': 'python', 'file': 'pyfile'}
|
||||||
elseif has('python3')
|
elseif has('python3')
|
||||||
let s:python_command = 'python3'
|
let g:_python = {'exec': 'python3', 'file': 'py3file'}
|
||||||
else
|
else
|
||||||
echohl WarningMsg|echomsg
|
echohl WarningMsg|echomsg a:msg|echohl None
|
||||||
\ "PEP8 command cannot be initialized "
|
|
||||||
\ "no python support compiled in vim."|echohl None
|
|
||||||
finish
|
finish
|
||||||
endif
|
endif
|
||||||
let g:pep8_py_discovered = 1
|
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
if !exists("b:did_pep8_init")
|
|
||||||
let b:did_pep8_init = 0
|
|
||||||
|
|
||||||
if !has('python')
|
function! s:LoadPep8()
|
||||||
echoerr "pep8_fn.vim plugin requires Vim to be compiled with +python"
|
if !exists('g:pep8_fn_initialized ')
|
||||||
finish
|
call s:SetPython("Pep8 command cannot be initialized no python "
|
||||||
|
\ "support compiled in vim.")
|
||||||
|
execute g:_python['file'] . ' ' . s:plugin_path . '/pep8_fn.py'
|
||||||
|
let g:pep8_fn_initialized = 1
|
||||||
endif
|
endif
|
||||||
call s:DiscoverPython()
|
endfunction
|
||||||
|
|
||||||
python << EOF
|
|
||||||
import vim
|
|
||||||
import sys
|
|
||||||
from StringIO import StringIO
|
|
||||||
|
|
||||||
try:
|
call s:LoadPep8()
|
||||||
import pep8
|
|
||||||
except ImportError:
|
|
||||||
raise AssertionError('Error: pep8_fn.vim requires module pep8')
|
|
||||||
|
|
||||||
class VImPep8(object):
|
function s:Pep8()
|
||||||
|
execute g:_python['exec'] . ' VImPep8().run()'
|
||||||
def __init__(self):
|
endfunction
|
||||||
self.fname = vim.current.buffer.name
|
command Pep8 call s:Pep8()
|
||||||
self.bufnr = vim.current.buffer.number
|
|
||||||
self.output = []
|
|
||||||
self.exclude_list = vim.eval("g:pep8_exclude")
|
|
||||||
|
|
||||||
def reporter(self, lnum, col, text, check):
|
|
||||||
self.output.append([lnum, col, text])
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
pep8.process_options(['-r', vim.current.buffer.name])
|
|
||||||
checker = pep8.Checker(vim.current.buffer.name)
|
|
||||||
checker.report_error = self.reporter
|
|
||||||
checker.check_all()
|
|
||||||
self.process_output()
|
|
||||||
|
|
||||||
def process_output(self):
|
|
||||||
vim.command('call setqflist([])')
|
|
||||||
qf_list = []
|
|
||||||
qf_dict = {}
|
|
||||||
|
|
||||||
for line in self.output:
|
|
||||||
skip = False
|
|
||||||
for exclude_pattern in self.exclude_list:
|
|
||||||
if exclude_pattern in line[2]:
|
|
||||||
skip = True
|
|
||||||
break
|
|
||||||
if skip:
|
|
||||||
continue
|
|
||||||
qf_dict['bufnr'] = self.bufnr
|
|
||||||
qf_dict['lnum'] = line[0]
|
|
||||||
qf_dict['col'] = line[1]
|
|
||||||
qf_dict['text'] = line[2]
|
|
||||||
qf_dict['type'] = line[2][0]
|
|
||||||
qf_list.append(qf_dict)
|
|
||||||
qf_dict = {}
|
|
||||||
|
|
||||||
self.output = []
|
|
||||||
vim.command('call setqflist(%s)' % str(qf_list))
|
|
||||||
if qf_list:
|
|
||||||
vim.command('copen')
|
|
||||||
EOF
|
|
||||||
let b:did_pep8_init = 1
|
|
||||||
endif
|
|
||||||
|
|
||||||
if !exists('*s:Pep8')
|
|
||||||
function s:Pep8()
|
|
||||||
python << EOF
|
|
||||||
VImPep8().run()
|
|
||||||
EOF
|
|
||||||
endfunction
|
|
||||||
endif
|
|
||||||
|
|
||||||
if !exists(":Pep8")
|
|
||||||
command Pep8 call s:Pep8()
|
|
||||||
endif
|
|
||||||
|
|||||||
Reference in New Issue
Block a user