Skip to content

Commit 2012864

Browse files
aaronmgriffinvim-scripts
authored andcommitted
Version 0.6
Bug fix version: * Fixed argument completion * Removed the 'kind' completions, as they are better indicated with real syntax * Added tuple assignment parsing (whoops, that was forgotten) * Fixed import handling when flattening scope (visiblity)
1 parent d9ef877 commit 2012864

1 file changed

Lines changed: 31 additions & 21 deletions

File tree

ftplugin/pythoncomplete.vim

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
"pythoncomplete.vim - Omni Completion for python
22
" Maintainer: Aaron Griffin <[email protected]>
3-
" Version: 0.5
4-
" Last Updated: 19 April 2006
3+
" Version: 0.6
4+
" Last Updated: 14 May 2006
55
"
6+
" v 0.6:
7+
" * Fixed argument completion
8+
" * Removed the 'kind' completions, as they are better indicated
9+
" with real syntax
10+
" * Added tuple assignment parsing (whoops, that was forgotten)
11+
" * Fixed import handling when flattening scope
12+
"
13+
" v 0.5:
614
" Yeah, I skipped a version number - 0.4 was never public.
715
" It was a bugfix version on top of 0.3. This is a complete
816
" rewrite.
917
"
18+
" Changes
1019
" TODO:
1120
" User defined docstrings aren't handled right...
1221
" 'info' item output can use some formatting work
@@ -28,7 +37,7 @@ function! pythoncomplete#Complete(findstart, base)
2837
if c =~ '\w'
2938
continue
3039
elseif ! c =~ '\.'
31-
idx = -1
40+
let idx = -1
3241
break
3342
else
3443
break
@@ -45,7 +54,7 @@ function! pythoncomplete#Complete(findstart, base)
4554
while idx > 0
4655
let idx -= 1
4756
let c = line[idx]
48-
if c =~ '\w' || c =~ '\.'
57+
if c =~ '\w' || c =~ '\.' || c == '('
4958
let cword = c . cword
5059
continue
5160
elseif strlen(cword) > 0 || idx == 0
@@ -86,7 +95,7 @@ def vimcomplete(context,match):
8695
dictstr += '"icase":0},'
8796
if dictstr[-1] == ',': dictstr = dictstr[:-1]
8897
dictstr += ']'
89-
dbg("dict: %s" % dictstr)
98+
#dbg("dict: %s" % dictstr)
9099
vim.command("silent let g:pythoncomplete_completions = %s" % dictstr)
91100
#dbg("Completion dict:\n%s" % all)
92101
except vim.error:
@@ -128,23 +137,23 @@ class Completer(object):
128137
elif type(func_obj) == types.MethodType: func_obj = func_obj.im_func
129138
else: arg_offset = 0
130139

131-
arg_text = ')'
140+
arg_text=''
132141
if type(func_obj) in [types.FunctionType, types.LambdaType]:
133142
try:
134143
cd = func_obj.func_code
135144
real_args = cd.co_varnames[arg_offset:cd.co_argcount]
136-
defaults = func_obj.func_defaults or []
137-
defaults = [map(lambda name: "=%s" % name, defaults)]
145+
defaults = func_obj.func_defaults or ''
146+
defaults = map(lambda name: "=%s" % name, defaults)
138147
defaults = [""] * (len(real_args)-len(defaults)) + defaults
139148
items = map(lambda a,d: a+d, real_args, defaults)
140149
if func_obj.func_code.co_flags & 0x4:
141150
items.append("...")
142151
if func_obj.func_code.co_flags & 0x8:
143152
items.append("***")
144-
arg_text = ", ".join(items) + ')'
153+
arg_text = (','.join(items)) + ')'
145154

