annotate runtime/autoload/pythoncomplete.vim @ 1297:e2680bc6a180 v7.1.011

updated for version 7.1-011
author vimboss
date Tue, 19 Jun 2007 18:27:18 +0000
parents e63691e7c504
children f63ace015c63
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
1 "pythoncomplete.vim - Omni Completion for python
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
2 " Maintainer: Aaron Griffin <aaronmgriffin@gmail.com>
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
3 " Version: 0.7
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
4 " Last Updated: 19 Oct 2006
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
5 "
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
6 " Changes
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
7 " TODO:
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
8 " User defined docstrings aren't handled right...
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
9 " 'info' item output can use some formatting work
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
10 " Add an "unsafe eval" mode, to allow for return type evaluation
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
11 " Complete basic syntax along with import statements
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
12 " i.e. "import url<c-x,c-o>"
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
13 " Continue parsing on invalid line??
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
14 "
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
15 " v 0.7
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
16 " * Fixed function list sorting (_ and __ at the bottom)
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
17 " * Removed newline removal from docs. It appears vim handles these better in
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
18 " recent patches
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
19 "
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
20 " v 0.6:
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
21 " * Fixed argument completion
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
22 " * Removed the 'kind' completions, as they are better indicated
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
23 " with real syntax
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
24 " * Added tuple assignment parsing (whoops, that was forgotten)
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
25 " * Fixed import handling when flattening scope
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
26 "
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
27 " v 0.5:
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
28 " Yeah, I skipped a version number - 0.4 was never public.
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
29 " It was a bugfix version on top of 0.3. This is a complete
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
30 " rewrite.
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
31 "
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
32
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
33 if !has('python')
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
34 echo "Error: Required vim compiled with +python"
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
35 finish
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
36 endif
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
37
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
38 function! pythoncomplete#Complete(findstart, base)
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
39 "findstart = 1 when we need to get the text length
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
40 if a:findstart == 1
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
41 let line = getline('.')
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
42 let idx = col('.')
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
43 while idx > 0
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
44 let idx -= 1
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
45 let c = line[idx]
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
46 if c =~ '\w'
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
47 continue
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
48 elseif ! c =~ '\.'
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
49 let idx = -1
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
50 break
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
51 else
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
52 break
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
53 endif
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
54 endwhile
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
55
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
56 return idx
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
57 "findstart = 0 when we need to return the list of completions
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
58 else
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
59 "vim no longer moves the cursor upon completion... fix that
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
60 let line = getline('.')
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
61 let idx = col('.')
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
62 let cword = ''
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
63 while idx > 0
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
64 let idx -= 1
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
65 let c = line[idx]
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
66 if c =~ '\w' || c =~ '\.' || c == '('
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
67 let cword = c . cword
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
68 continue
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
69 elseif strlen(cword) > 0 || idx == 0
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
70 break
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
71 endif
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
72 endwhile
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
73 execute "python vimcomplete('" . cword . "', '" . a:base . "')"
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
74 return g:pythoncomplete_completions
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
75 endif
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
76 endfunction
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
77
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
78 function! s:DefPython()
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
79 python << PYTHONEOF
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
80 import sys, tokenize, cStringIO, types
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
81 from token import NAME, DEDENT, NEWLINE, STRING
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
82
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
83 debugstmts=[]
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
84 def dbg(s): debugstmts.append(s)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
85 def showdbg():
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
86 for d in debugstmts: print "DBG: %s " % d
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
87
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
88 def vimcomplete(context,match):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
89 global debugstmts
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
90 debugstmts = []
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
91 try:
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
92 import vim
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
93 def complsort(x,y):
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
94 try:
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
95 xa = x['abbr']
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
96 ya = y['abbr']
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
97 if xa[0] == '_':
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
98 if xa[1] == '_' and ya[0:2] == '__':
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
99 return xa > ya
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
100 elif ya[0:2] == '__':
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
101 return -1
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
102 elif y[0] == '_':
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
103 return xa > ya
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
104 else:
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
105 return 1
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
106 elif ya[0] == '_':
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
107 return -1
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
108 else:
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
109 return xa > ya
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
110 except:
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
111 return 0
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
112 cmpl = Completer()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
113 cmpl.evalsource('\n'.join(vim.current.buffer),vim.eval("line('.')"))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
114 all = cmpl.get_completions(context,match)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
115 all.sort(complsort)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
116 dictstr = '['
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
117 # have to do this for double quoting
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
118 for cmpl in all:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
119 dictstr += '{'
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
120 for x in cmpl: dictstr += '"%s":"%s",' % (x,cmpl[x])
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
121 dictstr += '"icase":0},'
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
122 if dictstr[-1] == ',': dictstr = dictstr[:-1]
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
123 dictstr += ']'
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
124 #dbg("dict: %s" % dictstr)
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
125 vim.command("silent let g:pythoncomplete_completions = %s" % dictstr)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
126 #dbg("Completion dict:\n%s" % all)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
127 except vim.error:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
128 dbg("VIM Error: %s" % vim.error)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
129
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
130 class Completer(object):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
131 def __init__(self):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
132 self.compldict = {}
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
133 self.parser = PyParser()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
134
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
135 def evalsource(self,text,line=0):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
136 sc = self.parser.parse(text,line)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
137 src = sc.get_code()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
138 dbg("source: %s" % src)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
139 try: exec(src) in self.compldict
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
140 except: dbg("parser: %s, %s" % (sys.exc_info()[0],sys.exc_info()[1]))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
141 for l in sc.locals:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
142 try: exec(l) in self.compldict
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
143 except: dbg("locals: %s, %s [%s]" % (sys.exc_info()[0],sys.exc_info()[1],l))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
144
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
145 def _cleanstr(self,doc):
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
146 return doc.replace('"',' ').replace("'",' ')
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
147
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
148 def get_arguments(self,func_obj):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
149 def _ctor(obj):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
150 try: return class_ob.__init__.im_func
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
151 except AttributeError:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
152 for base in class_ob.__bases__:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
153 rc = _find_constructor(base)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
154 if rc is not None: return rc
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
155 return None
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
156
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
157 arg_offset = 1
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
158 if type(func_obj) == types.ClassType: func_obj = _ctor(func_obj)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
159 elif type(func_obj) == types.MethodType: func_obj = func_obj.im_func
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
160 else: arg_offset = 0
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
161
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
162 arg_text=''
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
163 if type(func_obj) in [types.FunctionType, types.LambdaType]:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
164 try:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
165 cd = func_obj.func_code
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
166 real_args = cd.co_varnames[arg_offset:cd.co_argcount]
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
167 defaults = func_obj.func_defaults or ''
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
168 defaults = map(lambda name: "=%s" % name, defaults)
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
169 defaults = [""] * (len(real_args)-len(defaults)) + defaults
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
170 items = map(lambda a,d: a+d, real_args, defaults)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
171 if func_obj.func_code.co_flags & 0x4:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
172 items.append("...")
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
173 if func_obj.func_code.co_flags & 0x8:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
174 items.append("***")
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
175 arg_text = (','.join(items)) + ')'
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
176
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
177 except:
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
178 dbg("arg completion: %s: %s" % (sys.exc_info()[0],sys.exc_info()[1]))
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
179 pass
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
180 if len(arg_text) == 0:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
181 # The doc string sometimes contains the function signature
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
182 # this works for alot of C modules that are part of the
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
183 # standard library
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
184 doc = func_obj.__doc__
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
185 if doc:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
186 doc = doc.lstrip()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
187 pos = doc.find('\n')
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
188 if pos > 0:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
189 sigline = doc[:pos]
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
190 lidx = sigline.find('(')
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
191 ridx = sigline.find(')')
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
192 if lidx > 0 and ridx > 0:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
193 arg_text = sigline[lidx+1:ridx] + ')'
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
194 if len(arg_text) == 0: arg_text = ')'
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
195 return arg_text
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
196
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
197 def get_completions(self,context,match):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
198 dbg("get_completions('%s','%s')" % (context,match))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
199 stmt = ''
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
200 if context: stmt += str(context)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
201 if match: stmt += str(match)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
202 try:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
203 result = None
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
204 all = {}
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
205 ridx = stmt.rfind('.')
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
206 if len(stmt) > 0 and stmt[-1] == '(':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
207 result = eval(_sanitize(stmt[:-1]), self.compldict)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
208 doc = result.__doc__
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
209 if doc == None: doc = ''
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
210 args = self.get_arguments(result)
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
211 return [{'word':self._cleanstr(args),'info':self._cleanstr(doc)}]
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
212 elif ridx == -1:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
213 match = stmt
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
214 all = self.compldict
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
215 else:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
216 match = stmt[ridx+1:]
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
217 stmt = _sanitize(stmt[:ridx])
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
218 result = eval(stmt, self.compldict)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
219 all = dir(result)
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
220
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
221 dbg("completing: stmt:%s" % stmt)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
222 completions = []
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
223
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
224 try: maindoc = result.__doc__
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
225 except: maindoc = ' '
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
226 if maindoc == None: maindoc = ' '
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
227 for m in all:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
228 if m == "_PyCmplNoType": continue #this is internal
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
229 try:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
230 dbg('possible completion: %s' % m)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
231 if m.find(match) == 0:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
232 if result == None: inst = all[m]
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
233 else: inst = getattr(result,m)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
234 try: doc = inst.__doc__
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
235 except: doc = maindoc
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
236 typestr = str(inst)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
237 if doc == None or doc == '': doc = maindoc
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
238
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
239 wrd = m[len(match):]
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
240 c = {'word':wrd, 'abbr':m, 'info':self._cleanstr(doc)}
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
241 if "function" in typestr:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
242 c['word'] += '('
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
243 c['abbr'] += '(' + self._cleanstr(self.get_arguments(inst))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
244 elif "method" in typestr:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
245 c['word'] += '('
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
246 c['abbr'] += '(' + self._cleanstr(self.get_arguments(inst))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
247 elif "module" in typestr:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
248 c['word'] += '.'
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
249 elif "class" in typestr:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
250 c['word'] += '('
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
251 c['abbr'] += '('
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
252 completions.append(c)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
253 except:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
254 i = sys.exc_info()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
255 dbg("inner completion: %s,%s [stmt='%s']" % (i[0],i[1],stmt))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
256 return completions
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
257 except:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
258 i = sys.exc_info()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
259 dbg("completion: %s,%s [stmt='%s']" % (i[0],i[1],stmt))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
260 return []
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
261
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
262 class Scope(object):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
263 def __init__(self,name,indent):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
264 self.subscopes = []
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
265 self.docstr = ''
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
266 self.locals = []
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
267 self.parent = None
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
268 self.name = name
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
269 self.indent = indent
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
270
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
271 def add(self,sub):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
272 #print 'push scope: [%s@%s]' % (sub.name,sub.indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
273 sub.parent = self
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
274 self.subscopes.append(sub)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
275 return sub
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
276
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
277 def doc(self,str):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
278 """ Clean up a docstring """
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
279 d = str.replace('\n',' ')
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
280 d = d.replace('\t',' ')
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
281 while d.find(' ') > -1: d = d.replace(' ',' ')
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
282 while d[0] in '"\'\t ': d = d[1:]
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
283 while d[-1] in '"\'\t ': d = d[:-1]
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
284 self.docstr = d
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
285
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
286 def local(self,loc):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
287 if not self._hasvaralready(loc):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
288 self.locals.append(loc)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
289
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
290 def copy_decl(self,indent=0):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
291 """ Copy a scope's declaration only, at the specified indent level - not local variables """
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
292 return Scope(self.name,indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
293
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
294 def _hasvaralready(self,test):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
295 "Convienance function... keep out duplicates"
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
296 if test.find('=') > -1:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
297 var = test.split('=')[0].strip()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
298 for l in self.locals:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
299 if l.find('=') > -1 and var == l.split('=')[0].strip():
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
300 return True
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
301 return False
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
302
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
303 def get_code(self):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
304 # we need to start with this, to fix up broken completions
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
305 # hopefully this name is unique enough...
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
306 str = '"""'+self.docstr+'"""\n'
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
307 for l in self.locals:
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
308 if l.startswith('import'): str += l+'\n'
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
309 str += 'class _PyCmplNoType:\n def __getattr__(self,name):\n return None\n'
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
310 for sub in self.subscopes:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
311 str += sub.get_code()
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
312 for l in self.locals:
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
313 if not l.startswith('import'): str += l+'\n'
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
314
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
315 return str
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
316
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
317 def pop(self,indent):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
318 #print 'pop scope: [%s] to [%s]' % (self.indent,indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
319 outer = self
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
320 while outer.parent != None and outer.indent >= indent:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
321 outer = outer.parent
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
322 return outer
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
323
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
324 def currentindent(self):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
325 #print 'parse current indent: %s' % self.indent
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
326 return ' '*self.indent
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
327
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
328 def childindent(self):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
329 #print 'parse child indent: [%s]' % (self.indent+1)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
330 return ' '*(self.indent+1)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
331
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
332 class Class(Scope):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
333 def __init__(self, name, supers, indent):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
334 Scope.__init__(self,name,indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
335 self.supers = supers
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
336 def copy_decl(self,indent=0):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
337 c = Class(self.name,self.supers,indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
338 for s in self.subscopes:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
339 c.add(s.copy_decl(indent+1))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
340 return c
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
341 def get_code(self):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
342 str = '%sclass %s' % (self.currentindent(),self.name)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
343 if len(self.supers) > 0: str += '(%s)' % ','.join(self.supers)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
344 str += ':\n'
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
345 if len(self.docstr) > 0: str += self.childindent()+'"""'+self.docstr+'"""\n'
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
346 if len(self.subscopes) > 0:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
347 for s in self.subscopes: str += s.get_code()
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
348 else:
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
349 str += '%spass\n' % self.childindent()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
350 return str
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
351
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
352
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
353 class Function(Scope):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
354 def __init__(self, name, params, indent):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
355 Scope.__init__(self,name,indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
356 self.params = params
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
357 def copy_decl(self,indent=0):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
358 return Function(self.name,self.params,indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
359 def get_code(self):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
360 str = "%sdef %s(%s):\n" % \
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
361 (self.currentindent(),self.name,','.join(self.params))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
362 if len(self.docstr) > 0: str += self.childindent()+'"""'+self.docstr+'"""\n'
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
363 str += "%spass\n" % self.childindent()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
364 return str
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
365
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
366 class PyParser:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
367 def __init__(self):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
368 self.top = Scope('global',0)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
369 self.scope = self.top
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
370
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
371 def _parsedotname(self,pre=None):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
372 #returns (dottedname, nexttoken)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
373 name = []
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
374 if pre == None:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
375 tokentype, token, indent = self.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
376 if tokentype != NAME and token != '*':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
377 return ('', token)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
378 else: token = pre
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
379 name.append(token)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
380 while True:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
381 tokentype, token, indent = self.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
382 if token != '.': break
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
383 tokentype, token, indent = self.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
384 if tokentype != NAME: break
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
385 name.append(token)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
386 return (".".join(name), token)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
387
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
388 def _parseimportlist(self):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
389 imports = []
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
390 while True:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
391 name, token = self._parsedotname()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
392 if not name: break
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
393 name2 = ''
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
394 if token == 'as': name2, token = self._parsedotname()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
395 imports.append((name, name2))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
396 while token != "," and "\n" not in token:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
397 tokentype, token, indent = self.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
398 if token != ",": break
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
399 return imports
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
400
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
401 def _parenparse(self):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
402 name = ''
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
403 names = []
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
404 level = 1
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
405 while True:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
406 tokentype, token, indent = self.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
407 if token in (')', ',') and level == 1:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
408 names.append(name)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
409 name = ''
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
410 if token == '(':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
411 level += 1
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
412 elif token == ')':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
413 level -= 1
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
414 if level == 0: break
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
415 elif token == ',' and level == 1:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
416 pass
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
417 else:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
418 name += str(token)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
419 return names
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
420
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
421 def _parsefunction(self,indent):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
422 self.scope=self.scope.pop(indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
423 tokentype, fname, ind = self.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
424 if tokentype != NAME: return None
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
425
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
426 tokentype, open, ind = self.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
427 if open != '(': return None
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
428 params=self._parenparse()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
429
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
430 tokentype, colon, ind = self.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
431 if colon != ':': return None
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
432
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
433 return Function(fname,params,indent)
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
434
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
435 def _parseclass(self,indent):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
436 self.scope=self.scope.pop(indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
437 tokentype, cname, ind = self.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
438 if tokentype != NAME: return None
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
439
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
440 super = []
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
441 tokentype, next, ind = self.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
442 if next == '(':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
443 super=self._parenparse()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
444 elif next != ':': return None
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
445
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
446 return Class(cname,super,indent)
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
447
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
448 def _parseassignment(self):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
449 assign=''
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
450 tokentype, token, indent = self.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
451 if tokentype == tokenize.STRING or token == 'str':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
452 return '""'
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
453 elif token == '(' or token == 'tuple':
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
454 return '()'
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
455 elif token == '[' or token == 'list':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
456 return '[]'
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
457 elif token == '{' or token == 'dict':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
458 return '{}'
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
459 elif tokentype == tokenize.NUMBER:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
460 return '0'
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
461 elif token == 'open' or token == 'file':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
462 return 'file'
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
463 elif token == 'None':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
464 return '_PyCmplNoType()'
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
465 elif token == 'type':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
466 return 'type(_PyCmplNoType)' #only for method resolution
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
467 else:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
468 assign += token
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
469 level = 0
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
470 while True:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
471 tokentype, token, indent = self.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
472 if token in ('(','{','['):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
473 level += 1
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
474 elif token in (']','}',')'):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
475 level -= 1
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
476 if level == 0: break
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
477 elif level == 0:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
478 if token in (';','\n'): break
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
479 assign += token
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
480 return "%s" % assign
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
481
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
482 def next(self):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
483 type, token, (lineno, indent), end, self.parserline = self.gen.next()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
484 if lineno == self.curline:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
485 #print 'line found [%s] scope=%s' % (line.replace('\n',''),self.scope.name)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
486 self.currentscope = self.scope
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
487 return (type, token, indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
488
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
489 def _adjustvisibility(self):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
490 newscope = Scope('result',0)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
491 scp = self.currentscope
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
492 while scp != None:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
493 if type(scp) == Function:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
494 slice = 0
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
495 #Handle 'self' params
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
496 if scp.parent != None and type(scp.parent) == Class:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
497 slice = 1
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
498 p = scp.params[0]
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
499 i = p.find('=')
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
500 if i != -1: p = p[:i]
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
501 newscope.local('%s = %s' % (scp.params[0],scp.parent.name))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
502 for p in scp.params[slice:]:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
503 i = p.find('=')
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
504 if i == -1:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
505 newscope.local('%s = _PyCmplNoType()' % p)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
506 else:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
507 newscope.local('%s = %s' % (p[:i],_sanitize(p[i+1])))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
508
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
509 for s in scp.subscopes:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
510 ns = s.copy_decl(0)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
511 newscope.add(ns)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
512 for l in scp.locals: newscope.local(l)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
513 scp = scp.parent
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
514
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
515 self.currentscope = newscope
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
516 return self.currentscope
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
517
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
518 #p.parse(vim.current.buffer[:],vim.eval("line('.')"))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
519 def parse(self,text,curline=0):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
520 self.curline = int(curline)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
521 buf = cStringIO.StringIO(''.join(text) + '\n')
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
522 self.gen = tokenize.generate_tokens(buf.readline)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
523 self.currentscope = self.scope
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
524
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
525 try:
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
526 freshscope=True
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
527 while True:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
528 tokentype, token, indent = self.next()
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
529 #dbg( 'main: token=[%s] indent=[%s]' % (token,indent))
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
530
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 838
diff changeset
531 if tokentype == DEDENT or token == "pass":
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
532 self.scope = self.scope.pop(indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
533 elif token == 'def':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
534 func = self._parsefunction(indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
535 if func == None:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
536 print "function: syntax error..."
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
537 continue
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
538 freshscope = True
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
539 self.scope = self.scope.add(func)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
540 elif token == 'class':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
541 cls = self._parseclass(indent)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
542 if cls == None:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
543 print "class: syntax error..."
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
544 continue
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
545 freshscope = True
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
546 self.scope = self.scope.add(cls)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
547
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
548 elif token == 'import':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
549 imports = self._parseimportlist()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
550 for mod, alias in imports:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
551 loc = "import %s" % mod
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
552 if len(alias) > 0: loc += " as %s" % alias
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
553 self.scope.local(loc)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
554 freshscope = False
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
555 elif token == 'from':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
556 mod, token = self._parsedotname()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
557 if not mod or token != "import":
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
558 print "from: syntax error..."
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
559 continue
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
560 names = self._parseimportlist()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
561 for name, alias in names:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
562 loc = "from %s import %s" % (mod,name)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
563 if len(alias) > 0: loc += " as %s" % alias
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
564 self.scope.local(loc)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
565 freshscope = False
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
566 elif tokentype == STRING:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
567 if freshscope: self.scope.doc(token)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
568 elif tokentype == NAME:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
569 name,token = self._parsedotname(token)
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
570 if token == '=':
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
571 stmt = self._parseassignment()
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
572 if stmt != None:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
573 self.scope.local("%s = %s" % (name,stmt))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
574 freshscope = False
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
575 except StopIteration: #thrown on EOF
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
576 pass
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
577 except:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
578 dbg("parse error: %s, %s @ %s" %
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
579 (sys.exc_info()[0], sys.exc_info()[1], self.parserline))
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
580 return self._adjustvisibility()
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
581
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
582 def _sanitize(str):
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
583 val = ''
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
584 level = 0
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
585 for c in str:
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
586 if c in ('(','{','['):
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
587 level += 1
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
588 elif c in (']','}',')'):
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
589 level -= 1
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
590 elif level == 0:
838
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
591 val += c
8e5830943bff updated for version 7.0e04
vimboss
parents: 827
diff changeset
592 return val
827
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
593
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
594 sys.path.extend(['.','..'])
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
595 PYTHONEOF
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
596 endfunction
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
597
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
598 call s:DefPython()
fd1b3406fd1c updated for version 7.0d02
vimboss
parents:
diff changeset
599 " vim: set et ts=4: