pythoncomplete.vim (24855B)
1 "pythoncomplete.vim - Omni Completion for python 2 " Maintainer: <vacancy> 3 " Previous Maintainer: Aaron Griffin <aaronmgriffin@gmail.com> 4 " Version: 0.10 5 " Last Updated: 2026 Jun 21 6 " 7 " Changes 8 " TODO: 9 " 'info' item output can use some formatting work 10 " Add an "unsafe eval" mode, to allow for return type evaluation 11 " Complete basic syntax along with import statements 12 " i.e. "import url<c-x,c-o>" 13 " Continue parsing on invalid line?? 14 " 15 " v 0.10 by Vim project 16 " * disables importing local modules, unless the global Vim variable 17 " g:pythoncomplete_allow_import is set to non-zero 18 " * strip default values and annotations from function parameter lists 19 " before exec(), and whitelist class base lists to dotted names: the 20 " previous code passed buffer-supplied expressions to exec() which 21 " Python evaluates at definition time, allowing arbitrary code 22 " execution via crafted def/class headers 23 " * use repr() on doc strings to prevent code execution 24 " 25 " v 0.9 26 " * Fixed docstring parsing for classes and functions 27 " * Fixed parsing of *args and **kwargs type arguments 28 " * Better function param parsing to handle things like tuples and 29 " lambda defaults args 30 " 31 " v 0.8 32 " * Fixed an issue where the FIRST assignment was always used instead of 33 " using a subsequent assignment for a variable 34 " * Fixed a scoping issue when working inside a parameterless function 35 " 36 " 37 " v 0.7 38 " * Fixed function list sorting (_ and __ at the bottom) 39 " * Removed newline removal from docs. It appears vim handles these better in 40 " recent patches 41 " 42 " v 0.6: 43 " * Fixed argument completion 44 " * Removed the 'kind' completions, as they are better indicated 45 " with real syntax 46 " * Added tuple assignment parsing (whoops, that was forgotten) 47 " * Fixed import handling when flattening scope 48 " 49 " v 0.5: 50 " Yeah, I skipped a version number - 0.4 was never public. 51 " It was a bugfix version on top of 0.3. This is a complete 52 " rewrite. 53 " 54 55 if !has('python') 56 echo "Error: Required vim compiled with +python" 57 finish 58 endif 59 60 function! pythoncomplete#Complete(findstart, base) 61 "findstart = 1 when we need to get the text length 62 if a:findstart == 1 63 let line = getline('.') 64 let idx = col('.') 65 while idx > 0 66 let idx -= 1 67 let c = line[idx] 68 if c =~ '\w' 69 continue 70 elseif ! c =~ '\.' 71 let idx = -1 72 break 73 else 74 break 75 endif 76 endwhile 77 78 return idx 79 "findstart = 0 when we need to return the list of completions 80 else 81 "vim no longer moves the cursor upon completion... fix that 82 let line = getline('.') 83 let idx = col('.') 84 let cword = '' 85 while idx > 0 86 let idx -= 1 87 let c = line[idx] 88 if c =~ '\w' || c =~ '\.' 89 let cword = c . cword 90 continue 91 elseif strlen(cword) > 0 || idx == 0 92 break 93 endif 94 endwhile 95 execute "python vimcomplete('" . escape(cword, "'") . "', '" . escape(a:base, "'") . "')" 96 return g:pythoncomplete_completions 97 endif 98 endfunction 99 100 function! s:DefPython() 101 python << PYTHONEOF 102 import sys, tokenize, cStringIO, types 103 from token import NAME, DEDENT, NEWLINE, STRING 104 import re 105 106 # Used by Class.get_code(): a base class expression is only included in the 107 # code passed to exec() if it is a pure dotted name (e.g. "Base", "mod.Base", 108 # "pkg.sub.Cls"). Anything containing calls, subscripts, "=", ":" or other 109 # operators is dropped, since exec()-ing it would evaluate buffer-supplied 110 # expressions. See the security note in the file header. 111 _DOTTED_NAME_RE = re.compile(r'^[A-Za-z_]\w*(\s*\.\s*[A-Za-z_]\w*)*$') 112 113 def _strip_param(p): 114 # Return the bare parameter name from a parameter spec harvested by 115 # _parenparse(), discarding any default value or annotation. Default 116 # values and annotations would otherwise be evaluated by exec() at 117 # function-definition time. Star prefixes ("*args", "**kw") and bare 118 # "*" / "/" are preserved as written. 119 p = p.split('=', 1)[0] 120 p = p.split(':', 1)[0] 121 return p.strip() 122 123 debugstmts=[] 124 def dbg(s): debugstmts.append(s) 125 def showdbg(): 126 for d in debugstmts: print "DBG: %s " % d 127 128 def vimcomplete(context,match): 129 global debugstmts 130 debugstmts = [] 131 try: 132 import vim 133 def complsort(x,y): 134 try: 135 xa = x['abbr'] 136 ya = y['abbr'] 137 if xa[0] == '_': 138 if xa[1] == '_' and ya[0:2] == '__': 139 return xa > ya 140 elif ya[0:2] == '__': 141 return -1 142 elif y[0] == '_': 143 return xa > ya 144 else: 145 return 1 146 elif ya[0] == '_': 147 return -1 148 else: 149 return xa > ya 150 except: 151 return 0 152 cmpl = Completer() 153 cmpl.evalsource('\n'.join(vim.current.buffer),vim.eval("line('.')")) 154 all = cmpl.get_completions(context,match) 155 all.sort(complsort) 156 dictstr = '[' 157 # have to do this for double quoting 158 for cmpl in all: 159 dictstr += '{' 160 for x in cmpl: dictstr += '"%s":"%s",' % (x,cmpl[x]) 161 dictstr += '"icase":0},' 162 if dictstr[-1] == ',': dictstr = dictstr[:-1] 163 dictstr += ']' 164 #dbg("dict: %s" % dictstr) 165 vim.command("silent let g:pythoncomplete_completions = %s" % dictstr) 166 #dbg("Completion dict:\n%s" % all) 167 except vim.error: 168 dbg("VIM Error: %s" % vim.error) 169 170 class Completer(object): 171 def __init__(self): 172 self.compldict = {} 173 self.parser = PyParser() 174 175 def evalsource(self,text,line=0): 176 # vim is imported locally in vimcomplete(); re-import here so the 177 # vim.eval() below works (otherwise NameError, silently caught). 178 import vim 179 sc = self.parser.parse(text,line) 180 try: allow_imports = int( 181 vim.eval("get(g:, 'pythoncomplete_allow_import', 0)")) 182 except Exception: 183 allow_imports = 0 184 src = sc.get_code() 185 dbg("source: %s" % src) 186 try: exec(src) in self.compldict 187 except: dbg("parser: %s, %s" % (sys.exc_info()[0],sys.exc_info()[1])) 188 for l in sc.locals: 189 # Executing import/from statements harvested from the buffer runs 190 # arbitrary package code; only do so when the user opted in. 191 if not allow_imports and (l.startswith('import') 192 or l.startswith('from ')): 193 continue 194 try: exec(l) in self.compldict 195 except: dbg("locals: %s, %s [%s]" % (sys.exc_info()[0],sys.exc_info()[1],l)) 196 197 def _cleanstr(self,doc): 198 return doc.replace('"',' ').replace("'",' ') 199 200 def get_arguments(self,func_obj): 201 def _ctor(obj): 202 try: return class_ob.__init__.im_func 203 except AttributeError: 204 for base in class_ob.__bases__: 205 rc = _find_constructor(base) 206 if rc is not None: return rc 207 return None 208 209 arg_offset = 1 210 if type(func_obj) == types.ClassType: func_obj = _ctor(func_obj) 211 elif type(func_obj) == types.MethodType: func_obj = func_obj.im_func 212 else: arg_offset = 0 213 214 arg_text='' 215 if type(func_obj) in [types.FunctionType, types.LambdaType]: 216 try: 217 cd = func_obj.func_code 218 real_args = cd.co_varnames[arg_offset:cd.co_argcount] 219 defaults = func_obj.func_defaults or '' 220 defaults = map(lambda name: "=%s" % name, defaults) 221 defaults = [""] * (len(real_args)-len(defaults)) + defaults 222 items = map(lambda a,d: a+d, real_args, defaults) 223 if func_obj.func_code.co_flags & 0x4: 224 items.append("...") 225 if func_obj.func_code.co_flags & 0x8: 226 items.append("***") 227 arg_text = (','.join(items)) + ')' 228 229 except: 230 dbg("arg completion: %s: %s" % (sys.exc_info()[0],sys.exc_info()[1])) 231 pass 232 if len(arg_text) == 0: 233 # The doc string sometimes contains the function signature 234 # this works for a lot of C modules that are part of the 235 # standard library 236 doc = func_obj.__doc__ 237 if doc: 238 doc = doc.lstrip() 239 pos = doc.find('\n') 240 if pos > 0: 241 sigline = doc[:pos] 242 lidx = sigline.find('(') 243 ridx = sigline.find(')') 244 if lidx > 0 and ridx > 0: 245 arg_text = sigline[lidx+1:ridx] + ')' 246 if len(arg_text) == 0: arg_text = ')' 247 return arg_text 248 249 def get_completions(self,context,match): 250 dbg("get_completions('%s','%s')" % (context,match)) 251 stmt = '' 252 if context: stmt += str(context) 253 if match: stmt += str(match) 254 try: 255 result = None 256 all = {} 257 ridx = stmt.rfind('.') 258 if len(stmt) > 0 and stmt[-1] == '(': 259 result = eval(_sanitize(stmt[:-1]), self.compldict) 260 doc = result.__doc__ 261 if doc is None: doc = '' 262 args = self.get_arguments(result) 263 return [{'word':self._cleanstr(args),'info':self._cleanstr(doc)}] 264 elif ridx == -1: 265 match = stmt 266 all = self.compldict 267 else: 268 match = stmt[ridx+1:] 269 stmt = _sanitize(stmt[:ridx]) 270 result = eval(stmt, self.compldict) 271 all = dir(result) 272 273 dbg("completing: stmt:%s" % stmt) 274 completions = [] 275 276 try: maindoc = result.__doc__ 277 except: maindoc = ' ' 278 if maindoc is None: maindoc = ' ' 279 for m in all: 280 if m == "_PyCmplNoType": continue #this is internal 281 try: 282 dbg('possible completion: %s' % m) 283 if m.find(match) == 0: 284 if result is None: inst = all[m] 285 else: inst = getattr(result,m) 286 try: doc = inst.__doc__ 287 except: doc = maindoc 288 typestr = str(inst) 289 if doc is None or doc == '': doc = maindoc 290 291 wrd = m[len(match):] 292 c = {'word':wrd, 'abbr':m, 'info':self._cleanstr(doc)} 293 if "function" in typestr: 294 c['word'] += '(' 295 c['abbr'] += '(' + self._cleanstr(self.get_arguments(inst)) 296 elif "method" in typestr: 297 c['word'] += '(' 298 c['abbr'] += '(' + self._cleanstr(self.get_arguments(inst)) 299 elif "module" in typestr: 300 c['word'] += '.' 301 elif "class" in typestr: 302 c['word'] += '(' 303 c['abbr'] += '(' 304 completions.append(c) 305 except: 306 i = sys.exc_info() 307 dbg("inner completion: %s,%s [stmt='%s']" % (i[0],i[1],stmt)) 308 return completions 309 except: 310 i = sys.exc_info() 311 dbg("completion: %s,%s [stmt='%s']" % (i[0],i[1],stmt)) 312 return [] 313 314 class Scope(object): 315 def __init__(self,name,indent,docstr=''): 316 self.subscopes = [] 317 self.docstr = docstr 318 self.locals = [] 319 self.parent = None 320 self.name = name 321 self.indent = indent 322 323 def add(self,sub): 324 #print 'push scope: [%s@%s]' % (sub.name,sub.indent) 325 sub.parent = self 326 self.subscopes.append(sub) 327 return sub 328 329 def doc(self,str): 330 """ Clean up a docstring """ 331 d = str.replace('\n',' ') 332 d = d.replace('\t',' ') 333 while d.find(' ') > -1: d = d.replace(' ',' ') 334 while d[0] in '"\'\t ': d = d[1:] 335 while d[-1] in '"\'\t ': d = d[:-1] 336 dbg("Scope(%s)::docstr = %s" % (self,d)) 337 self.docstr = d 338 339 def local(self,loc): 340 self._checkexisting(loc) 341 self.locals.append(loc) 342 343 def copy_decl(self,indent=0): 344 """ Copy a scope's declaration only, at the specified indent level - not local variables """ 345 return Scope(self.name,indent,self.docstr) 346 347 def _checkexisting(self,test): 348 "Convienance function... keep out duplicates" 349 if test.find('=') > -1: 350 var = test.split('=')[0].strip() 351 for l in self.locals: 352 if l.find('=') > -1 and var == l.split('=')[0].strip(): 353 self.locals.remove(l) 354 355 def get_code(self): 356 str = "" 357 if len(self.docstr) > 0: str += repr(self.docstr)+'\n' 358 str += 'class _PyCmplNoType:\n def __getattr__(self,name):\n return None\n' 359 for sub in self.subscopes: 360 str += sub.get_code() 361 for l in self.locals: 362 if not l.startswith('import') and not l.startswith('from '): str += l+'\n' 363 364 return str 365 366 def pop(self,indent): 367 #print 'pop scope: [%s] to [%s]' % (self.indent,indent) 368 outer = self 369 while outer.parent != None and outer.indent >= indent: 370 outer = outer.parent 371 return outer 372 373 def currentindent(self): 374 #print 'parse current indent: %s' % self.indent 375 return ' '*self.indent 376 377 def childindent(self): 378 #print 'parse child indent: [%s]' % (self.indent+1) 379 return ' '*(self.indent+1) 380 381 class Class(Scope): 382 def __init__(self, name, supers, indent, docstr=''): 383 Scope.__init__(self,name,indent, docstr) 384 self.supers = supers 385 def copy_decl(self,indent=0): 386 c = Class(self.name,self.supers,indent, self.docstr) 387 for s in self.subscopes: 388 c.add(s.copy_decl(indent+1)) 389 return c 390 def get_code(self): 391 str = '%sclass %s' % (self.currentindent(),self.name) 392 # Only include base class expressions that are pure dotted names. 393 # Anything else (calls, subscripts, conditionals, ...) is dropped 394 # because exec() would evaluate it at class-definition time. See 395 # the security note in the file header. 396 safe_supers = [s.strip() for s in self.supers 397 if _DOTTED_NAME_RE.match(s.strip())] 398 if len(safe_supers) > 0: str += '(%s)' % ','.join(safe_supers) 399 str += ':\n' 400 if len(self.docstr) > 0: str += self.childindent()+repr(self.docstr)+'\n' 401 if len(self.subscopes) > 0: 402 for s in self.subscopes: str += s.get_code() 403 else: 404 str += '%spass\n' % self.childindent() 405 return str 406 407 408 class Function(Scope): 409 def __init__(self, name, params, indent, docstr=''): 410 Scope.__init__(self,name,indent, docstr) 411 self.params = params 412 def copy_decl(self,indent=0): 413 return Function(self.name,self.params,indent, self.docstr) 414 def get_code(self): 415 # Strip default values and annotations from each parameter before 416 # joining: exec() evaluates these at definition time and a hostile 417 # buffer could otherwise execute arbitrary code via crafted def 418 # headers. See file header for details. 419 safe_params = [_strip_param(p) for p in self.params] 420 safe_params = [p for p in safe_params if p] 421 str = "%sdef %s(%s):\n" % \ 422 (self.currentindent(),self.name,','.join(safe_params)) 423 if len(self.docstr) > 0: str += self.childindent()+repr(self.docstr)+'\n' 424 str += "%spass\n" % self.childindent() 425 return str 426 427 class PyParser: 428 def __init__(self): 429 self.top = Scope('global',0) 430 self.scope = self.top 431 self.parserline = 0 432 433 def _parsedotname(self,pre=None): 434 #returns (dottedname, nexttoken) 435 name = [] 436 if pre is None: 437 tokentype, token, indent = self.next() 438 if tokentype != NAME and token != '*': 439 return ('', token) 440 else: token = pre 441 name.append(token) 442 while True: 443 tokentype, token, indent = self.next() 444 if token != '.': break 445 tokentype, token, indent = self.next() 446 if tokentype != NAME: break 447 name.append(token) 448 return (".".join(name), token) 449 450 def _parseimportlist(self): 451 imports = [] 452 while True: 453 name, token = self._parsedotname() 454 if not name: break 455 name2 = '' 456 if token == 'as': name2, token = self._parsedotname() 457 imports.append((name, name2)) 458 while token != "," and "\n" not in token: 459 tokentype, token, indent = self.next() 460 if token != ",": break 461 return imports 462 463 def _parenparse(self): 464 name = '' 465 names = [] 466 level = 1 467 while True: 468 tokentype, token, indent = self.next() 469 if token in (')', ',') and level == 1: 470 if '=' not in name: name = name.replace(' ', '') 471 names.append(name.strip()) 472 name = '' 473 if token == '(': 474 level += 1 475 name += "(" 476 elif token == ')': 477 level -= 1 478 if level == 0: break 479 else: name += ")" 480 elif token == ',' and level == 1: 481 pass 482 else: 483 name += "%s " % str(token) 484 return names 485 486 def _parsefunction(self,indent): 487 self.scope=self.scope.pop(indent) 488 tokentype, fname, ind = self.next() 489 if tokentype != NAME: return None 490 491 tokentype, open, ind = self.next() 492 if open != '(': return None 493 params=self._parenparse() 494 495 tokentype, colon, ind = self.next() 496 if colon != ':': return None 497 498 return Function(fname,params,indent) 499 500 def _parseclass(self,indent): 501 self.scope=self.scope.pop(indent) 502 tokentype, cname, ind = self.next() 503 if tokentype != NAME: return None 504 505 super = [] 506 tokentype, next, ind = self.next() 507 if next == '(': 508 super=self._parenparse() 509 elif next != ':': return None 510 511 return Class(cname,super,indent) 512 513 def _parseassignment(self): 514 assign='' 515 tokentype, token, indent = self.next() 516 if tokentype == tokenize.STRING or token == 'str': 517 return '""' 518 elif token == '(' or token == 'tuple': 519 return '()' 520 elif token == '[' or token == 'list': 521 return '[]' 522 elif token == '{' or token == 'dict': 523 return '{}' 524 elif tokentype == tokenize.NUMBER: 525 return '0' 526 elif token == 'open' or token == 'file': 527 return 'file' 528 elif token == 'None': 529 return '_PyCmplNoType()' 530 elif token == 'type': 531 return 'type(_PyCmplNoType)' #only for method resolution 532 else: 533 assign += token 534 level = 0 535 while True: 536 tokentype, token, indent = self.next() 537 if token in ('(','{','['): 538 level += 1 539 elif token in (']','}',')'): 540 level -= 1 541 if level == 0: break 542 elif level == 0: 543 if token in (';','\n'): break 544 assign += token 545 return "%s" % assign 546 547 def next(self): 548 type, token, (lineno, indent), end, self.parserline = self.gen.next() 549 if lineno == self.curline: 550 #print 'line found [%s] scope=%s' % (line.replace('\n',''),self.scope.name) 551 self.currentscope = self.scope 552 return (type, token, indent) 553 554 def _adjustvisibility(self): 555 newscope = Scope('result',0) 556 scp = self.currentscope 557 while scp != None: 558 if type(scp) == Function: 559 slice = 0 560 #Handle 'self' params 561 if scp.parent != None and type(scp.parent) == Class: 562 slice = 1 563 newscope.local('%s = %s' % (scp.params[0],scp.parent.name)) 564 for p in scp.params[slice:]: 565 i = p.find('=') 566 if len(p) == 0: continue 567 pvar = '' 568 ptype = '' 569 if i == -1: 570 pvar = p 571 ptype = '_PyCmplNoType()' 572 else: 573 pvar = p[:i] 574 ptype = _sanitize(p[i+1:]) 575 if pvar.startswith('**'): 576 pvar = pvar[2:] 577 ptype = '{}' 578 elif pvar.startswith('*'): 579 pvar = pvar[1:] 580 ptype = '[]' 581 582 newscope.local('%s = %s' % (pvar,ptype)) 583 584 for s in scp.subscopes: 585 ns = s.copy_decl(0) 586 newscope.add(ns) 587 for l in scp.locals: newscope.local(l) 588 scp = scp.parent 589 590 self.currentscope = newscope 591 return self.currentscope 592 593 #p.parse(vim.current.buffer[:],vim.eval("line('.')")) 594 def parse(self,text,curline=0): 595 self.curline = int(curline) 596 buf = cStringIO.StringIO(''.join(text) + '\n') 597 self.gen = tokenize.generate_tokens(buf.readline) 598 self.currentscope = self.scope 599 600 try: 601 freshscope=True 602 while True: 603 tokentype, token, indent = self.next() 604 #dbg( 'main: token=[%s] indent=[%s]' % (token,indent)) 605 606 if tokentype == DEDENT or token == "pass": 607 self.scope = self.scope.pop(indent) 608 elif token == 'def': 609 func = self._parsefunction(indent) 610 if func is None: 611 print "function: syntax error..." 612 continue 613 dbg("new scope: function") 614 freshscope = True 615 self.scope = self.scope.add(func) 616 elif token == 'class': 617 cls = self._parseclass(indent) 618 if cls is None: 619 print "class: syntax error..." 620 continue 621 freshscope = True 622 dbg("new scope: class") 623 self.scope = self.scope.add(cls) 624 625 elif token == 'import': 626 imports = self._parseimportlist() 627 for mod, alias in imports: 628 loc = "import %s" % mod 629 if len(alias) > 0: loc += " as %s" % alias 630 self.scope.local(loc) 631 freshscope = False 632 elif token == 'from': 633 mod, token = self._parsedotname() 634 if not mod or token != "import": 635 print "from: syntax error..." 636 continue 637 names = self._parseimportlist() 638 for name, alias in names: 639 loc = "from %s import %s" % (mod,name) 640 if len(alias) > 0: loc += " as %s" % alias 641 self.scope.local(loc) 642 freshscope = False 643 elif tokentype == STRING: 644 if freshscope: self.scope.doc(token) 645 elif tokentype == NAME: 646 name,token = self._parsedotname(token) 647 if token == '=': 648 stmt = self._parseassignment() 649 dbg("parseassignment: %s = %s" % (name, stmt)) 650 if stmt != None: 651 self.scope.local("%s = %s" % (name,stmt)) 652 freshscope = False 653 except StopIteration: #thrown on EOF 654 pass 655 except: 656 dbg("parse error: %s, %s @ %s" % 657 (sys.exc_info()[0], sys.exc_info()[1], self.parserline)) 658 return self._adjustvisibility() 659 660 def _sanitize(str): 661 val = '' 662 level = 0 663 for c in str: 664 if c in ('(','{','['): 665 level += 1 666 elif c in (']','}',')'): 667 level -= 1 668 elif level == 0: 669 val += c 670 return val 671 672 sys.path.extend(['.','..']) 673 PYTHONEOF 674 endfunction 675 676 call s:DefPython() 677 " vim: set et ts=4: