Repository navigation
Expand file tree
/
Copy pathpython_completion_server.py
More file actions
217 lines (205 loc) · 6.91 KB
/
Copy pathpython_completion_server.py
File metadata and controls
217 lines (205 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import code
import sys
import os.path
import time
import types
import struct
import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer
#from pysmell import idehelper
#from pysmell import tags
from re import split
from rope.contrib import codeassist
import rope.base.project
'''
| | instance | class | function | module | None
| local | + | + | + | + |
| global | + | + | + | + |
| builtin | + | + | + | |
| attribute | + | + | + | + |
| imported | + | + | + | + |
| keyword | | | | | +
| parameter_keyword | | | | | +
'''
'''
type_map = {
str(types.ModuleType) : 1,
str(types.ClassType) : 2,
str(types.ObjectType) : 3,
str(types.TypeType) : 4,
str(types.InstanceType) : 5,
str(types.FunctionType) : 6,
str(types.MethodType) : 7,
str(types.UnboundMethodType) : 8,
str(types.BuiltinFunctionType) : 6,
str(types.BuiltinMethodType) : 7,
str(types.LambdaType) : 9,
str(types.GeneratorType) : 10 }
'''
def type_suffix(o):
t = o.type
s = o.scope
if t=='instance':
return '?'+str(5)
elif t=='class':
return '?'+str(2)
elif t=='function':
return '?'+str(6)
elif t=='module':
return '?'+str(1)
elif s=='keyword':
return ''
return ''
def _uniquify(l):
found = set()
for item in l:
if item not in found:
yield item
found.add(item)
isz=struct.calcsize('I')
class XmlRpcPipeServer:
def __init__(self):
self.fn_dict={}
self.inpipe=sys.stdin
self.outpipe=sys.stdout
sys.stdout=open(os.devnull,'wb')
def register_function(self,fn,name):
self.fn_dict[name]=fn
def register_introspection_functions(self):
pass
def handle_request(self):
##TODO: Need more error handling!
size_buf = self.inpipe.read(isz)
size = struct.unpack('I',size_buf)[0]
call_xml = self.inpipe.read(size)
self.outpipe.write(struct.pack('c','M'))
name=''
try:
args,name = xmlrpclib.loads(call_xml)
result = self.__call__(name, *args)
if not isinstance(result,tuple):
result=(result,)
res_xml = bytes(xmlrpclib.dumps(result, methodresponse=True))
except:
import traceback
res_xml='Error running call'+name+call_xml+'\n'
res_xml+='\n'.join(traceback.format_exception(*sys.exc_info()))
size = len(res_xml)
self.outpipe.write(struct.pack('I',size))
self.outpipe.write(res_xml)
def __call__(self,name,*args):
return self.fn_dict[name](*args)
class PyCompletionServer:
def __init__(self,port):
# Create XMLRPC server
self.timeout=0.2
self._quit=False
self.port=port
self.project=None
self.active_path=None
def run(self):
if self.port==-1:
self.server = XmlRpcPipeServer()
else:
self.server = SimpleXMLRPCServer(("localhost", self.port))
self.server.logRequests=0
self.server.register_introspection_functions()
#self.server.socket.settimeout(self.timeout) ##timeouts cause probs
self.server.register_function(self.end,'end')
self.server.register_function(self.complete_phrase,'complete_phrase')
self.server.register_function(self.complete_tip,'complete_tip')
self.server.register_function(self.get_definition_location,'get_definition_location')
while not self._quit:
self.server.handle_request()
def notify_active_path(self,path):
if self.active_path==os.path.split(path)[0]:
return True
if not os.path.exists(path):
return False
fdir,fname = os.path.split(path)
self.active_path=fdir
try:
self.project = rope.base.project.Project(fdir)
except:
return False
return True
def complete_phrase(self,path,source,position):
# print 'complete context',path,position
# if '\r' in source:
# print 'replacing CRs'
# source=source.replace('\r\n',' \n').replace('\r','\n')
if path!=None:
if not self.notify_active_path(path):
return []
if self.project==None:
return []
results = codeassist.code_assist(self.project, source, position)
completions=sorted([s.name+type_suffix(s) for s in results])
return completions
def complete_tip(self,path,source,position):
# print 'complete tip',symbol
# source=source.replace('\r\n',' \n').replace('\r','\n')
if path!=None:
if not self.notify_active_path(path):
return []
if self.project==None:
return []
calltip = codeassist.get_calltip(self.project, source, position)
if calltip==None:
calltip=''
doc = codeassist.get_doc(self.project, source, position)
if doc is not None:
lines=doc.split('\n')
del lines[0] ##rope inserts its own header in the doc string
doc=doc.strip(' \t\n')
fnfull = calltip[:calltip.find('(')]
fn = calltip[fnfull.find('.'):]
if not (fnfull and lines[0].startswith(fnfull)):
if not (fn and lines[0].startswith(fn)):
lines.insert(0,calltip)
if lines[0].strip()[-1]!=':':
lines[0]=lines[0]+':'
shortened=False
if len(lines)>6:
shortened=True
doc='\n'.join(lines[:8])
else:
doc='\n'.join(lines)
if len(doc)>250:
shortened=True
doc=doc[:250]
if shortened:
doc+='...'
calltip=doc
if calltip==None:
calltip='<unknown function>'
return calltip
def get_definition_location(self,path,source,position):
if path!=None:
if not self.notify_active_path(path):
return ['',-1]
resource,lineno = codeassist.get_definition_location(self.project,source,position)
if lineno == None:
return ['',-1]
if resource == None:
return [path,lineno]
return [resource.path,lineno]
def end(self):
self._quit=True
return True
def cmd_err():
print 'Correct usage: python_completion_server.py <port>'
print '<port> must be a positive integer or -1 to use stdin/stdout'
exit()
if __name__=='__main__':
if len(sys.argv)!=2:
port=8001
else:
try:
port = int(sys.argv[1])
except:
cmd_err()
if port<-1:
cmd_err()
server=PyCompletionServer(port)
server.run()