annotate runtime/autoload/pycomplete.vim @ 823:9ab23f1e137f v7.0c12

updated for version 7.0c12
author vimboss
date Fri, 07 Apr 2006 21:40:07 +0000
parents e4fa26ce8769
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
1 "pycomplete.vim - Omni Completion for python
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
2 " Maintainer: Aaron Griffin
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
3 " Version: 0.3
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
4 " Last Updated: 23 January 2006
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
5 "
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
6 " v0.3 Changes:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
7 " added top level def parsing
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
8 " for safety, call returns are not evaluated
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
9 " handful of parsing changes
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
10 " trailing ( and . characters
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
11 " argument completion on open parens
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
12 " stop parsing at current line - ++performance, local var resolution
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
13 "
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
14 " TODO
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
15 " RExec subclass
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
16 " Code cleanup + make class
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
17 " use internal dict, not globals()
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
18
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
19 if !has('python')
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
20 echo "Error: Required vim compiled with +python"
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
21 finish
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
22 endif
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
23
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
24 function! pycomplete#Complete(findstart, base)
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
25 "findstart = 1 when we need to get the text length
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
26 if a:findstart
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
27 let line = getline('.')
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
28 let idx = col('.')
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
29 while idx > 0
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
30 let idx -= 1
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
31 let c = line[idx-1]
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
32 if c =~ '\w'
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
33 continue
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
34 elseif ! c =~ '\.'
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
35 idx = -1
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
36 break
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
37 else
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
38 break
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
39 endif
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
40 endwhile
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
41
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
42 return idx
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
43 "findstart = 0 when we need to return the list of completions
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
44 else
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
45 execute "python get_completions('" . a:base . "')"
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
46 return g:pycomplete_completions
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
47 endif
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
48 endfunction
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
49
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
50 function! s:DefPython()
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
51 python << PYTHONEOF
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
52 import vim, sys, types
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
53 import __builtin__
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
54 import tokenize, keyword, cStringIO
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
55
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
56 LOCALDEFS = \
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
57 ['LOCALDEFS', 'clean_up','eval_source_code', \
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
58 'get_completions', '__builtin__', '__builtins__', \
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
59 'dbg', '__name__', 'vim', 'sys', 'parse_to_end', \
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
60 'parse_statement', 'tokenize', 'keyword', 'cStringIO', \
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
61 'debug_level', 'safe_eval', '_ctor', 'get_arguments', \
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
62 'strip_calls', 'types', 'parse_block']
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
63
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
64 def dbg(level,msg):
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
65 debug_level = 1
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
66 try:
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
67 debug_level = vim.eval("g:pycomplete_debug_level")
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
68 except:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
69 pass
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
70 if level <= debug_level: print(msg)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
71
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
72 def strip_calls(stmt):
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
73 parsed=''
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
74 level = 0
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
75 for c in stmt:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
76 if c in ['[','(']:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
77 level += 1
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
78 elif c in [')',']']:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
79 level -= 1
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
80 elif level == 0:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
81 parsed += c
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
82 ##dbg(10,"stripped: %s" % parsed)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
83 return parsed
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
84
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
85 def get_completions(base):
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
86 stmt = vim.eval('expand("<cWORD>")')
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
87 #dbg(1,"statement: %s - %s" % (stmt, base))
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
88 stmt = stmt+base
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
89 eval_source_code()
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
90
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
91 try:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
92 ridx = stmt.rfind('.')
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
93 if stmt[-1] == '(':
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
94 match = ""
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
95 stmt = strip_calls(stmt[:len(stmt)-1])
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
96 all = get_arguments(eval(stmt))
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
97 elif ridx == -1:
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
98 match = stmt
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
99 all = globals() + __builtin__.__dict__
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
100 else:
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
101 match = stmt[ridx+1:]
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
102 stmt = strip_calls(stmt[:ridx])
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
103 all = eval(stmt).__dict__
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
104
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
105 #dbg(15,"completions for: %s, match=%s" % (stmt,match))
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
106 completions = []
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
107 if type(all) == types.DictType:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
108 for m in all:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
109 if m.find('_') != 0 and m.find(match) == 0 and \
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
110 m not in LOCALDEFS:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
111 #dbg(25,"matched... %s, %s" % (m, m.find(match)))
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
112 typestr = str(all[m])
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
113 if "function" in typestr: m += '('
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
114 elif "method" in typestr: m += '('
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
115 elif "module" in typestr: m += '.'
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
116 elif "class" in typestr: m += '('
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
117 completions.append(m)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
118 completions.sort()
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
119 else:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
120 completions.append(all)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
121 #dbg(10,"all completions: %s" % completions)
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
122 vim.command("let g:pycomplete_completions = %s" % completions)
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
123 except:
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
124 vim.command("let g:pycomplete_completions = []")
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
125 #dbg(1,"exception: %s" % sys.exc_info()[1])
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
126 clean_up()
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
127
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
128 def get_arguments(func_obj):
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
129 def _ctor(obj):
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
130 try:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
131 return class_ob.__init__.im_func
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
132 except AttributeError:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
133 for base in class_ob.__bases__:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
134 rc = _find_constructor(base)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
135 if rc is not None: return rc
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
136 return None
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
137
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
138 arg_offset = 1
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
139 if type(func_obj) == types.ClassType: func_obj = _ctor(func_obj)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
140 elif type(func_obj) == types.MethodType: func_obj = func_obj.im_func
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
141 else: arg_offset = 0
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
142
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
143 #dbg(20,"%s, offset=%s" % (str(func_obj), arg_offset))
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
144
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
145 arg_text = ''
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
146 if type(func_obj) in [types.FunctionType, types.LambdaType]:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
147 try:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
148 cd = func_obj.func_code
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
149 real_args = cd.co_varnames[arg_offset:cd.co_argcount]
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
150 defaults = func_obj.func_defaults or []
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
151 defaults = list(map(lambda name: "=%s" % name, defaults))
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
152 defaults = [""] * (len(real_args)-len(defaults)) + defaults
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
153 items = map(lambda a,d: a+d, real_args, defaults)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
154 if func_obj.func_code.co_flags & 0x4:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
155 items.append("...")
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
156 if func_obj.func_code.co_flags & 0x8:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
157 items.append("***")
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
158 arg_text = ", ".join(items) + ')'
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
159
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
160 except:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
161 #dbg(1,"exception: %s" % sys.exc_info()[1])
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
162 pass
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
163 if len(arg_text) == 0:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
164 # The doc string sometimes contains the function signature
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
165 # this works for alot of C modules that are part of the
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
166 # standard library
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
167 doc = getattr(func_obj, '__doc__', '')
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
168 if doc:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
169 doc = doc.lstrip()
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
170 pos = doc.find('\n')
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
171 if pos > 0:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
172 sigline = doc[:pos]
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
173 lidx = sigline.find('(')
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
174 ridx = sigline.find(')')
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
175 retidx = sigline.find('->')
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
176 ret = sigline[retidx+2:].strip()
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
177 if lidx > 0 and ridx > 0:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
178 arg_text = sigline[lidx+1:ridx] + ')'
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
179 if len(ret) > 0: arg_text += ' #returns %s' % ret
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
180 #dbg(15,"argument completion: %s" % arg_text)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
181 return arg_text
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
182
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
183 def parse_to_end(gen):
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
184 stmt=''
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
185 level = 0
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
186 for type, str, begin, end, line in gen:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
187 if line == vim.eval('getline(\'.\')'): break
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
188 elif str == '\\': continue
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
189 elif str == ';':
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
190 break
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
191 elif type == tokenize.NEWLINE and level == 0:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
192 break
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
193 elif str in ['[','(']:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
194 level += 1
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
195 elif str in [')',']']:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
196 level -= 1
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
197 elif level == 0:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
198 stmt += str
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
199 #dbg(10,"current statement: %s" % stmt)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
200 return stmt
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
201
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
202 def parse_block(gen):
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
203 lines = []
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
204 level = 0
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
205 for type, str, begin, end, line in gen:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
206 if line.replace('\n','') == vim.eval('getline(\'.\')'): break
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
207 elif type == tokenize.INDENT:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
208 level += 1
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
209 elif type == tokenize.DEDENT:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
210 level -= 1
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
211 if level == 0: break;
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
212 else:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
213 stmt = parse_statement(gen,str)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
214 if len(stmt) > 0: lines.append(stmt)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
215 return lines
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
216
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
217 def parse_statement(gen,curstr=''):
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
218 var = curstr
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
219 type, str, begin, end, line = gen.next()
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
220 if str == '=':
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
221 type, str, begin, end, line = gen.next()
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
222 if type == tokenize.NEWLINE:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
223 return ''
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
224 elif type == tokenize.STRING or str == 'str':
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
225 return '%s = str' % var
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
226 elif str == '[' or str == 'list':
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
227 return '%s= list' % var
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
228 elif str == '{' or str == 'dict':
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
229 return '%s = dict' % var
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
230 elif type == tokenize.NUMBER:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
231 return '%s = 0' % var
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
232 elif str == 'Set':
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
233 return '%s = Set' % var
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
234 elif str == 'open' or str == 'file':
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
235 return '%s = file' % var
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
236 else:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
237 inst = str + parse_to_end(gen)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
238 if len(inst) > 0:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
239 #dbg(5,"found [%s = %s]" % (var, inst))
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
240 return '%s = %s' % (var, inst)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
241 return ''
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
242
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
243 def eval_source_code():
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
244 LINE=vim.eval('getline(\'.\')')
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
245 s = cStringIO.StringIO('\n'.join(vim.current.buffer[:]) + '\n')
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
246 g = tokenize.generate_tokens(s.readline)
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
247
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
248 stmts = []
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
249 lineNo = 0
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
250 try:
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
251 for type, str, begin, end, line in g:
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
252 if line.replace('\n','') == vim.eval('getline(\'.\')'): break
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
253 elif begin[0] == lineNo: continue
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
254 #junk
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
255 elif type == tokenize.INDENT or \
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
256 type == tokenize.DEDENT or \
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
257 type == tokenize.ERRORTOKEN or \
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
258 type == tokenize.ENDMARKER or \
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
259 type == tokenize.NEWLINE or \
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
260 type == tokenize.COMMENT:
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
261 continue
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
262 #import statement
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
263 elif str == 'import':
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
264 import_stmt=parse_to_end(g)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
265 if len(import_stmt) > 0:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
266 #dbg(5,"found [import %s]" % import_stmt)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
267 stmts.append("import %s" % import_stmt)
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
268 #import from statement
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
269 elif str == 'from':
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
270 type, str, begin, end, line = g.next()
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
271 mod = str
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
272
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
273 type, str, begin, end, line = g.next()
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
274 if str != "import": break
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
275 from_stmt=parse_to_end(g)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
276 if len(from_stmt) > 0:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
277 #dbg(5,"found [from %s import %s]" % (mod, from_stmt))
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
278 stmts.append("from %s import %s" % (mod, from_stmt))
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
279 #def statement
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
280 elif str == 'def':
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
281 funcstr = ''
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
282 for type, str, begin, end, line in g:
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
283 if line.replace('\n','') == vim.eval('getline(\'.\')'): break
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
284 elif str == ':':
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
285 stmts += parse_block(g)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
286 break
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
287 funcstr += str
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
288 if len(funcstr) > 0:
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
289 #dbg(5,"found [def %s]" % funcstr)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
290 stmts.append("def %s:\n pass" % funcstr)
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
291 #class declaration
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
292 elif str == 'class':
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
293 type, str, begin, end, line = g.next()
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
294 classname = str
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
295 #dbg(5,"found [class %s]" % classname)
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
296
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
297 level = 0
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
298 members = []
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
299 for type, str, begin, end, line in g:
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
300 if line.replace('\n','') == vim.eval('getline(\'.\')'): break
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
301 elif type == tokenize.INDENT:
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
302 level += 1
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
303 elif type == tokenize.DEDENT:
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
304 level -= 1
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
305 if level == 0: break;
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
306 elif str == 'def':
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
307 memberstr = ''
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
308 for type, str, begin, end, line in g:
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
309 if line.replace('\n','') == vim.eval('getline(\'.\')'): break
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
310 elif str == ':':
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
311 stmts += parse_block(g)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
312 break
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
313 memberstr += str
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
314 #dbg(5," member [%s]" % memberstr)
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
315 members.append(memberstr)
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
316 classstr = 'class %s:' % classname
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
317 for m in members:
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
318 classstr += ("\n def %s:\n pass" % m)
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
319 stmts.append("%s\n" % classstr)
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
320 elif keyword.iskeyword(str) or str in globals():
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
321 #dbg(5,"keyword = %s" % str)
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
322 lineNo = begin[0]
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
323 else:
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
324 assign = parse_statement(g,str)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
325 if len(assign) > 0: stmts.append(assign)
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
326
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
327 for s in stmts:
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
328 try:
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
329 #dbg(15,"evaluating: %s\n" % s)
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
330 exec(s) in globals()
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
331 except:
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
332 #dbg(1,"exception: %s" % sys.exc_info()[1])
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
333 pass
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
334 except:
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
335 #dbg(1,"exception: %s" % sys.exc_info()[1])
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
336 pass
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
337
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
338 def clean_up():
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
339 for o in globals().keys():
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
340 if o not in LOCALDEFS:
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
341 try:
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
342 exec('del %s' % o) in globals()
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
343 except: pass
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
344
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
345 sys.path.extend(['.','..'])
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
346 PYTHONEOF
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
347 endfunction
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
348
644
e4fa26ce8769 updated for version 7.0187
vimboss
parents: 626
diff changeset
349 let g:pycomplete_debug_level = 0
626
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
350 call s:DefPython()
732c7ae5743e updated for version 7.0180
vimboss
parents:
diff changeset
351 " vim: set et ts=4: