def get_completions(self, info): '''Get Python completions''' # https://github.com/davidhalter/jedi/blob/master/jedi/utils.py if jedi is None: return [] text = info['code'] position = (info['line_num'], info['column']) interpreter = Interpreter(text, [self.env]) if jedi.__version__ >= LooseVersion('0.12.0'): lines = split_lines(text) name = get_on_completion_name(interpreter._module_node, lines, position) before = text[:len(text) - len(name)] elif jedi.__version__ >= LooseVersion('0.10.0'): lines = split_lines(text) name = get_on_completion_name(interpreter._get_module_node(), lines, position) before = text[:len(text) - len(name)] else: path = UserContext(text, position).get_path_until_cursor() path, dot, like = completion_parts(path) before = text[:len(text) - len(like)] completions = interpreter.completions() completions = [before + c.name_with_symbols for c in completions] self.kernel.log.error(completions) return [c[info['start']:] for c in completions]
def complete(self, text, state): """ This complete stuff is pretty weird, a generator would make a lot more sense, but probably due to backwards compatibility this is still the way how it works. The only important part is stuff in the ``state == 0`` flow, everything else has been copied from the ``rlcompleter`` std. library module. """ if state == 0: sys.path.insert(0, os.getcwd()) # Calling python doesn't have a path, so add to sys.path. try: logging.debug("Start REPL completion: " + repr(text)) interpreter = Interpreter(text, [namespace_module.__dict__]) completions = interpreter.complete(fuzzy=fuzzy) logging.debug("REPL completions: %s", completions) self.matches = [ text[:len(text) - c._like_name_length] + c.name_with_symbols for c in completions ] except: logging.error("REPL Completion error:\n" + traceback.format_exc()) raise finally: sys.path.pop(0) try: return self.matches[state] except IndexError: return None
def complete(self, text, state): """ This complete stuff is pretty weird, a generator would make a lot more sense, but probably due to backwards compatibility this is still the way how it works. The only important part is stuff in the ``state == 0`` flow, everything else has been copied from the ``rlcompleter`` std. library module. """ if state == 0: sys.path.insert(0, os.getcwd()) # Calling python doesn't have a path, so add to sys.path. try: logging.debug("Start REPL completion: " + repr(text)) interpreter = Interpreter(text, [namespace_module.__dict__]) lines = common.splitlines(text) position = (len(lines), len(lines[-1])) name = get_on_completion_name(interpreter._get_module(), lines, position) before = text[:len(text) - len(name)] completions = interpreter.completions() except: logging.error("REPL Completion error:\n" + traceback.format_exc()) raise finally: sys.path.pop(0) self.matches = [before + c.name_with_symbols for c in completions] try: return self.matches[state] except IndexError: return None
def serialize_suggestion(prompt, from_, to, cursor, frame): answer = {"prompt": prompt, "from": from_, "to": to, "suggestion": {}} try: script = Interpreter(prompt, [frame.f_locals, frame.f_globals]) completions = script.complete(cursor["line"] + 1, cursor["ch"]) except Exception: log.exception("Completion failed") return answer params_first_completions = [ c for c in completions if c.name_with_symbols != c.name ] + [c for c in completions if c.name_with_symbols == c.name] completions = [ { "text": comp.name_with_symbols, "description": comp.description, "docstring": comp.docstring(), "type": comp.type, "base": comp.name_with_symbols[ : len(comp.name_with_symbols) - len(comp.complete) ], "complete": comp.complete, } for comp in params_first_completions ] suggestion = {"from": from_, "to": to, "list": completions} answer["suggestion"] = suggestion return answer
def complete(self, text, state): """ This complete stuff is pretty weird, a generator would make a lot more sense, but probably due to backwards compatibility this is still the way how it works. The only important part is stuff in the ``state == 0`` flow, everything else has been copied from the ``rlcompleter`` std. library module. """ if state == 0: import os, sys sys.path.insert(0, os.getcwd()) # Calling python doesn't have a path, so add to sys.path. try: interpreter = Interpreter(text, [namespace_module.__dict__]) path, dot, like = interpreter._get_completion_parts() before = text[:len(text) - len(like)] completions = interpreter.completions() finally: sys.path.pop(0) self.matches = [before + c.name_with_symbols for c in completions] try: return self.matches[state] except IndexError: return None
def get_completions(self, info): '''Get Python completions''' # https://github.com/davidhalter/jedi/blob/master/jedi/utils.py if jedi is None: return [] text = info['code'] position = (info['line_num'], info['column']) interpreter = Interpreter(text, [self.env]) lines = split_lines(text) name = get_on_completion_name( interpreter._module_node, lines, position ) before = text[:len(text) - len(name)] try: completions = interpreter.complete() except AttributeError: completions = interpreter.completions() completions = [before + c.name_with_symbols for c in completions] self.kernel.log.error(completions) return [c[info['start']:] for c in completions]
def complete(self, text, state): """ This complete stuff is pretty weird, a generator would make a lot more sense, but probably due to backwards compatibility this is still the way how it works. The only important part is stuff in the ``state == 0`` flow, everything else has been copied from the ``rlcompleter`` std. library module. """ if state == 0: sys.path.insert(0, os.getcwd()) # Calling python doesn't have a path, so add to sys.path. try: interpreter = Interpreter(text, [namespace_module.__dict__]) path = UserContext(text, (1, len(text))).get_path_until_cursor() path, dot, like = completion_parts(path) before = text[:len(text) - len(like)] completions = interpreter.completions() finally: sys.path.pop(0) self.matches = [before + c.name_with_symbols for c in completions] try: return self.matches[state] except IndexError: return None
def do_complete(self, data): completion = loads(data) source = completion.pop('source') pos = completion.pop('pos') try: script = Interpreter(source, [ self.current_locals, self.get_globals()], **completion) with timeout_of(.75): completions = script.completions() except Exception: self.db.send('Suggest') self.notify_exc('Completion failed for %s' % data) return try: with timeout_of(.25): funs = script.call_signatures() or [] except Exception: self.db.send('Suggest') self.notify_exc('Completion of function failed for %s' % data) return before = source[:pos] after = source[pos:] like = '' if len(completions): completion = completions[0] base = completion.name[ :len(completion.name) - len(completion.complete)] if len(base): like = before[-len(base):] if len(like): before = before[:-len(like)] try: suggest_obj = { 'data': { 'start': before, 'end': after, 'like': like }, 'params': [{ 'params': [p.description.replace('\n', '') for p in fun.params], 'index': fun.index, 'module': fun.module_name, 'call_name': fun.name} for fun in funs], 'completions': [{ 'base': comp.name[ :len(comp.name) - len(comp.complete)], 'complete': comp.complete, 'description': comp.description } for comp in completions if comp.name.endswith( comp.complete)] } self.db.send('Suggest|%s' % dump(suggest_obj)) except Exception: self.db.send('Suggest') self.notify_exc('Completion generation failed for %s' % data)
def get_jedi_completions(self, info, env=None): '''Get Python completions''' # https://github.com/davidhalter/jedi/blob/master/jedi/utils.py text = info['code'][:info['end']] env = env or {} interpreter = Interpreter(text, [env]) position = (info['line_num'], info['column']) path = UserContext(text, position).get_path_until_cursor() path, dot, like = completion_parts(path) before = text[:len(text) - len(like)] completions = interpreter.completions() completions = [before + c.name_with_symbols for c in completions] return [c[info['start']:] for c in completions]
def __ein_object_info_for(name): from jedi import Interpreter try: inferred = Interpreter(name, [globals(), locals()]).infer() defs = inferred[0] sig = defs.get_signatures() info_dict = { 'type': defs.type, 'module_name': defs.module_name, 'module_path': defs.module_path, 'line': defs.line, 'column': defs.column, 'docstring': defs.docstring(), 'full_name': defs.full_name, 'call_signature': '' } if (type(sig) is list): info_dict['call_signature'] = sig[0].to_string() return info_dict except Exception: return { 'type': '', 'module_name': '', 'line': '', 'column': '', 'docstring': '', 'full_name': '', 'call_signature': '', }
def get_completions(self, info): """Get Python completions.""" # https://github.com/davidhalter/jedi/blob/master/jedi/utils.py if jedi is None: return [] text = info['code'] interpreter = Interpreter(text, [self.env]) position = (info['line_num'], info['column']) path = UserContext(text, position).get_path_until_cursor() path, dot, like = completion_parts(path) before = text[:len(text) - len(like)] completions = interpreter.completions() completions = [before + c.name_with_symbols for c in completions] return [c[info['start']:] for c in completions]
def do_complete(self, data): script = Interpreter(data, [self.current_locals, self.get_globals()]) try: with timeout_of(.75): completions = script.completions() except Exception: self.db.send('Suggest') self.notify_exc('Completion failed for %s' % data) return try: with timeout_of(.25): funs = script.call_signatures() or [] except Exception: self.db.send('Suggest') self.notify_exc('Completion of function failed for %s' % data) return try: suggest_obj = { 'params': [{ 'params': [p.get_code().replace('\n', '') for p in fun.params], 'index': fun.index, 'module': fun.module_name, 'call_name': fun.name } for fun in funs], 'completions': [{ 'base': comp.name[:len(comp.name) - len(comp.complete)], 'complete': comp.complete, 'description': comp.description } for comp in completions if comp.name.endswith(comp.complete)] } self.db.send('Suggest|%s' % dump(suggest_obj)) except Exception: self.db.send('Suggest') self.notify_exc('Completion generation failed for %s' % data)
def __ein_find_edit_target_python(name): from jedi import Interpreter try: defs = Interpreter(name, [globals(), locals()]).infer() if defs: return (defs[0].module_path, defs[0].line) else: return False except: return False
def complete(self, text, state): """ This complete stuff is pretty weird, a generator would make a lot more sense, but probably due to backwards compatibility this is still the way how it works. The only important part is stuff in the ``state == 0`` flow, everything else has been copied from the ``rlcompleter`` std. library module. """ if state == 0: sys.path.insert(0, os.getcwd()) # Calling python doesn't have a path, so add to sys.path. try: logging.debug("Start REPL completion: " + repr(text)) interpreter = Interpreter(text, [namespace_module.__dict__]) lines = split_lines(text) position = (len(lines), len(lines[-1])) name = get_on_completion_name( interpreter._get_module_node(), lines, position) before = text[:len(text) - len(name)] completions = interpreter.completions() except: logging.error("REPL Completion error:\n" + traceback.format_exc()) raise finally: sys.path.pop(0) self.matches = [ before + c.name_with_symbols for c in completions ] try: return self.matches[state] except IndexError: return None
def do_complete(self, data): script = Interpreter(data, [self.current_locals, self.get_globals()]) try: with timeout_of(.75): completions = script.completions() except Exception: self.db.send('Suggest') self.notify_exc('Completion failed for %s' % data) return try: with timeout_of(.25): funs = script.call_signatures() or [] except Exception: self.db.send('Suggest') self.notify_exc('Completion of function failed for %s' % data) return try: suggest_obj = { 'params': [{ 'params': [p.get_code().replace('\n', '') for p in fun.params], 'index': fun.index, 'module': fun.module_name, 'call_name': fun.name} for fun in funs], 'completions': [{ 'base': comp.name[ :len(comp.name) - len(comp.complete)], 'complete': comp.complete, 'description': comp.description } for comp in completions if comp.name.endswith( comp.complete)] } self.db.send('Suggest|%s' % dump(suggest_obj)) except Exception: self.db.send('Suggest') self.notify_exc('Completion generation failed for %s' % data)
def get_completions(self, info): '''Get Python completions''' # https://github.com/davidhalter/jedi/blob/master/jedi/utils.py if jedi is None: return [] text = info['code'] position = (info['line_num'], info['column']) interpreter = Interpreter(text, [self.env]) if jedi.__version__ >= LooseVersion('0.12.0'): lines = split_lines(text) name = get_on_completion_name( interpreter._module_node, lines, position ) before = text[:len(text) - len(name)] elif jedi.__version__ >= LooseVersion('0.10.0'): lines = split_lines(text) name = get_on_completion_name( interpreter._get_module_node(), lines, position ) before = text[:len(text) - len(name)] else: path = UserContext(text, position).get_path_until_cursor() path, dot, like = completion_parts(path) before = text[:len(text) - len(like)] completions = interpreter.completions() completions = [before + c.name_with_symbols for c in completions] self.kernel.log.error(completions) return [c[info['start']:] for c in completions]
def do_complete(self, data): completion = loads(data) source = completion.pop('source') pos = completion.pop('pos') try: script = Interpreter( source, [self.current_locals, self.get_globals()], **completion) with timeout_of(.75): completions = script.completions() except Exception: self.db.send('Suggest') self.notify_exc('Completion failed for %s' % data) return try: with timeout_of(.25): funs = script.call_signatures() or [] except Exception: self.db.send('Suggest') self.notify_exc('Completion of function failed for %s' % data) return before = source[:pos] after = source[pos:] like = '' if len(completions): completion = completions[0] base = completion.name[:len(completion.name) - len(completion.complete)] if len(base): like = before[-len(base):] if len(like): before = before[:-len(like)] try: suggest_obj = { 'data': { 'start': before, 'end': after, 'like': like }, 'params': [{ 'params': [p.description.replace('\n', '') for p in fun.params], 'index': fun.index, 'module': fun.module_name, 'call_name': fun.name } for fun in funs], 'completions': [{ 'base': comp.name[:len(comp.name) - len(comp.complete)], 'complete': comp.complete, 'description': comp.description } for comp in completions if comp.name.endswith(comp.complete)] } self.db.send('Suggest|%s' % dump(suggest_obj)) except Exception: self.db.send('Suggest') self.notify_exc('Completion generation failed for %s' % data)
def iter_scripts(): yield Interpreter(code + '(', namespaces=[locals_]) yield Script(src + code + "2(") yield Interpreter(code + '2(', namespaces=[executed_locals])
def is_callable(text=""): completions = Interpreter(text, [locals()]).complete() match = next((i for i in completions if i.name == text), None) return match.type in ("class", "function") if match else None
def test_interpreter_project_path(): # Run from anywhere it should be the cwd. dir = os.path.join(root_dir, 'test') with set_cwd(dir): project = Interpreter('', [locals()])._inference_state.project assert project._path == dir