146155
except:
147-
dbg("completion: %s: %s" % (sys.exc_info()[0],sys.exc_info()[1]))
156+
dbg("arg completion: %s: %s" % (sys.exc_info()[0],sys.exc_info()[1]))
148157
pass
149158
if len(arg_text) == 0:
150159
# The doc string sometimes contains the function signature
@@ -160,6 +169,7 @@ class Completer(object):
160169
ridx = sigline.find(')')
161170
if lidx > 0 and ridx > 0:
162171
arg_text = sigline[lidx+1:ridx] + ')'
172+
if len(arg_text) == 0: arg_text = ')'
163173
return arg_text
164174

165175
def get_completions(self,context,match):
@@ -172,12 +182,11 @@ class Completer(object):
172182
all = {}
173183
ridx = stmt.rfind('.')
174184
if len(stmt) > 0 and stmt[-1] == '(':
175-
#TODO
176185
result = eval(_sanitize(stmt[:-1]), self.compldict)
177186
doc = result.__doc__
178187
if doc == None: doc = ''
179-
args = self.get_arguments(res)
180-
return [{'word':self._cleanstr(args),'info':self._cleanstr(doc),'kind':'p'}]
188+
args = self.get_arguments(result)
189+
return [{'word':self._cleanstr(args),'info':self._cleanstr(doc)}]
181190
elif ridx == -1:
182191
match = stmt
183192
all = self.compldict
@@ -206,22 +215,18 @@ class Completer(object):
206215
if doc == None or doc == '': doc = maindoc
207216

208217
wrd = m[len(match):]
209-
c = {'word':wrd, 'abbr':m, 'info':self._cleanstr(doc),'kind':'m'}
218+
c = {'word':wrd, 'abbr':m, 'info':self._cleanstr(doc)}
210219
if "function" in typestr:
211220
c['word'] += '('
212221
c['abbr'] += '(' + self._cleanstr(self.get_arguments(inst))
213-
c['kind'] = 'f'
214222
elif "method" in typestr:
215223
c['word'] += '('
216224
c['abbr'] += '(' + self._cleanstr(self.get_arguments(inst))
217-
c['kind'] = 'f'
218225
elif "module" in typestr:
219226
c['word'] += '.'
220-
c['kind'] = 'm'
221227
elif "class" in typestr:
222228
c['word'] += '('
223229
c['abbr'] += '('
224-
c['kind']='c'
225230
completions.append(c)
226231
except:
227232
i = sys.exc_info()
@@ -277,10 +282,13 @@ class Scope(object):
277282
# we need to start with this, to fix up broken completions
278283
# hopefully this name is unique enough...
279284
str = '"""'+self.docstr+'"""\n'
285+
for l in self.locals:
286+
if l.startswith('import'): str += l+'\n'
280287
str += 'class _PyCmplNoType:\n def __getattr__(self,name):\n return None\n'
281288
for sub in self.subscopes:
282289
str += sub.get_code()
283-
#str += '\n'.join(self.locals)+'\n'
290+
for l in self.locals:
291+
if not l.startswith('import'): str += l+'\n'
284292

285293
return str
286294

@@ -420,6 +428,8 @@ class PyParser:
420428
tokentype, token, indent = self.next()
421429
if tokentype == tokenize.STRING or token == 'str':
422430
return '""'
431+
elif token == '(' or token == 'tuple':
432+
return '()'
423433
elif token == '[' or token == 'list':
424434
return '[]'
425435
elif token == '{' or token == 'dict':
@@ -494,9 +504,9 @@ class PyParser:
494504
freshscope=True
495505
while True:
496506
tokentype, token, indent = self.next()
497-
#print 'main: token=[%s] indent=[%s]' % (token,indent)
507+
#dbg( 'main: token=[%s] indent=[%s]' % (token,indent))
498508

499-
if tokentype == DEDENT:
509+
if tokentype == DEDENT or token == "pass":
500510
self.scope = self.scope.pop(indent)
501511
elif token == 'def':
502512
func = self._parsefunction(indent)

0 commit comments

Comments
 (0)