def InstallEngine(self, engine_pack_name): engine_info = {} try: if type(engine_pack_name) is dict: sys.path.append(engine_pack_name['dir']) module_obj = importlib.import_module(engine_pack_name['name']) engine_name = engine_pack_name['name'] else: engine_name = engine_pack_name module_obj = importlib.import_module(engine_pack_name) obj = module_obj.Operate(engine_name) obj.engine_name = engine_name engine_info['engine_obj'] = obj except Exception as e: logger.exception(e) return False engine_info['handler_queue'] = queue.Queue() engine_info['res_queue'] = queue.Queue() engine_info['name'] = engine_pack_name threading.Thread(target=self.EngineCallbackThread, args=(engine_info, ), daemon=True).start() threading.Thread(target=self.EngineEventHandler, args=(engine_info, ), daemon=True).start() logger.debug("installed engine %s" % (engine_info)) self.engine_dict[engine_pack_name] = engine_info return self.engine_dict[engine_pack_name]
def Send(types, function_name='', variable_name='', params=[]): global request_list global request_id global g_lock g_lock.acquire() request_id += 1 cmd = { 'type': types, 'params': params, 'function_name': function_name, 'variable_name': variable_name, 'id': request_id } res_queue = queue.Queue() request_list[request_id] = res_queue # print(json.dumps(cmd), flush=True) # with "\n" sys.stdout.write(json.dumps(cmd) + "\n") sys.stdout.flush() g_lock.release() try: context = res_queue.get() # block here if context['status'] != 0: logger.error(context) except: logger.exception('timeout') return context['res']
def _Call(self): while True: try: event = g_call_queue.get() self._handler(event) except Exception as e: logger.exception(e)
def FallBack(): global g_event_handle_thread global request_list content2 = '' logger.debug('Started RPC') while True: try: content = ReadStdIn() content2 = content2 + content content_split = content2.split('\n') lens = len(content_split) if lens == 1: continue else: content2 = content_split[lens - 1] i = 0 while i < (lens - 1): if content_split[i] == '': continue context = json.loads(content_split[i]) if context['type'] == 'event': g_event_handle_thread.put(context) elif context['type'] == 'fallback': re_id = context['request_id'] if re_id not in request_list: continue request_list[re_id].put(context) else: pass i += 1 except Exception as e: logger.exception(e) raise
def _callback_thread(self): try: res = self.response_queue.get() res['callback_additional_data'] = self.callback_additional_data self.Callback(res) except Exception as e: logger.exception(e) self.Callback(e)
def try_listdir(self, try_path): try: res = [] for item in os.listdir(try_path): if self._apply_ignore(item): continue res.append(item) return res except Exception as e: logger.exception(e) return []
def _handle_log_msg(self): while 1: try: response = self._lsp.GetRequestOrNotification( 'window/logMessage') msg = response['params']['message'] if msg.find('compile_commands') != -1: # clangd 12+ self._show_msg(msg.split('\n')) logger.debug(response) except Exception as e: logger.exception(e)
def Preview(self, event): try: event = self._handle_preview(event) res = self._handler(event) if type(res) is not str: return "not str" except Exception as e: logger.exception(e) res = str(e) res = self.StringToBase64(res) return res
def EngineCallbackThread(self, *args): engine_info = args[0] res_queue = engine_info['res_queue'] engine_name = engine_info['name'] while True: try: callback_context = res_queue.get() event_name = callback_context['event_name'] self.CallFunction(self.events_callback, event_name, engine_name, callback_context) except Exception as e: logger.exception(e)
def _get_diagnosis(self): while True: try: temp = self._lsp.GetRequestOrNotification( 'textDocument/publishDiagnostics') self._diagnosis_cache = temp['params']['diagnostics'] lists = self._diagnosis_analysis(temp['params']) rpc.DoCall('ECY#diagnostics#PlaceSign', [{ 'engine_name': self.engine_name, 'res_list': lists }]) except Exception as e: logger.exception(e)
def UriToPath(self, uri): try: if utils.GetCurrentOS() == 'Windows': # url2pathname does not understand %3A (VS Code's encoding forced on all servers :/) file_path = url2pathname(urlparse(uri).path).strip('\\') if file_path[0].islower(): file_path = file_path[0].upper() + file_path[1:] return file_path else: return url2pathname(urlparse(uri).path) except Exception as e: logger.exception(e) return "file:///home/ECY/1"
def _handle_show_msg(self): if not utils.GetEngineConfig('GLOBAL_SETTING', 'lsp.showMessage'): return while 1: try: response = self._lsp.GetRequestOrNotification( 'window/showMessage') res = response['params'] msg = res['message'] self._show_msg( "{ %s } %s" % (messsage_type.MESSAGE_TYPE[res['type']], msg.split('\n'))) except Exception as e: logger.exception(e)
def _GetJediScript(self, context): try: params = context['params'] path = params['buffer_path'] line_nr = params['buffer_position']['line'] + 1 line_text = params['buffer_content'] line_text = "\n".join(params['buffer_content']) current_colum = params['buffer_position']['colum'] temp = jedi.Script(line_text, line_nr, current_colum, path) # environment=self._get_environment(context['ForceVersion'])) return temp except Exception as e: logger.exception(e) return []
def _get_registerCapability(self): while True: try: jobs = self._lsp.GetRequestOrNotification( 'client/registerCapability') params = jobs['params'] for item in params['registrations']: logger.debug(item) method = item['method'] if method == 'workspace/didChangeWorkspaceFolders': if 'workspace' not in self.capabilities: self.capabilities['workspace'] = {} self.capabilities['workspace'][ 'workspaceFolders'] = True self._lsp._build_response(None, item['id']) except Exception as e: logger.exception(e)
def EngineEventHandler(self, *args): engine_info = args[0] handler_queue = engine_info['handler_queue'] engine_name = engine_info['name'] while True: context = handler_queue.get() event_name = context['event_name'] try: before_context = self.CallFunction(engine_info['engine_obj'], 'OnRequest', engine_name, context) if before_context is None: # has no before event before_context = context if event_name == 'OnCheckEngine': self.CheckEngine(context) continue elif event_name == 'ReStart': try: module_obj = importlib.import_module(engine_name) temp = module_obj.Operate() del engine_info['engine_obj'] engine_info['engine_obj'] = temp except: Echo('Failed to reload %s' % (engine_name)) continue Echo('Reload %s OK.' % (engine_name)) continue pre_context = self.CallFunction(self.events_pre, event_name, engine_name, before_context) if pre_context is False: # filter pre event continue if pre_context is None: # has no pre event pre_context = context callback_context = self.CallFunction(engine_info['engine_obj'], event_name, engine_name, pre_context) if callback_context is None: # filter this event continue engine_info['res_queue'].put(callback_context) except Exception as e: logger.exception(e) Echo( 'Something wrong with [%s] causing ECY can NOT go on, check log info for more.' % (engine_name))
def _did_open_or_change(self, context): # {{{ uri = context['params']['buffer_path'] text = context['params']['buffer_content'] text = "\n".join(text) try: uri = self._lsp.PathToUri(uri) except Exception as e: # wrong uri while using fugitive. logger.exception(e) return version = context['params']['buffer_id'] # LSP requires the edit-version if uri not in self._did_open_list: self._lsp.didopen(uri, self.languageId, text, version=version) self._did_open_list[uri] = {'buffer_id': version} else: self._lsp.didchange(uri, text, version=version) self._did_open_list[uri]['buffer_id'] = version
def _handle_edit(self): while 1: try: response = self._lsp.GetRequestOrNotification( 'workspace/applyEdit') try: res = workspace_edit.WorkspaceEdit( response['params']['edit']) self._do_action(res) applied = True except Exception as e: logger.exception(e) applied = False logger.debug(response) self._lsp.applyEdit_response(response['id'], applied) except Exception as e: logger.exception(e)
def New(self, new_obj, context): global event_id event_id += 1 new_obj = new_obj(event_id) try: context['source'] = new_obj.GetSource(context) context['id'] = event_id context['engine_name'] = new_obj.engine_name key_bind = new_obj.RegKeyBind() if type(context['source']) != list or type(key_bind) is not dict: return g_context[event_id] = { 'obj': new_obj, 'context': context, 'key_bind': key_bind, 'id': event_id } return g_context[event_id] except Exception as e: logger.exception(e)
def StartJob(self, cmd, argvs=None): try: if type(cmd) is list: cmd = " ".join(cmd) logger.debug(cmd) if argvs is not None: if type(argvs) is list: argvs = " ".join(argvs) cmd += ' ' + argvs logger.debug("real cmd: " + str(cmd)) process_obj = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) self.server_count += 1 process_hanlder = ThreadOfJob(self.server_count, process_obj, self._queue) # stdin threading.Thread(target=process_hanlder.Start, daemon=True).start() # stderr threading.Thread(target=process_hanlder.LogStderr, daemon=True).start() self.server_info[self.server_count] = {} self.server_info[self.server_count]['cmd'] = cmd self.server_info[self.server_count]['proc_object'] = process_obj self.server_info[ self.server_count]['thread_object'] = process_hanlder except Exception as e: logger.exception(e) return None return self.server_count
def _handle_diagnosis(self): reporter = PyflakesDiagnosticReport('') self.document_id = -1 while 1: try: context = self._diagnosis_queue.get() params = context['params'] if params['buffer_id'] <= self.document_id: continue self.document_id = params['buffer_id'] text_string = '\n'.join(params['buffer_content']) reporter.SetContent(text_string) pyflakes_api.check(text_string, params['buffer_path'], reporter=reporter) res = reporter.GetDiagnosis() rpc.DoCall('ECY#diagnostics#PlaceSign', [{ 'engine_name': self.engine_name, 'res_list': res }]) time.sleep(1) except Exception as e: logger.exception(e) break