def suggestForCode(code, index, path): global _completions_queue try: visibles = ConsoleViewController.objcVisibles except AttributeError: visibles = [] for console in visibles: try: try: if console.editorSplitViewController is None: continue except AttributeError: return try: visibleEditor = console.editorSplitViewController.editor except AttributeError: visibleEditor = console.editorSplitViewController().editor if visibleEditor is None: continue try: if visibleEditor.document.fileURL.path != path: continue except AttributeError: if visibleEditor.document().fileURL.path != path: continue if code.endswith(");\n"): if visibleEditor is None: return visibleEditor.completions = [] visibleEditor.suggestions = [] visibleEditor.docStrings = None return except Exception as e: sys.__stdout__.write("Code completion:\n " + traceback.format_exc() + "\n") return try: script = jedi.Script(code, path=path) definitions = [] for _def in script.get_names(): decl = _def.description line = _def.line if "=" in decl: decl = decl.split("=")[0] if ":" in decl: decl = decl.split(":")[0] definitions.append([decl, line]) visibleEditor.definitions = definitions suggestions = [] completions = [] docs = {} line = 1 column = 0 for i in range(index): char = code[i] if char == "\n": line += 1 column = 0 else: column += 1 signature = "" infer = script.infer(line, column) for _infer in infer: for __infer in _infer.get_signatures(): signature = __infer.to_string() break break if signature == "": context = script.get_context(line, column) for _signature in context.get_signatures(): signature = _signature.to_string() break _completions = script.complete(line, column) for completion in _completions: suggestion = completion.name if completion.complete.startswith("."): suggestion = "." + suggestion if completion.name in __builtins__["deprecated"]: continue complete = completion.complete if complete.endswith("="): complete = complete[:-1] suggestions.append(suggestion) completions.append(complete) docs[suggestion] = "" if complete == "" and signature != "": completions = [] suggestions = [] break visibleEditor.signature = signature visibleEditor.completions = completions visibleEditor.suggestions = suggestions visibleEditor.docStrings = docs except Exception as e: sys.__stdout__.write("Code completion:\n " + traceback.format_exc() + "\n") if visibleEditor is None: return visibleEditor.completions = [] visibleEditor.suggestions = [] visibleEditor.signature = "" visibleEditor.docStrings = None
def jedi_script(self, source, line, column, source_path): if NEED_ENCODE: source = source.encode('utf-8') source_path = source_path and source_path.encode('utf-8') return jedi.Script(source, line, column, source_path or '', **self.script_kwargs)
def test_os_path_join(self): s = "from posixpath import join; join('', '')." assert len(jedi.Script(s).completions()) > 10 # is a str completion
def _eval_literal(value): def_, = jedi.Script(value).goto_definitions() return def_._name._context.obj
def check(self, source, desired): script = jedi.Script(textwrap.dedent(source)) definitions = getattr(script, type(self).operation)() self.assertEqual(definitions[0].full_name, desired)
def test_named_import(self): """ named import - jedi-vim issue #8 """ s = "import time as dt" assert len(jedi.Script(s, 1, 15, '/').goto_definitions()) == 1 assert len(jedi.Script(s, 1, 10, '/').goto_definitions()) == 1
def loop_modules(modules, data): # Loop modules AKA files - this is garbage but works lmao for module in modules: modulesplit = list(splitsource) modulesplit[2] = "%s%s." % (modulesplit[2], module) #print(modulesplit) source = "\n".join(modulesplit) entrypoint = jedi.Script(source, line=len(modulesplit)-1, column=len(modulesplit[2])) # Loop classes in the files for classcompletion in entrypoint.completions(): if classcompletion.type != "class": continue if not classcompletion.full_name.startswith(modulesplit[2]): continue # Same thing again, but for functions within classes # CBA with subclasses etc atm #print(classcompletion.full_name, modulesplit[2]) classplit = list(modulesplit) classplit[2] = "%s." % (classcompletion.full_name) #print(modulesplit) source = "\n".join(classplit) entrypoint = jedi.Script(source, line=len(classplit)-1, column=len(classplit[2])) # List of functions sorted by their name nameinternalfunctions = [] for functioncompletion in entrypoint.completions(): if functioncompletion.type != "function": continue if not functioncompletion.full_name.startswith(classplit[2]): continue nameinternalfunctions.append(functioncompletion) #print(nameinternalfunctions) # List of functions sorted by their line in the file (reversed) # CODE USED TO ACTUALLY PRINT THE CODE #prevnumber = 0 #numberinternalfunctions = sorted(nameinternalfunctions, key=lambda k: k.line, reverse=True) numberinternalfunctions = sorted(nameinternalfunctions, key=lambda k: k.line) prevnumber = 0 origparent = "TheHiveApi" # Predefined functions? - maybe skip: __init__ skip_functions = ["__init__"] skip_parameters = [""] cnt = 0 for item in numberinternalfunctions: if item.parent().name != origparent: continue # FIXME - prolly wrong if item.name in skip_functions or (item.name.startswith("__") and item.name.endswith("__")): continue # FIXME - remove #print(item.get_line_code()) #if "=" not in item.get_line_code(): # continue #if item.docstring() in item.get_line_code(): # print("NO DOCSTRING FOR: %s. Skipping!" % item.name) # cnt += 1 # continue curfunction = { "name": item.name, "description": "HEY", } params = [] curreturn = {} function = item.docstring().split("\n")[0] for line in item.docstring().split("\n"): if not line: continue linesplit = line.split(" ") try: curname = linesplit[1][:-1] except IndexError as e: print("IndexError: %s. Line: %s" % (e, line)) continue paramfound = False foundindex = 0 cnt = 0 for param in params: #print(param["name"], curname) if param["name"] == curname: #print("ALREADY EXISTS: %s" % curname) paramfound = True foundindex = cnt break cnt += 1 # CBA finding a good parser, as that seemed impossible :( # Skipped :return if line.startswith(":param"): if not paramfound: #print("HERE!: %s" % line) curparam = {} #print(line) curparam["name"] = curname curparam["description"] = " ".join(linesplit[2:]) #print(curparam["description"]) if "\r\n" in curparam["description"]: curparam["description"] = " ".join(curparam["description"].split("\r\n")) if "\n" in curparam["description"]: curparam["description"] = " ".join(curparam["description"].split("\n")) curparam["function"] = function #curparam["docstring"] = item.docstring() params.append(curparam) elif line.startswith(":type"): if paramfound: params[foundindex]["schema"] = {} params[foundindex]["schema"]["type"] = " ".join(linesplit[2:]) #print(params) #print(line) elif line.startswith(":rtype"): curreturn["type"] = " ".join(linesplit[1:]) # Check whether param is required # FIXME - remove #if len(params) != 0: # print(params) # continue #print(function) #print(params) # FIXME - this might crash when missing docstrings # FIXME - is also bad splitt (can be written without e.g. spaces # This should maybe be done first? idk fields = function.split("(")[1][:-1].split(", ") if len(params) == 0: # Handle missing docstrings params = [] for item in fields: params.append({ "name": item, "description": "", "schema": {}, "function": function, }) cnt = 0 for param in params: found = False for field in fields: if param["name"] in field: if "=" in field: param["required"] = False param["default_value"] = field else: param["required"] = True found = True break if not param.get("schema"): #print("Defining object schema for %s" % param["name"]) param["schema"] = {} param["schema"]["type"] = "object" param["position"] = cnt if not found: # FIXME - what here? pass #print("HANDLE NOT FOUND") #print(param) #print(fields) cnt += 1 if len(params) > 0: curfunction["parameters"] = params if not curfunction.get("returns"): curfunction["returns"] = {} curfunction["returns"]["schema"] = {} curfunction["returns"]["schema"]["type"] = "object" #print(curfunction) try: print("Finished prepping %s with %d parameters and return %s" % (item.name, len(curfunction["parameters"]), curfunction["returns"]["schema"]["type"])) except KeyError as e: print("Error: %s" % e) #print("Finished prepping %s with 0 parameters and return %s" % (item.name, curfunction["returns"]["schema"]["type"])) curfunction["parameters"] = [] except AttributeError as e: pass try: data["actions"].append(curfunction) except KeyError: data["actions"] = [] data["actions"].append(curfunction) #return data # FIXME #if cnt == breakcnt: # break #cnt += 1 # Check if # THIS IS TO GET READ THE ACTUAL CODE #functioncode = item.get_line_code(after=prevnumber-item.line-1) #prevnumber = item.line # break return data
tmp.append(j+letter) return tmp ann = lambda x,y: x+y anybody=True print(answer())''' def print_definitions(definitions): if not definitions: print("not found") return for definition in definitions: print( "{type} {name} in {module}.py:{line}".format( type=definition.type, name=definition.full_name, module=definition.module_name, line=definition.line, ) ) lines = src.count("\n") script = jedi.Script(src, lines + 1, len("print("), "test.py") definitions = script.goto_definitions() print_definitions(definitions)
answer="42" an''' def print_definitions(definitions): if not definitions: print("not found") return for definition in definitions: print("{type} {name} in {module}.py:{line}".format( type=definition.type, name=definition.full_name, module=definition.module_name, line=definition.line, )) lines = src.count("\n") script = jedi.Script(src, lines + 1, len("an"), "test.py") completions = script.completions() for completion in completions: print(completion.name) print("-" * 40) definitions = completion.follow_definition() print_definitions(definitions) print(completion.docstring()) print("\n" * 3)
def parse_script(script, ns=None, infer=None, prev="", config=None): """ Parse a script into tokens and use Jedi to infer the fully qualified names of each token. Parameters ---------- script : str the script to tokenize and infer types on ns : dict extra namespace to use with jedi's Interpreter. infer : bool whether to run jedi type inference that can be quite time consuming. prev : str previous lines that lead to this. Yields ------ index index in the tokenstream type pygments token type text text of the token reference : str fully qualified name of the type of current token """ jeds = [] import warnings warnings.simplefilter("ignore", UserWarning) l_delta = len(prev.split("\n")) contextscript = prev + "\n" + script if ns: jeds.append(jedi.Interpreter(contextscript, namespaces=[ns])) jeds.append(jedi.Script(prev + "\n" + script)) P = PythonLexer() for index, type_, text in P.get_tokens_unprocessed(script): line_n, col_n = pos_to_nl(script, index) line_n += l_delta try: ref = None for jed in jeds: failed = "" try: if infer and (text not in (" .=()[],")) and text.isidentifier(): inf = jed.infer(line_n + 1, col_n) if inf: ref = inf[0].full_name # if ref.startswith('builtins'): # ref = '' else: ref = "" except (AttributeError, TypeError, Exception) as e: raise type( e )(f"{contextscript}, {line_n=}, {col_n=}, {prev=}, {jed=}" ) from e failed = "(jedi failed inference)" print("failed inference on ", script, ns, jed, col_n, line_n + 1) break except IndexError: raise ref = "" yield text + failed, ref warnings.simplefilter("default", UserWarning)
def run(self, compare_cb): analysis = jedi.Script(self._source, path=self._path)._analysis() typ_str = lambda inst: 'warning ' if isinstance(inst, Warning) else '' analysis = [(r.line, r.column, typ_str(r) + r.name) for r in analysis] compare_cb(self, analysis, self.collect_comparison())
def test_cache_line_split_issues(): """Should still work even if there's a newline.""" assert jedi.Script('int(\n').call_signatures()[0].name == 'int'
def check(column, call_name, path=None): assert jedi.Script(s, 1, column, path).call_signatures()[0].name == call_name
def refactor(self): script = jedi.Script(self.source, self.line_nr, self.index, self.path) f_name = os.path.basename(self.path) refactor_func = getattr(refactoring, f_name.replace('.py', '')) args = (self.new_name, ) if self.new_name else () return refactor_func(script, *args)
def test_completion(self): assert jedi.Script(''' class DocstringCompletion(): #? [] """ asdfas """''').completions()
def test_call_signatures_stdlib(): code = "import math; math.cos(" s = jedi.Script(code) defs = s.call_signatures() for call_def in defs: assert len(call_def.params) == 1
def get_define_linenumber(source: str, line: int, column: int): script = jedi.Script(source, line, column) return script.completions()
def get_dirs(version_info): return { d.replace(typeshed.TYPESHED_PATH, "").lstrip(os.path.sep) for d in typeshed._get_typeshed_directories(version_info) } def get_stub_files(): def get_map(version_info): return typeshed._create_stub_map(version_info) map_ = typeshed._create_stub_map(TYPESHED_PYTHON3) return os.path.join(TYPESHED_PYTHON3, "functools.pyi") TYPESHED_PYTHON3 = os.path.join(typeshed.TYPESHED_PATH, "stdlib", "3") print("@", TYPESHED_PYTHON3) print("----------------------------------------") for d in get_dirs(PythonVersionInfo("3", "8")): print(d) print("----------------------------------------") print(get_stub_files().replace(os.path.expanduser("~"), "~")) print("----------------------------------------") print(jedi.Script("import string; string.capwords").usages())
def test_scipy_speed(self): s = 'import scipy.weave; scipy.weave.inline(' script = jedi.Script(s, 1, len(s), '') script.function_definition()
def _GetJediScript(request_data): return jedi.Script(request_data['source'], request_data['line'], request_data['col'], request_data['source_path'])
def script(self, environment): return jedi.Script(self.source, path=self.path, environment=environment)
def refactor(self, environment): project = jedi.Project(os.path.join(test_dir, 'refactor')) script = jedi.Script(self._code, path=self._path, project=project, environment=environment) refactor_func = getattr(script, self.refactor_type) return refactor_func(self._line_nr, self._index, **self._kwargs)
def __init__(self, file_path: str, project_path: str = None): super().__init__(file_path, project_path, Language.Python) self.jedi_script = jedi.Script(source=self.source_code, path=self.file_path)
def _get_script(): """ Get the Jedi script object from the source passed on stdin""" source = sys.stdin.read() path = os.environ.get('TM_FILE_PATH') script = jedi.Script(code=source, path=path) return script
def run(self): # Echo server program try: from _pydev_bundle import _pydev_log log = _pydev_log.Log() dbg( SERVER_NAME + ' connecting to java server on %s (%s)' % (HOST, self.port), INFO1) # after being connected, create a socket as a client. self.connect_to_server() dbg(SERVER_NAME + ' Connected to java server', INFO1) while not self.ended: data = '' while data.find(MSG_END) == -1: received = self.socket.recv(BUFFER_SIZE) if len(received) == 0: raise Exit() # ok, connection ended if IS_PYTHON3K: data = data + received.decode('utf-8') else: data = data + received try: try: if data.find(MSG_KILL_SERVER) != -1: dbg(SERVER_NAME + ' kill message received', INFO1) # break if we received kill message. self.ended = True raise Exit() dbg(SERVER_NAME + ' starting keep alive thread', INFO2) if data.find(MSG_PYTHONPATH) != -1: comps = [] for p in _sys_path: comps.append((p, ' ')) self.send(self.get_completions_message( None, comps)) else: data = data[:data.rfind(MSG_END)] if data.startswith(MSG_IMPORTS): data = data[len(MSG_IMPORTS):] data = unquote_plus(data) defFile, comps = _pydev_imports_tipper.generate_tip( data, log) self.send( self.get_completions_message( defFile, comps)) elif data.startswith(MSG_CHANGE_PYTHONPATH): data = data[len(MSG_CHANGE_PYTHONPATH):] data = unquote_plus(data) change_python_path(data) self.send(MSG_OK) elif data.startswith(MSG_JEDI): data = data[len(MSG_JEDI):] data = unquote_plus(data) line, column, encoding, path, source = data.split( '|', 4) try: import jedi # @UnresolvedImport except: self.send( self.get_completions_message( None, [('Error on import jedi', 'Error importing jedi', '')])) else: script = jedi.Script( # Line +1 because it expects lines 1-based (and col 0-based) source=source, line=int(line) + 1, column=int(column), source_encoding=encoding, path=path, ) lst = [] for completion in script.completions(): t = completion.type if t == 'class': t = '1' elif t == 'function': t = '2' elif t == 'import': t = '0' elif t == 'keyword': continue # Keywords are already handled in PyDev elif t == 'statement': t = '3' else: t = '-1' # gen list(tuple(name, doc, args, type)) lst.append( (completion.name, '', '', t)) self.send( self.get_completions_message( 'empty', lst)) elif data.startswith(MSG_SEARCH): data = data[len(MSG_SEARCH):] data = unquote_plus(data) ( f, line, col ), foundAs = _pydev_imports_tipper.search_definition( data) self.send( self.get_completions_message( f, [(line, col, foundAs)])) elif data.startswith(MSG_CHANGE_DIR): data = data[len(MSG_CHANGE_DIR):] data = unquote_plus(data) complete_from_dir(data) self.send(MSG_OK) else: self.send(MSG_INVALID_REQUEST) except Exit: self.send( self.get_completions_message( None, [('Exit:', 'SystemExit', '')])) raise except: dbg(SERVER_NAME + ' exception occurred', ERROR) s = StringIO.StringIO() traceback.print_exc(file=s) err = s.getvalue() dbg(SERVER_NAME + ' received error: ' + str(err), ERROR) self.send( self.get_completions_message( None, [('ERROR:', '%s\nLog:%s' % (err, log.get_contents()), '')])) finally: log.clear_log() self.socket.close() self.ended = True raise Exit() # connection broken except Exit: if self.exit_process_on_kill: sys.exit(0) # No need to log SystemExit error except: s = StringIO.StringIO() exc_info = sys.exc_info() traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], limit=None, file=s) err = s.getvalue() dbg(SERVER_NAME + ' received error: ' + str(err), ERROR) raise
def test_function_doc(self): defs = jedi.Script(""" def func(): '''Docstring of `func`.''' func""").goto_definitions() self.assertEqual(defs[0].raw_doc, 'Docstring of `func`.')
def test_not_importable_file(): src = 'import not_importable_file as x; x.' assert not jedi.Script(src, path='example.py').completions()
def test_attribute_docstring(self): defs = jedi.Script(""" x = None '''Docstring of `x`.''' x""").goto_definitions() self.assertEqual(defs[0].raw_doc, 'Docstring of `x`.')
def test_scipy_speed(self): s = 'import scipy.weave; scipy.weave.inline(' script = jedi.Script(s, 1, len(s), '') script.call_signatures()
def get_jedi_doc(code, line, col): script = jedi.Script(code, path='temp.py') names = script.help(line, col) return names