Ejemplo n.º 1
0
def goto_definition(event):
    if not control_is_pressed(event.state):
        return

    assert isinstance(event.widget, tk.Text)
    text = event.widget

    source = text.get("1.0", "end")
    index = text.index("insert")
    index_parts = index.split(".")
    line, column = int(index_parts[0]), int(index_parts[1])
    # TODO: find current editor filename
    from jedi import Script

    script = Script(source, line=line, column=column, path="")
    defs = script.goto_definitions()
    if len(defs) > 0:
        module_path = defs[0].module_path
        module_name = defs[0].module_name
        line = defs[0].line
        if module_path and line is not None:
            get_workbench().get_editor_notebook().show_file(module_path, line)
        elif module_name == "" and line is not None:  # current editor
            get_workbench().get_editor_notebook().get_current_editor(
            ).select_range(line)
Ejemplo n.º 2
0
 def test_add_dynamic_mods(self):
     fname = '__main__.py'
     api.settings.additional_dynamic_modules = [fname]
     # Fictional module that defines a function.
     src1 = "def r(a): return a"
     # Other fictional modules in another place in the fs.
     src2 = 'from .. import setup; setup.r(1)'
     script = Script(src1, path='../setup.py')
     imports.load_module(script._evaluator, os.path.abspath(fname), src2)
     result = script.goto_definitions()
     assert len(result) == 1
     assert result[0].description == 'class int'
Ejemplo n.º 3
0
 def test_add_dynamic_mods(self):
     fname = '__main__.py'
     api.settings.additional_dynamic_modules = [fname]
     # Fictional module that defines a function.
     src1 = "def r(a): return a"
     # Other fictional modules in another place in the fs.
     src2 = 'from .. import setup; setup.r(1)'
     script = Script(src1, path='../setup.py')
     imports.load_module(script._evaluator, os.path.abspath(fname), src2)
     result = script.goto_definitions()
     assert len(result) == 1
     assert result[0].description == 'class int'
def _get_defs(source, func, line=None, column=None):
    error = None
    try:
        num_lines = len(source.split('\n'))
        if line is None:
            line = num_lines
        s = Script(source, line=line, column=column)
        defs = s.goto_definitions()
        sigs = s.call_signatures()
    except:
        print('Exception in defs thread')
        traceback.print_exc()
        defs = []
        sigs = []
        error = 'Could not retrieve docstring'

    mainthread(WrappablePartial(func, defs, sigs, error=error))()
Ejemplo n.º 5
0
 def run(self, edit) -> None:
     try:
         loc = self.view.sel()[0].end()
         row, col = self.view.rowcol(loc)
         script = Script(source=(self.view.substr(
             sublime.Region(0, self.view.size()))),
                         line=(row + 1),
                         column=col,
                         path=self.view.file_name())
         definitions = script.goto_definitions()
         if definitions:
             doc = definitions[-1].doc or 'No docstring found'
             self.view.show_popup(doc.replace('\n', '<br>'))
         else:
             self.view.show_popup('No docstring found')
     except Exception:
         self.view.show_popup('No docstring found')
         raise
Ejemplo n.º 6
0
def make_definitions():
    """
    Return a list of definitions for parametrized tests.

    :rtype: [jedi.api_classes.BaseDefinition]
    """
    source = dedent(
        """
    import sys

    class C:
        pass

    x = C()

    def f():
        pass

    def g():
        yield

    h = lambda: None
    """
    )

    definitions = []
    definitions += defined_names(source)

    source += dedent(
        """
    variable = sys or C or x or f or g or g() or h"""
    )
    lines = source.splitlines()
    script = Script(source, len(lines), len("variable"), None)
    definitions += script.goto_definitions()

    script2 = Script(source, 4, len("class C"), None)
    definitions += script2.usages()

    source_param = "def f(a): return a"
    script_param = Script(source_param, 1, len(source_param), None)
    definitions += script_param.goto_assignments()

    return definitions
Ejemplo n.º 7
0
    def finddocstring(self):
        ''' find the docstring at current cursor location
        '''
        import StringIO
        from jedi import Script

        i=editor.get_selection()
        t=editor.get_text()
        (line,txt)=[(line,n) for (line,n) in enumerate(StringIO.StringIO(editor.get_text()[:i[1]]))][-1]
        script = Script(t, line+1, len(txt))

        dfn = script.goto_definitions()
        if dfn:
            doc=dfn[0].doc
            import ui
            v=ui.TextView()
            v.width=100
            v.height=50
            v.text=doc
            editor._set_toolbar(v)
Ejemplo n.º 8
0
def finddocstring(self):
	''' find the docstring at current cursor location
	'''
	import StringIO
	from jedi import Script
	
	i=editor.get_selection()
	t=editor.get_text()
	(line,txt)=[(line,n) for (line,n) in enumerate(StringIO.StringIO(editor.get_text()[:i[1]]))][-1]
	script = Script(t, line+1, len(txt))
	
	dfn = script.goto_definitions()
	if dfn:
		doc=dfn[0].doc
		import ui
		v=ui.TextView()
		v.width=200
		v.height=250
		v.text=doc
		v.present('popover')
Ejemplo n.º 9
0
 def run(self, edit) -> None:
     try:
         loc = self.view.sel()[0].end()
         row, col = self.view.rowcol(loc)
         script = Script(source=(self.view.substr(
             sublime.Region(0, self.view.size()))),
                         line=(row + 1),
                         column=col,
                         path=self.view.file_name())
         definitions = script.goto_definitions()
         if definitions:
             def_dir = definitions[0].defined_names()
             pop_contents = '<br>'.join(
                 clean_list([i.description
                             for i in def_dir])) or 'No dir found'
             self.view.show_popup(pop_contents)
         else:
             self.view.show_popup('No dir found')
     except Exception:
         self.view.show_popup('No dir found')
         raise
Ejemplo n.º 10
0
def make_definitions():
    """
    Return a list of definitions for parametrized tests.

    :rtype: [jedi.api_classes.BaseDefinition]
    """
    source = dedent("""
    import sys

    class C:
        pass

    x = C()

    def f():
        pass

    def g():
        yield

    h = lambda: None
    """)

    definitions = []
    definitions += defined_names(source)

    source += dedent("""
    variable = sys or C or x or f or g or g() or h""")
    lines = source.splitlines()
    script = Script(source, len(lines), len('variable'), None)
    definitions += script.goto_definitions()

    script2 = Script(source, 4, len('class C'), None)
    definitions += script2.usages()

    source_param = "def f(a): return a"
    script_param = Script(source_param, 1, len(source_param), None)
    definitions += script_param.goto_assignments()

    return definitions
Ejemplo n.º 11
0
 def run(self, edit) -> None:
     try:
         loc = self.view.sel()[0].end()
         row, col = self.view.rowcol(loc)
         script = Script(source=(self.view.substr(
             sublime.Region(0, self.view.size()))),
                         line=(row + 1),
                         column=col,
                         path=self.view.file_name())
         definitions = script.goto_definitions()
         if definitions:
             try:
                 params = definitions[0].params
                 status_line = ', '.join([i.description for i in params
                                          ]) or 'No params available'
                 self.view.window().status_message(status_line)
             except Exception:
                 self.view.window().status_message('No params available')
                 raise
         else:
             self.view.window().status_message('No params available')
     except Exception:
         self.view.window().status_message('No params available')
         raise