1 "pythoncomplete.vim - Omni Completion for python
2 " Maintainer: Aaron Griffin <aaronmgriffin@gmail.com>
4 " Last Updated: 19 Oct 2006
8 " User defined docstrings aren't handled right...
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??
16 " * Fixed function list sorting (_ and __ at the bottom)
17 " * Removed newline removal from docs. It appears vim handles these better in
21 " * Fixed argument completion
22 " * Removed the 'kind' completions, as they are better indicated
24 " * Added tuple assignment parsing (whoops, that was forgotten)
25 " * Fixed import handling when flattening scope
28 " Yeah, I skipped a version number - 0.4 was never public.
29 " It was a bugfix version on top of 0.3. This is a complete
34 echo "Error: Required vim compiled with +python"
38 function! pythoncomplete#Complete(findstart, base)
39 "findstart = 1 when we need to get the text length
41 let line = getline('.')
57 "findstart = 0 when we need to return the list of completions
59 "vim no longer moves the cursor upon completion... fix that
60 let line = getline('.')
66 if c =~ '\w' || c =~ '\.' || c == '('
69 elseif strlen(cword) > 0 || idx == 0
73 execute "python vimcomplete('" . cword . "', '" . a:base . "')"
74 return g:pythoncomplete_completions
78 function! s:DefPython()
80 import sys, tokenize, cStringIO, types
81 from token import NAME, DEDENT, NEWLINE, STRING
84 def dbg(s): debugstmts.append(s)
86 for d in debugstmts: print "DBG: %s " % d
88 def vimcomplete(context,match):
98 if xa[1] == '_' and ya[0:2] == '__':
100 elif ya[0:2] == '__':
113 cmpl.evalsource('\n'.join(vim.current.buffer),vim.eval("line('.')"))
114 all = cmpl.get_completions(context,match)
117 # have to do this for double quoting
120 for x in cmpl: dictstr += '"%s":"%s",' % (x,cmpl[x])
121 dictstr += '"icase":0},'
122 if dictstr[-1] == ',': dictstr = dictstr[:-1]
124 #dbg("dict: %s" % dictstr)
125 vim.command("silent let g:pythoncomplete_completions = %s" % dictstr)
126 #dbg("Completion dict:\n%s" % all)
128 dbg("VIM Error: %s" % vim.error)
130 class Completer(object):
133 self.parser = PyParser()
135 def evalsource(self,text,line=0):
136 sc = self.parser.parse(text,line)
138 dbg("source: %s" % src)
139 try: exec(src) in self.compldict
140 except: dbg("parser: %s, %s" % (sys.exc_info()[0],sys.exc_info()[1]))
142 try: exec(l) in self.compldict
143 except: dbg("locals: %s, %s [%s]" % (sys.exc_info()[0],sys.exc_info()[1],l))
145 def _cleanstr(self,doc):
146 return doc.replace('"',' ').replace("'",' ')
148 def get_arguments(self,func_obj):
150 try: return class_ob.__init__.im_func
151 except AttributeError:
152 for base in class_ob.__bases__:
153 rc = _find_constructor(base)
154 if rc is not None: return rc
158 if type(func_obj) == types.ClassType: func_obj = _ctor(func_obj)
159 elif type(func_obj) == types.MethodType: func_obj = func_obj.im_func
163 if type(func_obj) in [types.FunctionType, types.LambdaType]:
165 cd = func_obj.func_code
166 real_args = cd.co_varnames[arg_offset:cd.co_argcount]
167 defaults = func_obj.func_defaults or ''
168 defaults = map(lambda name: "=%s" % name, defaults)
169 defaults = [""] * (len(real_args)-len(defaults)) + defaults
170 items = map(lambda a,d: a+d, real_args, defaults)
171 if func_obj.func_code.co_flags & 0x4:
173 if func_obj.func_code.co_flags & 0x8:
175 arg_text = (','.join(items)) + ')'
178 dbg("arg completion: %s: %s" % (sys.exc_info()[0],sys.exc_info()[1]))
180 if len(arg_text) == 0:
181 # The doc string sometimes contains the function signature
182 # this works for alot of C modules that are part of the
184 doc = func_obj.__doc__
190 lidx = sigline.find('(')
191 ridx = sigline.find(')')
192 if lidx > 0 and ridx > 0:
193 arg_text = sigline[lidx+1:ridx] + ')'
194 if len(arg_text) == 0: arg_text = ')'
197 def get_completions(self,context,match):
198 dbg("get_completions('%s','%s')" % (context,match))
200 if context: stmt += str(context)
201 if match: stmt += str(match)
205 ridx = stmt.rfind('.')
206 if len(stmt) > 0 and stmt[-1] == '(':
207 result = eval(_sanitize(stmt[:-1]), self.compldict)
209 if doc == None: doc = ''
210 args = self.get_arguments(result)
211 return [{'word':self._cleanstr(args),'info':self._cleanstr(doc)}]
216 match = stmt[ridx+1:]
217 stmt = _sanitize(stmt[:ridx])
218 result = eval(stmt, self.compldict)
221 dbg("completing: stmt:%s" % stmt)
224 try: maindoc = result.__doc__
225 except: maindoc = ' '
226 if maindoc == None: maindoc = ' '
228 if m == "_PyCmplNoType": continue #this is internal
230 dbg('possible completion: %s' % m)
231 if m.find(match) == 0:
232 if result == None: inst = all[m]
233 else: inst = getattr(result,m)
234 try: doc = inst.__doc__
235 except: doc = maindoc
237 if doc == None or doc == '': doc = maindoc
240 c = {'word':wrd, 'abbr':m, 'info':self._cleanstr(doc)}
241 if "function" in typestr:
243 c['abbr'] += '(' + self._cleanstr(self.get_arguments(inst))
244 elif "method" in typestr:
246 c['abbr'] += '(' + self._cleanstr(self.get_arguments(inst))
247 elif "module" in typestr:
249 elif "class" in typestr:
252 completions.append(c)
255 dbg("inner completion: %s,%s [stmt='%s']" % (i[0],i[1],stmt))
259 dbg("completion: %s,%s [stmt='%s']" % (i[0],i[1],stmt))
263 def __init__(self,name,indent):
272 #print 'push scope: [%s@%s]' % (sub.name,sub.indent)
274 self.subscopes.append(sub)
278 """ Clean up a docstring """
279 d = str.replace('\n',' ')
280 d = d.replace('\t',' ')
281 while d.find(' ') > -1: d = d.replace(' ',' ')
282 while d[0] in '"\'\t ': d = d[1:]
283 while d[-1] in '"\'\t ': d = d[:-1]
287 if not self._hasvaralready(loc):
288 self.locals.append(loc)
290 def copy_decl(self,indent=0):
291 """ Copy a scope's declaration only, at the specified indent level - not local variables """
292 return Scope(self.name,indent)
294 def _hasvaralready(self,test):
295 "Convienance function... keep out duplicates"
296 if test.find('=') > -1:
297 var = test.split('=')[0].strip()
298 for l in self.locals:
299 if l.find('=') > -1 and var == l.split('=')[0].strip():
304 # we need to start with this, to fix up broken completions
305 # hopefully this name is unique enough...
306 str = '"""'+self.docstr+'"""\n'
307 for l in self.locals:
308 if l.startswith('import'): str += l+'\n'
309 str += 'class _PyCmplNoType:\n def __getattr__(self,name):\n return None\n'
310 for sub in self.subscopes:
311 str += sub.get_code()
312 for l in self.locals:
313 if not l.startswith('import'): str += l+'\n'
317 def pop(self,indent):
318 #print 'pop scope: [%s] to [%s]' % (self.indent,indent)
320 while outer.parent != None and outer.indent >= indent:
324 def currentindent(self):
325 #print 'parse current indent: %s' % self.indent
326 return ' '*self.indent
328 def childindent(self):
329 #print 'parse child indent: [%s]' % (self.indent+1)
330 return ' '*(self.indent+1)
333 def __init__(self, name, supers, indent):
334 Scope.__init__(self,name,indent)
336 def copy_decl(self,indent=0):
337 c = Class(self.name,self.supers,indent)
338 for s in self.subscopes:
339 c.add(s.copy_decl(indent+1))
342 str = '%sclass %s' % (self.currentindent(),self.name)
343 if len(self.supers) > 0: str += '(%s)' % ','.join(self.supers)
345 if len(self.docstr) > 0: str += self.childindent()+'"""'+self.docstr+'"""\n'
346 if len(self.subscopes) > 0:
347 for s in self.subscopes: str += s.get_code()
349 str += '%spass\n' % self.childindent()
353 class Function(Scope):
354 def __init__(self, name, params, indent):
355 Scope.__init__(self,name,indent)
357 def copy_decl(self,indent=0):
358 return Function(self.name,self.params,indent)
360 str = "%sdef %s(%s):\n" % \
361 (self.currentindent(),self.name,','.join(self.params))
362 if len(self.docstr) > 0: str += self.childindent()+'"""'+self.docstr+'"""\n'
363 str += "%spass\n" % self.childindent()
368 self.top = Scope('global',0)
369 self.scope = self.top
371 def _parsedotname(self,pre=None):
372 #returns (dottedname, nexttoken)
375 tokentype, token, indent = self.next()
376 if tokentype != NAME and token != '*':
381 tokentype, token, indent = self.next()
382 if token != '.': break
383 tokentype, token, indent = self.next()
384 if tokentype != NAME: break
386 return (".".join(name), token)
388 def _parseimportlist(self):
391 name, token = self._parsedotname()
394 if token == 'as': name2, token = self._parsedotname()
395 imports.append((name, name2))
396 while token != "," and "\n" not in token:
397 tokentype, token, indent = self.next()
398 if token != ",": break
401 def _parenparse(self):
406 tokentype, token, indent = self.next()
407 if token in (')', ',') and level == 1:
415 elif token == ',' and level == 1:
421 def _parsefunction(self,indent):
422 self.scope=self.scope.pop(indent)
423 tokentype, fname, ind = self.next()
424 if tokentype != NAME: return None
426 tokentype, open, ind = self.next()
427 if open != '(': return None
428 params=self._parenparse()
430 tokentype, colon, ind = self.next()
431 if colon != ':': return None
433 return Function(fname,params,indent)
435 def _parseclass(self,indent):
436 self.scope=self.scope.pop(indent)
437 tokentype, cname, ind = self.next()
438 if tokentype != NAME: return None
441 tokentype, next, ind = self.next()
443 super=self._parenparse()
444 elif next != ':': return None
446 return Class(cname,super,indent)
448 def _parseassignment(self):
450 tokentype, token, indent = self.next()
451 if tokentype == tokenize.STRING or token == 'str':
453 elif token == '(' or token == 'tuple':
455 elif token == '[' or token == 'list':
457 elif token == '{' or token == 'dict':
459 elif tokentype == tokenize.NUMBER:
461 elif token == 'open' or token == 'file':
463 elif token == 'None':
464 return '_PyCmplNoType()'
465 elif token == 'type':
466 return 'type(_PyCmplNoType)' #only for method resolution
471 tokentype, token, indent = self.next()
472 if token in ('(','{','['):
474 elif token in (']','}',')'):
478 if token in (';','\n'): break
483 type, token, (lineno, indent), end, self.parserline = self.gen.next()
484 if lineno == self.curline:
485 #print 'line found [%s] scope=%s' % (line.replace('\n',''),self.scope.name)
486 self.currentscope = self.scope
487 return (type, token, indent)
489 def _adjustvisibility(self):
490 newscope = Scope('result',0)
491 scp = self.currentscope
493 if type(scp) == Function:
495 #Handle 'self' params
496 if scp.parent != None and type(scp.parent) == Class:
500 if i != -1: p = p[:i]
501 newscope.local('%s = %s' % (scp.params[0],scp.parent.name))
502 for p in scp.params[slice:]:
505 newscope.local('%s = _PyCmplNoType()' % p)
507 newscope.local('%s = %s' % (p[:i],_sanitize(p[i+1])))
509 for s in scp.subscopes:
512 for l in scp.locals: newscope.local(l)
515 self.currentscope = newscope
516 return self.currentscope
518 #p.parse(vim.current.buffer[:],vim.eval("line('.')"))
519 def parse(self,text,curline=0):
520 self.curline = int(curline)
521 buf = cStringIO.StringIO(''.join(text) + '\n')
522 self.gen = tokenize.generate_tokens(buf.readline)
523 self.currentscope = self.scope
528 tokentype, token, indent = self.next()
529 #dbg( 'main: token=[%s] indent=[%s]' % (token,indent))
531 if tokentype == DEDENT or token == "pass":
532 self.scope = self.scope.pop(indent)
534 func = self._parsefunction(indent)
536 print "function: syntax error..."
539 self.scope = self.scope.add(func)
540 elif token == 'class':
541 cls = self._parseclass(indent)
543 print "class: syntax error..."
546 self.scope = self.scope.add(cls)
548 elif token == 'import':
549 imports = self._parseimportlist()
550 for mod, alias in imports:
551 loc = "import %s" % mod
552 if len(alias) > 0: loc += " as %s" % alias
553 self.scope.local(loc)
555 elif token == 'from':
556 mod, token = self._parsedotname()
557 if not mod or token != "import":
558 print "from: syntax error..."
560 names = self._parseimportlist()
561 for name, alias in names:
562 loc = "from %s import %s" % (mod,name)
563 if len(alias) > 0: loc += " as %s" % alias
564 self.scope.local(loc)
566 elif tokentype == STRING:
567 if freshscope: self.scope.doc(token)
568 elif tokentype == NAME:
569 name,token = self._parsedotname(token)
571 stmt = self._parseassignment()
573 self.scope.local("%s = %s" % (name,stmt))
575 except StopIteration: #thrown on EOF
578 dbg("parse error: %s, %s @ %s" %
579 (sys.exc_info()[0], sys.exc_info()[1], self.parserline))
580 return self._adjustvisibility()
586 if c in ('(','{','['):
588 elif c in (']','}',')'):
594 sys.path.extend(['.','..'])