def rpc_get_definition(self, project_root, filename, source, offset): source = get_source(source) line, column = pos_to_linecol(source, offset) sys.path.append(project_root) try: script = self.jedi.Script(source, line, column, filename, encoding='utf-8') locations = script.goto_definitions() # goto_definitions() can return silly stuff like __builtin__ # for int variables, so we fall back on goto() in those # cases. See issue #76. if ( locations and locations[0].module_path is None ): locations = script.goto_assignments() finally: sys.path.pop() if not locations: return None else: loc = locations[-1] try: if loc.module_path: with open(loc.module_path) as f: offset = linecol_to_pos(f.read(), loc.line, loc.column) except IOError: return None return (loc.module_path, offset)
def rpc_get_calltip(self, project_root, filename, source, offset): source = get_source(source) line, column = pos_to_linecol(source, offset) sys.path.append(project_root) try: calls = run_with_debug(self.jedi, 'call_signatures', source=source, line=line, column=column, path=filename, encoding='utf-8') finally: sys.path.pop() if calls: call = calls[0] else: call = None if not call: return None return { "name": call.name, "index": call.index, "params": [param.description for param in call.params] }
def rpc_get_usages(self, project_root, filename, source, offset): """Return the uses of the symbol at offset. Returns a list of occurrences of the symbol, as dicts with the fields name, filename, and offset. """ source = get_source(source) line, column = pos_to_linecol(source, offset) sys.path.append(project_root) try: uses = run_with_debug(self.jedi, 'usages', source=source, line=line, column=column, path=filename, encoding='utf-8', re_raise=(self.jedi.NotFoundError,)) except self.jedi.NotFoundError: return [] finally: sys.path.pop() result = [] for use in uses: if use.module_path == filename: offset = linecol_to_pos(source, use.line, use.column) else: with open(use.module_path) as f: text = f.read() offset = linecol_to_pos(text, use.line, use.column) result.append({"name": use.name, "filename": use.module_path, "offset": offset}) return result
def test_should_return_file_contents(self): fd, filename = tempfile.mkstemp(prefix="elpy-test-") self.addCleanup(os.remove, filename) with open(filename, "w") as f: f.write("file contents") fileobj = {'filename': filename} self.assertEqual(nativebackend.get_source(fileobj), "file contents")
def test_should_support_utf8(self): fd, filename = tempfile.mkstemp(prefix="elpy-test-") self.addCleanup(os.remove, filename) with open(filename, "wb") as f: f.write(u"möp".encode("utf-8")) source = nativebackend.get_source({'filename': filename}) self.assertEqual(source, u"möp")
def test_should_clean_up_tempfile(self): fd, filename = tempfile.mkstemp(prefix="elpy-test-") with open(filename, "w") as f: f.write("file contents") fileobj = {"filename": filename, "delete_after_use": True} self.assertEqual(nativebackend.get_source(fileobj), "file contents") self.assertFalse(os.path.exists(filename))
def test_should_return_file_contents(self): fd, filename = tempfile.mkstemp(prefix="elpy-test-") self.addCleanup(os.remove, filename) with open(filename, "w") as f: f.write("file contents") fileobj = {"filename": filename} self.assertEqual(nativebackend.get_source(fileobj), "file contents")
def test_should_clean_up_tempfile(self): fd, filename = tempfile.mkstemp(prefix="elpy-test-") with open(filename, "w") as f: f.write("file contents") fileobj = {'filename': filename, 'delete_after_use': True} self.assertEqual(nativebackend.get_source(fileobj), "file contents") self.assertFalse(os.path.exists(filename))
def rpc_get_docstring(self, project_root, filename, source, offset): """Return a docstring for the symbol at offset. This uses the nativebackend, as apparently, Jedi does not know how to do this. It can do a completion and find docstrings for that, but not for the symbol at a location. Huh. """ source = get_source(source) return super(JediBackend, self).rpc_get_docstring(project_root, filename, source, offset)
def rpc_get_completions(self, project_root, filename, source, offset): source = get_source(source) line, column = pos_to_linecol(source, offset) sys.path.append(project_root) try: script = self.jedi.Script(source, line, column, filename, encoding='utf-8') proposals = script.completions() finally: sys.path.pop() return [[proposal.complete, proposal.docstring()] for proposal in proposals]
def rpc_get_completions(self, project_root, filename, source, offset): source = get_source(source) line, column = pos_to_linecol(source, offset) sys.path.append(project_root) try: proposals = run_with_debug(self.jedi, 'completions', source=source, line=line, column=column, path=filename, encoding='utf-8') finally: sys.path.pop() return [{'suffix': proposal.complete, 'annotation': proposal.type, 'meta': proposal.description, 'docstring': proposal.docstring(fast=True)} for proposal in proposals]
def rpc_get_calltip(self, project_root, filename, source, offset): source = get_source(source) offset = find_called_name_offset(source, offset) project = self.get_project(project_root) resource = self.get_resource(project, filename) try: return self.codeassist.get_calltip(project, source, offset, resource, MAXFIXES, remove_self=True) except (self.ModuleSyntaxError, IndentationError): # Rope can't parse this file return None except (self.BadIdentifierError, IndexError): # IndexError seems to be a bug in Rope. I don't know what # it causing it, exactly. return None
def rpc_get_docstring(self, project_root, filename, source, offset): source = get_source(source) project = self.get_project(project_root) resource = self.get_resource(project, filename) try: docstring = self.codeassist.get_doc(project, source, offset, resource, MAXFIXES) except (self.ModuleSyntaxError, IndentationError): # Rope can't parse this file docstring = None except (self.BadIdentifierError, IndexError): docstring = None if docstring is None: super(RopeBackend, self).rpc_get_docstring(project_root, filename, source, offset) else: return docstring
def rpc_get_definition(self, project_root, filename, source, offset): source = get_source(source) project = self.get_project(project_root) resource = self.get_resource(project, filename) # The find_definition call fails on an empty strings if source == '': return None try: location = self.findit.find_definition(project, source, offset, resource, MAXFIXES) except (self.ModuleSyntaxError, IndentationError): # Rope can't parse this file return None if location is None: return None else: return (location.resource.real_path, location.offset)
def rpc_get_calltip(self, project_root, filename, source, offset): source = get_source(source) line, column = pos_to_linecol(source, offset) sys.path.append(project_root) try: calls = run_with_debug(self.jedi, 'call_signatures', source=source, line=line, column=column, path=filename, encoding='utf-8') finally: sys.path.pop() if calls: call = calls[0] else: call = None if not call: return None return {"name": call.name, "index": call.index, "params": [param.description for param in call.params]}
def rpc_get_calltip(self, project_root, filename, source, offset): source = get_source(source) line, column = pos_to_linecol(source, offset) sys.path.append(project_root) try: script = self.jedi.Script(source, line, column, filename, encoding='utf-8') call = script.call_signatures() if call: call = call[0] else: call = None finally: sys.path.pop() if call is None: return None return "{0}({1})".format(call.name, ", ".join(param.description.strip() for param in call.params))
def rpc_get_definition(self, project_root, filename, source, offset): source = get_source(source) project = self.get_project(project_root) resource = self.get_resource(project, filename) # The find_definition call fails on an empty strings if source == '': return None try: location = self.findit.find_definition(project, source, offset, resource, MAXFIXES) except (IndentationError, self.ModuleSyntaxError, self.BadIdentifierError): # Rope can't parse this file return None if location is None: return None else: return (location.resource.real_path, location.offset)
def rpc_get_completions(self, project_root, filename, source, offset): source = get_source(source) line, column = pos_to_linecol(source, offset) sys.path.append(project_root) try: proposals = run_with_debug(self.jedi, 'completions', source=source, line=line, column=column, path=filename, encoding='utf-8') finally: sys.path.pop() return [{ 'suffix': proposal.complete, 'annotation': proposal.type, 'meta': proposal.description, 'docstring': proposal.docstring(fast=True) } for proposal in proposals]
def rpc_get_completions(self, project_root, filename, source, offset): source = get_source(source) project = self.get_project(project_root) resource = self.get_resource(project, filename) try: proposals = self.codeassist.code_assist(project, source, offset, resource, maxfixes=MAXFIXES) starting_offset = self.codeassist.starting_offset(source, offset) except self.ModuleSyntaxError: # Rope can't parse this file return [] except IndentationError: # Rope can't parse this file return [] except IndexError as e: # Bug in Rope, see #186 return [] prefixlen = offset - starting_offset return [[proposal.name[prefixlen:], proposal.get_doc()] for proposal in proposals]
def rpc_get_usages(self, project_root, filename, source, offset): """Return the uses of the symbol at offset. Returns a list of occurrences of the symbol, as dicts with the fields name, filename, and offset. """ source = get_source(source) line, column = pos_to_linecol(source, offset) sys.path.append(project_root) try: uses = run_with_debug(self.jedi, 'usages', source=source, line=line, column=column, path=filename, encoding='utf-8', re_raise=(self.jedi.NotFoundError, )) except self.jedi.NotFoundError: return [] finally: sys.path.pop() result = [] for use in uses: if use.module_path == filename: offset = linecol_to_pos(source, use.line, use.column) else: with open(use.module_path) as f: text = f.read() offset = linecol_to_pos(text, use.line, use.column) result.append({ "name": use.name, "filename": use.module_path, "offset": offset }) return result
def rpc_get_completions(self, project_root, filename, source, offset): source = get_source(source) project = self.get_project(project_root) resource = self.get_resource(project, filename) try: proposals = self.codeassist.code_assist(project, source, offset, resource, maxfixes=MAXFIXES) starting_offset = self.codeassist.starting_offset(source, offset) except self.ModuleSyntaxError: # Rope can't parse this file return [] except IndentationError: # Rope can't parse this file return [] except IndexError: # Bug in Rope, see #186 return [] prefixlen = offset - starting_offset result = [] for proposal in proposals: doc = proposal.get_doc() if doc: meta = doc.strip().split("\n", 1)[0] else: meta = None result.append({ 'suffix': proposal.name[prefixlen:], 'docstring': doc, 'annotation': proposal.type, 'meta': meta }) return result
def rpc_get_calltip(self, project_root, filename, source, offset): source = get_source(source) offset = find_called_name_offset(source, offset) project = self.get_project(project_root) resource = self.get_resource(project, filename) if 0 < offset < len(source) and source[offset] == ')': offset -= 1 try: calltip = self.codeassist.get_calltip(project, source, offset, resource, MAXFIXES, remove_self=True) if calltip: calltip = calltip.replace(".__init__(", "(") calltip = calltip.replace("(self)", "()") calltip = calltip.replace("(self, ", "(") # "elpy.tests.support.source_and_offset(source)" # => # "support.source_and_offset(source)" try: openpos = calltip.index("(") period2 = calltip.rindex(".", 0, openpos) period1 = calltip.rindex(".", 0, period2) calltip = calltip[period1 + 1:] except ValueError: pass return calltip except (self.ModuleSyntaxError, IndentationError): # Rope can't parse this file return None except (self.BadIdentifierError, IndexError): # IndexError seems to be a bug in Rope. I don't know what # it causing it, exactly. return None
def rpc_get_definition(self, project_root, filename, source, offset): source = get_source(source) line, column = pos_to_linecol(source, offset) sys.path.append(project_root) try: locations = run_with_debug(self.jedi, 'goto_definitions', source=source, line=line, column=column, path=filename, encoding='utf-8') # goto_definitions() can return silly stuff like __builtin__ # for int variables, so we fall back on goto() in those # cases. See issue #76. if (locations and locations[0].module_path is None): locations = run_with_debug(self.jedi, 'goto_assignments', source=source, line=line, column=column, path=filename, encoding='utf-8') finally: sys.path.pop() if not locations: return None else: loc = locations[-1] try: if loc.module_path: with open(loc.module_path) as f: offset = linecol_to_pos(f.read(), loc.line, loc.column) except IOError: return None return (loc.module_path, offset)
def test_should_return_string_by_default(self): self.assertEqual(nativebackend.get_source("foo"), "foo")