|
| 1 | +" vim: ts=4 shiftwidth=4 expandtab fdm=marker |
| 2 | + |
| 3 | +" version: 0.6 |
| 4 | +" lastchange: 2008-12-15 |
| 5 | + |
| 6 | + |
| 7 | +if !has('python') |
| 8 | + echohl ErrorMsg | echomsg "Required vim compiled with +python" | echohl None |
| 9 | + finish |
| 10 | +endif |
| 11 | + |
| 12 | +" the following is python code {{{ |
| 13 | +python << eof |
| 14 | + |
| 15 | +import vim |
| 16 | +if vim.eval('exists("g:pcs_hotkey")') == '0': |
| 17 | + key_check = '<LocalLeader>cs' |
| 18 | +else: |
| 19 | + key_check = vim.eval('g:pcs_hotkey') |
| 20 | +if vim.eval('exists("g:pcs_check_when_saving")') == '0': |
| 21 | + check_when_saving = True |
| 22 | +else: |
| 23 | + check_when_saving = vim.eval('g:pcs_check_when_saving') |
| 24 | + |
| 25 | +cmd_check = 'noremap <buffer> %s :py pysyntaxchecker.check()<CR>' % key_check |
| 26 | +vim.command(cmd_check) |
| 27 | +if check_when_saving: |
| 28 | + cmd = 'autocmd BufWritePost *.py py pysyntaxchecker.check()' |
| 29 | + vim.command(cmd) |
| 30 | + |
| 31 | +eof |
| 32 | +" }}} |
| 33 | + |
| 34 | +if exists("g:loaded_pysyntaxchecker") |
| 35 | + finish |
| 36 | +endif |
| 37 | +let g:loaded_pysyntaxchecker=1 |
| 38 | + |
| 39 | +" the following is python code {{{ |
| 40 | +python << end |
| 41 | + |
| 42 | +import vim |
| 43 | +try: |
| 44 | + from pyflakes.checker import Checker |
| 45 | +except: # pyflakes version <= 0.2.1 |
| 46 | + from pyflakes import Checker |
| 47 | + |
| 48 | +def vimeval(expr): |
| 49 | + vim.command('let g:pcs_variant = eval("%s")' % expr) |
| 50 | + isint = bool(int(vim.eval('type(pcs_variant) == type(0)'))) |
| 51 | + isfunc = bool(int(vim.eval('type(pcs_variant) == type(function("tr"))'))) |
| 52 | + isfloat = bool(int(vim.eval('type(pcs_variant) == type(0.0)'))) |
| 53 | + value = vim.eval('pcs_variant') |
| 54 | + if isint: |
| 55 | + value = int(value) |
| 56 | + elif isfloat: |
| 57 | + value = float(value) |
| 58 | + elif isfunc: |
| 59 | + raise VimError, 'Not supported date type' |
| 60 | + else: # cound treat correctly |
| 61 | + pass |
| 62 | + return value |
| 63 | + |
| 64 | +class VimOption(object): |
| 65 | + setcmd = 'set' |
| 66 | + |
| 67 | + def __getattr__(self, name): |
| 68 | + _value = vim.eval('&%s' % name) |
| 69 | + if isinstance(_value, int): |
| 70 | + try: |
| 71 | + vim.eval('&no%s' % name) |
| 72 | + value = bool(_value) |
| 73 | + except VimError: |
| 74 | + value = int(_value) |
| 75 | + else: |
| 76 | + value = str(_value) |
| 77 | + return value |
| 78 | + |
| 79 | + def __setattr__(self, name, value): |
| 80 | + if isinstance(value, bool): # is boolean |
| 81 | + opt = name if value else 'no%s' % name |
| 82 | + else: # is number or string |
| 83 | + opt = '%s=%s' % (name, value) |
| 84 | + return vim.command('%s %s' % (self.setcmd, opt)) |
| 85 | + |
| 86 | +vimopt = VimOption() |
| 87 | + |
| 88 | +class VimFunction(object): |
| 89 | + def __getattr__(self, name): |
| 90 | + self.func_name = name |
| 91 | + return self.call |
| 92 | + |
| 93 | + def call(self, *args): |
| 94 | + _args = ','.join([repr(arg) for arg in args]) |
| 95 | + return vim.eval('%s(%s)' % (self.func_name, _args)) |
| 96 | + |
| 97 | +vimfunc = VimFunction() |
| 98 | + |
| 99 | +class VimQuickFix(object): |
| 100 | + def __init__(self): |
| 101 | + if vimfunc.exists("g:pcs_max_win_height"): |
| 102 | + self.max_height = 10 |
| 103 | + else: |
| 104 | + self.max_height = vim.eval('g:pcs_max_win_height') |
| 105 | + |
| 106 | + def open(self): |
| 107 | + self.close() |
| 108 | + h = len(vimfunc.getqflist()) |
| 109 | + if h: |
| 110 | + height = min(h, self.max_height) |
| 111 | + vim.command("copen %d" % height) |
| 112 | + else: |
| 113 | + self.close() |
| 114 | + |
| 115 | + def close(sel): |
| 116 | + vim.command("cclose") |
| 117 | + |
| 118 | + def make(self, msgs): |
| 119 | + if msgs: |
| 120 | + keys = ['filename', 'lnum', 'text', 'type'] |
| 121 | + # errors = [dict(zip(keys, msg)) for msg in msgs if None not in msg] |
| 122 | + errors = [dict(zip(keys, msg)) for msg in msgs] |
| 123 | + vimfunc.setqflist(errors, 'r') |
| 124 | + self.open() |
| 125 | + else: |
| 126 | + self.close() |
| 127 | + |
| 128 | +quickfix = VimQuickFix() |
| 129 | + |
| 130 | +class PySyntaxChecker(object): |
| 131 | + def check(self): |
| 132 | + source = '\n'.join(vim.current.buffer[:]) |
| 133 | + filename = vimfunc.expand(r"%:p") |
| 134 | + self._check(source, filename) |
| 135 | + |
| 136 | + def _check(self, source, filename): |
| 137 | + msgs = [] |
| 138 | + try: |
| 139 | + tree = compiler.parse(source) |
| 140 | + except (SyntaxError, IndentationError), e: |
| 141 | + msgs.append((filename, e.lineno, e.args[0], 'E')) |
| 142 | + except Exception, e: |
| 143 | + msgs.append((filename, 1, e.args[0], 'E')) |
| 144 | + else: |
| 145 | + w = Checker(tree, filename) |
| 146 | + w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno)) |
| 147 | + for msg in w.messages: |
| 148 | + msgs.append((filename, msg.lineno, msg.message % msg.message_args, 'W')) |
| 149 | + |
| 150 | + quickfix.make(sorted(msgs)) |
| 151 | + |
| 152 | + |
| 153 | + # NOT IMPLEMENT COMPLETELY |
| 154 | + def pep8check(self, filename, message): |
| 155 | + """ |
| 156 | + Parse command line options and run checks on Python source. |
| 157 | + """ |
| 158 | + from modules import pep8 |
| 159 | + from optparse import OptionParser |
| 160 | + import os |
| 161 | + #from modules import Globals |
| 162 | + |
| 163 | + #pref = Globals.pref |
| 164 | + |
| 165 | + class MyPep8(pep8.Checker): |
| 166 | + def report_error(self, line_number, offset, text, check): |
| 167 | + message.append((self.filename, line_number, text)) |
| 168 | + |
| 169 | + options = None |
| 170 | + usage = "%prog [options] input ..." |
| 171 | + parser = OptionParser(usage) |
| 172 | + parser.add_option('-v', '--verbose', default=0, action='count', |
| 173 | + help="print status messages, or debug with -vv") |
| 174 | + parser.add_option('-q', '--quiet', default=0, action='count', |
| 175 | + help="report only file names, or nothing with -qq") |
| 176 | + parser.add_option('--exclude', metavar='patterns', default=pep8.default_exclude, |
| 177 | + help="skip matches (default %s)" % pep8.default_exclude) |
| 178 | + parser.add_option('--filename', metavar='patterns', |
| 179 | + help="only check matching files (e.g. *.py)") |
| 180 | + parser.add_option('--ignore', metavar='errors', default='', |
| 181 | + help="skip errors and warnings (e.g. E4,W)") |
| 182 | + parser.add_option('--repeat', action='store_true', |
| 183 | + help="show all occurrences of the same error") |
| 184 | + parser.add_option('--show-source', action='store_true', |
| 185 | + help="show source code for each error") |
| 186 | + parser.add_option('--show-pep8', action='store_true', |
| 187 | + help="show text of PEP 8 for each error") |
| 188 | + parser.add_option('--statistics', action='store_true', |
| 189 | + help="count errors and warnings") |
| 190 | + parser.add_option('--benchmark', action='store_true', |
| 191 | + help="measure processing speed") |
| 192 | + parser.add_option('--testsuite', metavar='dir', |
| 193 | + help="run regression tests from dir") |
| 194 | + parser.add_option('--doctest', action='store_true', |
| 195 | + help="run doctest on myself") |
| 196 | + options, args = parser.parse_args([filename]) |
| 197 | + pep8.options = options |
| 198 | + if options.doctest: |
| 199 | + import doctest |
| 200 | + return doctest.testmod() |
| 201 | + if options.testsuite: |
| 202 | + args.append(options.testsuite) |
| 203 | + if len(args) == 0: |
| 204 | + parser.error('input not specified') |
| 205 | + options.prog = os.path.basename(sys.argv[0]) |
| 206 | + options.exclude = options.exclude.split(',') |
| 207 | + for index in range(len(options.exclude)): |
| 208 | + options.exclude[index] = options.exclude[index].rstrip('/') |
| 209 | + if options.filename: |
| 210 | + options.filename = options.filename.split(',') |
| 211 | + if options.ignore: |
| 212 | + options.ignore = options.ignore.split(',') |
| 213 | + else: |
| 214 | + options.ignore = [] |
| 215 | + options.counters = {} |
| 216 | + options.messages = {} |
| 217 | + # if pref.py_check_skip_long_line: |
| 218 | + # pep8.maximum_line_length = None |
| 219 | + # if pref.py_check_skip_tailing_whitespace: |
| 220 | + # pep8.trailing_whitespace = None |
| 221 | + # if pref.py_check_skip_blank_lines: |
| 222 | + # pep8.blank_lines = None |
| 223 | + MyPep8(filename).check_all() |
| 224 | + |
| 225 | +pysyntaxchecker = PySyntaxChecker() |
| 226 | + |
| 227 | +end |
| 228 | +" }}} |
0 commit comments