| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 1 | #@PydevCodeAnalysisIgnore |
| 2 | ''' |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 3 | @author Fabio Zadrozny |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 4 | ''' |
| 5 | IS_PYTHON3K = 0 |
| 6 | try: |
| 7 | import __builtin__ |
| 8 | except ImportError: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 9 | import builtins as __builtin__ # Python 3.0 |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 10 | IS_PYTHON3K = 1 |
| 11 | |
| 12 | try: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 13 | True |
| 14 | False |
| 15 | except NameError: |
| 16 | # If it's not defined, let's define it now. |
| 17 | setattr(__builtin__, 'True', 1) # Python 3.0 does not accept __builtin__.True = 1 in its syntax |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 18 | setattr(__builtin__, 'False', 0) |
| 19 | |
| 20 | import pydevd_constants |
| 21 | |
| 22 | try: |
| 23 | from java.lang import Thread |
| 24 | IS_JYTHON = True |
| 25 | SERVER_NAME = 'jycompletionserver' |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 26 | import _pydev_jy_imports_tipper # as _pydev_imports_tipper #changed to be backward compatible with 1.5 |
| 27 | _pydev_imports_tipper = _pydev_jy_imports_tipper |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 28 | |
| 29 | except ImportError: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 30 | # it is python |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 31 | IS_JYTHON = False |
| 32 | SERVER_NAME = 'pycompletionserver' |
| 33 | if pydevd_constants.USE_LIB_COPY: |
| 34 | from _pydev_threading import Thread |
| 35 | else: |
| 36 | from threading import Thread |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 37 | import _pydev_imports_tipper |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 38 | |
| 39 | |
| 40 | if pydevd_constants.USE_LIB_COPY: |
| 41 | import _pydev_socket as socket |
| 42 | else: |
| 43 | import socket |
| 44 | |
| 45 | import sys |
| 46 | if sys.platform == "darwin": |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 47 | # See: https://sourceforge.net/projects/pydev/forums/forum/293649/topic/3454227 |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 48 | try: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 49 | import _CF # Don't fail if it doesn't work -- do it because it must be loaded on the main thread! @UnresolvedImport @UnusedImport |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 50 | except: |
| 51 | pass |
| 52 | |
| 53 | |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 54 | # initial sys.path |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 55 | _sys_path = [] |
| 56 | for p in sys.path: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 57 | # changed to be compatible with 1.5 |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 58 | _sys_path.append(p) |
| 59 | |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 60 | # initial sys.modules |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 61 | _sys_modules = {} |
| 62 | for name, mod in sys.modules.items(): |
| 63 | _sys_modules[name] = mod |
| 64 | |
| 65 | |
| 66 | import traceback |
| 67 | |
| 68 | if pydevd_constants.USE_LIB_COPY: |
| 69 | import _pydev_time as time |
| 70 | else: |
| 71 | import time |
| 72 | |
| 73 | try: |
| 74 | import StringIO |
| 75 | except: |
| 76 | import io as StringIO #Python 3.0 |
| 77 | |
| 78 | try: |
| 79 | from urllib import quote_plus, unquote_plus |
| 80 | except ImportError: |
| 81 | from urllib.parse import quote_plus, unquote_plus #Python 3.0 |
| 82 | |
| 83 | INFO1 = 1 |
| 84 | INFO2 = 2 |
| 85 | WARN = 4 |
| 86 | ERROR = 8 |
| 87 | |
| 88 | DEBUG = INFO1 | ERROR |
| 89 | |
| 90 | def dbg(s, prior): |
| 91 | if prior & DEBUG != 0: |
| 92 | sys.stdout.write('%s\n' % (s,)) |
| 93 | # f = open('c:/temp/test.txt', 'a') |
| 94 | # print_ >> f, s |
| 95 | # f.close() |
| 96 | |
| 97 | import pydev_localhost |
| 98 | HOST = pydev_localhost.get_localhost() # Symbolic name meaning the local host |
| 99 | |
| 100 | MSG_KILL_SERVER = '@@KILL_SERVER_END@@' |
| 101 | MSG_COMPLETIONS = '@@COMPLETIONS' |
| 102 | MSG_END = 'END@@' |
| 103 | MSG_INVALID_REQUEST = '@@INVALID_REQUEST' |
| 104 | MSG_JYTHON_INVALID_REQUEST = '@@JYTHON_INVALID_REQUEST' |
| 105 | MSG_CHANGE_DIR = '@@CHANGE_DIR:' |
| 106 | MSG_OK = '@@MSG_OK_END@@' |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 107 | MSG_IMPORTS = '@@IMPORTS:' |
| 108 | MSG_PYTHONPATH = '@@PYTHONPATH_END@@' |
| 109 | MSG_CHANGE_PYTHONPATH = '@@CHANGE_PYTHONPATH:' |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 110 | MSG_JEDI = '@@MSG_JEDI:' |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 111 | MSG_SEARCH = '@@SEARCH' |
| 112 | |
| 113 | BUFFER_SIZE = 1024 |
| 114 | |
| 115 | |
| 116 | |
| 117 | currDirModule = None |
| 118 | |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 119 | def CompleteFromDir(directory): |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 120 | ''' |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 121 | This is necessary so that we get the imports from the same directory where the file |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 122 | we are completing is located. |
| 123 | ''' |
| 124 | global currDirModule |
| 125 | if currDirModule is not None: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 126 | if len(sys.path) > 0 and sys.path[0] == currDirModule: |
| 127 | del sys.path[0] |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 128 | |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 129 | currDirModule = directory |
| 130 | sys.path.insert(0, directory) |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 131 | |
| 132 | |
| 133 | def ChangePythonPath(pythonpath): |
| 134 | '''Changes the pythonpath (clears all the previous pythonpath) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 135 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 136 | @param pythonpath: string with paths separated by | |
| 137 | ''' |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 138 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 139 | split = pythonpath.split('|') |
| 140 | sys.path = [] |
| 141 | for path in split: |
| 142 | path = path.strip() |
| 143 | if len(path) > 0: |
| 144 | sys.path.append(path) |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 145 | |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 146 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 147 | class Processor: |
| 148 | |
| 149 | def __init__(self): |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 150 | # nothing to do |
| 151 | return |
| 152 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 153 | def removeInvalidChars(self, msg): |
| 154 | try: |
| 155 | msg = str(msg) |
| 156 | except UnicodeDecodeError: |
| 157 | pass |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 158 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 159 | if msg: |
| 160 | try: |
| 161 | return quote_plus(msg) |
| 162 | except: |
| 163 | sys.stdout.write('error making quote plus in %s\n' % (msg,)) |
| 164 | raise |
| 165 | return ' ' |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 166 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 167 | def formatCompletionMessage(self, defFile, completionsList): |
| 168 | ''' |
| 169 | Format the completions suggestions in the following format: |
| 170 | @@COMPLETIONS(modFile(token,description),(token,description),(token,description))END@@ |
| 171 | ''' |
| 172 | compMsg = [] |
| 173 | compMsg.append('%s' % defFile) |
| 174 | for tup in completionsList: |
| 175 | compMsg.append(',') |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 176 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 177 | compMsg.append('(') |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 178 | compMsg.append(str(self.removeInvalidChars(tup[0]))) # token |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 179 | compMsg.append(',') |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 180 | compMsg.append(self.removeInvalidChars(tup[1])) # description |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 181 | |
| 182 | if(len(tup) > 2): |
| 183 | compMsg.append(',') |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 184 | compMsg.append(self.removeInvalidChars(tup[2])) # args - only if function. |
| 185 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 186 | if(len(tup) > 3): |
| 187 | compMsg.append(',') |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 188 | compMsg.append(self.removeInvalidChars(tup[3])) # TYPE |
| 189 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 190 | compMsg.append(')') |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 191 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 192 | return '%s(%s)%s' % (MSG_COMPLETIONS, ''.join(compMsg), MSG_END) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 193 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 194 | |
| 195 | class T(Thread): |
| 196 | |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 197 | def __init__(self, port): |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 198 | Thread.__init__(self) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 199 | self.ended = False |
| 200 | self.port = port |
| 201 | self.socket = None # socket to send messages. |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 202 | self.processor = Processor() |
| 203 | |
| 204 | |
| 205 | def connectToServer(self): |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 206 | import socket |
| 207 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 208 | self.socket = s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 209 | try: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 210 | s.connect((HOST, self.port)) |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 211 | except: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 212 | sys.stderr.write('Error on connectToServer with parameters: host: %s port: %s\n' % (HOST, self.port)) |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 213 | raise |
| 214 | |
| 215 | def getCompletionsMessage(self, defFile, completionsList): |
| 216 | ''' |
| 217 | get message with completions. |
| 218 | ''' |
| 219 | return self.processor.formatCompletionMessage(defFile, completionsList) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 220 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 221 | def getTokenAndData(self, data): |
| 222 | ''' |
| 223 | When we receive this, we have 'token):data' |
| 224 | ''' |
| 225 | token = '' |
| 226 | for c in data: |
| 227 | if c != ')': |
| 228 | token = token + c |
| 229 | else: |
| 230 | break; |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 231 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 232 | return token, data.lstrip(token + '):') |
| 233 | |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 234 | def emulated_sendall(self, msg): |
| 235 | MSGLEN = 1024 * 20 |
| 236 | |
| 237 | totalsent = 0 |
| 238 | while totalsent < MSGLEN: |
| 239 | sent = self.socket.send(msg[totalsent:]) |
| 240 | if sent == 0: |
| 241 | return |
| 242 | totalsent = totalsent + sent |
| 243 | |
| 244 | |
| 245 | def send(self, msg): |
| 246 | if not hasattr(self.socket, 'sendall'): |
| 247 | #Older versions (jython 2.1) |
| 248 | self.emulated_sendall(msg) |
| 249 | else: |
| 250 | if IS_PYTHON3K: |
| 251 | self.socket.sendall(bytearray(msg, 'utf-8')) |
| 252 | else: |
| 253 | self.socket.sendall(msg) |
| 254 | |
| 255 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 256 | def run(self): |
| 257 | # Echo server program |
| 258 | try: |
| 259 | import _pydev_log |
| 260 | log = _pydev_log.Log() |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 261 | |
| 262 | dbg(SERVER_NAME + ' connecting to java server on %s (%s)' % (HOST, self.port) , INFO1) |
| 263 | # after being connected, create a socket as a client. |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 264 | self.connectToServer() |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 265 | |
| 266 | dbg(SERVER_NAME + ' Connected to java server', INFO1) |
| 267 | |
| 268 | |
| 269 | while not self.ended: |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 270 | data = '' |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 271 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 272 | while data.find(MSG_END) == -1: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 273 | received = self.socket.recv(BUFFER_SIZE) |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 274 | if len(received) == 0: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 275 | sys.exit(0) # ok, connection ended |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 276 | if IS_PYTHON3K: |
| 277 | data = data + received.decode('utf-8') |
| 278 | else: |
| 279 | data = data + received |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 280 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 281 | try: |
| 282 | try: |
| 283 | if data.find(MSG_KILL_SERVER) != -1: |
| 284 | dbg(SERVER_NAME + ' kill message received', INFO1) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 285 | # break if we received kill message. |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 286 | self.ended = True |
| 287 | sys.exit(0) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 288 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 289 | dbg(SERVER_NAME + ' starting keep alive thread', INFO2) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 290 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 291 | if data.find(MSG_PYTHONPATH) != -1: |
| 292 | comps = [] |
| 293 | for p in _sys_path: |
| 294 | comps.append((p, ' ')) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 295 | self.send(self.getCompletionsMessage(None, comps)) |
| 296 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 297 | else: |
| 298 | data = data[:data.rfind(MSG_END)] |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 299 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 300 | if data.startswith(MSG_IMPORTS): |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 301 | data = data[len(MSG_IMPORTS):] |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 302 | data = unquote_plus(data) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 303 | defFile, comps = _pydev_imports_tipper.GenerateTip(data, log) |
| 304 | self.send(self.getCompletionsMessage(defFile, comps)) |
| 305 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 306 | elif data.startswith(MSG_CHANGE_PYTHONPATH): |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 307 | data = data[len(MSG_CHANGE_PYTHONPATH):] |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 308 | data = unquote_plus(data) |
| 309 | ChangePythonPath(data) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 310 | self.send(MSG_OK) |
| 311 | |
| 312 | elif data.startswith(MSG_JEDI): |
| 313 | data = data[len(MSG_JEDI):] |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 314 | data = unquote_plus(data) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 315 | line, column, encoding, path, source = data.split('|', 4) |
| 316 | try: |
| 317 | import jedi # @UnresolvedImport |
| 318 | except: |
| 319 | self.send(self.getCompletionsMessage(None, [('Error on import jedi', 'Error importing jedi', '')])) |
| 320 | else: |
| 321 | script = jedi.Script( |
| 322 | # Line +1 because it expects lines 1-based (and col 0-based) |
| 323 | source=source, |
| 324 | line=int(line) + 1, |
| 325 | column=int(column), |
| 326 | source_encoding=encoding, |
| 327 | path=path, |
| 328 | ) |
| 329 | lst = [] |
| 330 | for completion in script.completions(): |
| 331 | t = completion.type |
| 332 | if t == 'class': |
| 333 | t = '1' |
| 334 | |
| 335 | elif t == 'function': |
| 336 | t = '2' |
| 337 | |
| 338 | elif t == 'import': |
| 339 | t = '0' |
| 340 | |
| 341 | elif t == 'keyword': |
| 342 | continue # Keywords are already handled in PyDev |
| 343 | |
| 344 | elif t == 'statement': |
| 345 | t = '3' |
| 346 | |
| 347 | else: |
| 348 | t = '-1' |
| 349 | |
| 350 | # gen list(tuple(name, doc, args, type)) |
| 351 | lst.append((completion.name, '', '', t)) |
| 352 | self.send(self.getCompletionsMessage('empty', lst)) |
| 353 | |
| 354 | elif data.startswith(MSG_SEARCH): |
| 355 | data = data[len(MSG_SEARCH):] |
| 356 | data = unquote_plus(data) |
| 357 | (f, line, col), foundAs = _pydev_imports_tipper.Search(data) |
| 358 | self.send(self.getCompletionsMessage(f, [(line, col, foundAs)])) |
| 359 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 360 | elif data.startswith(MSG_CHANGE_DIR): |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 361 | data = data[len(MSG_CHANGE_DIR):] |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 362 | data = unquote_plus(data) |
| 363 | CompleteFromDir(data) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 364 | self.send(MSG_OK) |
| 365 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 366 | else: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 367 | self.send(MSG_INVALID_REQUEST) |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 368 | except SystemExit: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 369 | self.send(self.getCompletionsMessage(None, [('Exit:', 'SystemExit', '')])) |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 370 | raise |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 371 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 372 | except: |
| 373 | dbg(SERVER_NAME + ' exception occurred', ERROR) |
| 374 | s = StringIO.StringIO() |
| 375 | traceback.print_exc(file=s) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 376 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 377 | err = s.getvalue() |
| 378 | dbg(SERVER_NAME + ' received error: ' + str(err), ERROR) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 379 | self.send(self.getCompletionsMessage(None, [('ERROR:', '%s\nLog:%s' % (err, log.GetContents()), '')])) |
| 380 | |
| 381 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 382 | finally: |
| 383 | log.Clear() |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 384 | |
| 385 | self.socket.close() |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 386 | self.ended = True |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 387 | sys.exit(0) # connection broken |
| 388 | |
| 389 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 390 | except SystemExit: |
| 391 | raise |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 392 | # No need to log SystemExit error |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 393 | except: |
| 394 | s = StringIO.StringIO() |
| 395 | exc_info = sys.exc_info() |
| 396 | |
| 397 | traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], limit=None, file=s) |
| 398 | err = s.getvalue() |
| 399 | dbg(SERVER_NAME + ' received error: ' + str(err), ERROR) |
| 400 | raise |
| 401 | |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 402 | |
| 403 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 404 | if __name__ == '__main__': |
| 405 | |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame^] | 406 | port = int(sys.argv[1]) # this is from where we want to receive messages. |
| 407 | |
| 408 | t = T(port) |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 409 | dbg(SERVER_NAME + ' will start', INFO1) |
| 410 | t.start() |
| 411 | time.sleep(5) |
| 412 | t.join() |