Exemplo n.º 1
0
    def get_code_definition(self, code, line, column):
        # Calculate the offset
        index = 0
        for i in range(int(line)):
            try:
                index = code.index('\n', index+1)
            except:
                return ""

        offset = index + int(column) + 1

        try:
            definition = get_definition_location(self.rope_project, code, offset)
        except:
            # Something bad happened
            return ""

        if definition:
            fileobject = definition[0]
            lineno = definition[1]

            if fileobject and lineno:
                filepath = fileobject.path        
                return "%s:%s" % (filepath, lineno)
            else:
                return ""
        else:
            return ""
Exemplo n.º 2
0
 def test_get_definition_location_dotted_names(self):
     code = 'class AClass(object):\n' \
            '    @staticmethod\n' \
            '    def a_method():\n' \
            '        pass\n' \
            'AClass.a_method()'
     result = get_definition_location(self.project, code, len(code) - 3)
     self.assertEquals((None, 2), result)
Exemplo n.º 3
0
 def test_get_location_on_relative_imports(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod1 = testutils.create_module(self.project, 'mod1', pkg)
     mod2 = testutils.create_module(self.project, 'mod2', pkg)
     mod1.write('def a_func():\n    pass\n')
     code = 'import mod1\nmod1.a_func\n'
     result = get_definition_location(self.project, code,
                                      len(code) - 2, mod2)
     self.assertEquals((mod1, 1), result)
Exemplo n.º 4
0
 def definition_location(self, source, project_path, file_path, loc):
     project = self.project_for(project_path, file_path)
     resource = libutils.path_to_resource(project, file_path)
     try:
         def_resource, def_lineno = get_definition_location(
             project, source, loc, resource=resource, maxfixes=3)
     except ModuleSyntaxError:
         return None, None
     return def_resource.real_path, def_lineno
Exemplo n.º 5
0
    def test_get_definition_location_for_nested_packages(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        pkg1 = testutils.create_package(self.project, 'pkg1')
        pkg2 = testutils.create_package(self.project, 'pkg2', pkg1)
        mod1.write('import pkg1.pkg2.mod2')

        init_dot_py = pkg2.get_child('__init__.py')
        found_pyname = get_definition_location(self.project, mod1.read(),
                                               mod1.read().index('pkg2') + 1)
        self.assertEquals(init_dot_py, found_pyname[0])
Exemplo n.º 6
0
 def _base_definition_location(self):
     self._check_project()
     resource, offset = self._get_location()
     maxfixes = self.env.get("codeassist_maxfixes")
     try:
         definition = codeassist.get_definition_location(self.project, self._get_text(), offset, resource, maxfixes)
     except exceptions.BadIdentifierError:
         return None
     if tuple(definition) != (None, None):
         return definition
     return None
Exemplo n.º 7
0
 def goto_definition(self):
     self._check_project()
     resource, offset = self._get_location()
     maxfixes = lisp['ropemacs-codeassist-maxfixes'].value()
     definition = codeassist.get_definition_location(
         self.project, self._get_text(), offset, resource, maxfixes)
     if tuple(definition) != (None, None):
         lisp.push_mark()
         self._goto_location(definition[0], definition[1])
     else:
         lisputils.message('Cannot find the definition!')
Exemplo n.º 8
0
 def goto_definition(self):
     self._check_project()
     resource, offset = self._get_location()
     maxfixes = self.env.get('codeassist_maxfixes')
     definition = codeassist.get_definition_location(
         self.project, self._get_text(), offset, resource, maxfixes)
     if tuple(definition) != (None, None):
         self.env.push_mark()
         self._goto_location(definition[0], definition[1])
     else:
         self.env.message('Cannot find the definition!')
Exemplo n.º 9
0
def goto_definition():
    with ropemate.context as context:
        offset = caret_position(context.input)
        found_resource, line = None, None
        try:
            found_resource, line = codeassist.get_definition_location(
                context.project, context.input, offset, context.resource)
        except rope.base.exceptions.BadIdentifierError, e:
            # fail silently -> the user selected empty space etc
            pass
        except Exception, e:
            tooltip(e)
Exemplo n.º 10
0
 def _base_definition_location(self):
     self._check_project()
     resource, offset = self._get_location()
     maxfixes = self.env.get('codeassist_maxfixes')
     try:
         definition = codeassist.get_definition_location(
             self.project, self._get_text(), offset, resource, maxfixes)
     except exceptions.BadIdentifierError:
         return None
     if tuple(definition) != (None, None):
         return definition
     return None
Exemplo n.º 11
0
 def run(self, edit, block=False):
     with ropemate.context_for(self.view) as context:
         offset = self.view.sel()[0].a
         found_resource, line = None, None
         try:
             found_resource, line = codeassist.get_definition_location(
                 context.project, context.input, offset, context.resource)
         except rope.base.exceptions.BadIdentifierError, e:
             # fail silently -> the user selected empty space etc
             pass
         except Exception, e:
             print e
Exemplo n.º 12
0
 def run(self, edit, block=False):
     with ropemate.ropecontext(self.view) as context:
         offset = self.view.sel()[0].a
         found_resource, line = None, None
         try:
             found_resource, line = codeassist.get_definition_location(
                 context.project, context.input, offset, context.resource)
         except rope.base.exceptions.BadIdentifierError, e:
             # fail silently -> the user selected empty space etc
             pass
         except Exception, e:
             print e
Exemplo n.º 13
0
    def test_get_definition_location_for_nested_packages(self):
        pycore = self.project.pycore
        mod1 = testutils.create_module(self.project, 'mod1')
        pkg1 = testutils.create_package(self.project, 'pkg1')
        pkg2 = testutils.create_package(self.project, 'pkg2', pkg1)
        mod2 = testutils.create_module(self.project, 'mod2', pkg2)
        mod1.write('import pkg1.pkg2.mod2')

        mod1_scope = pycore.resource_to_pyobject(mod1).get_scope()
        init_dot_py = pkg2.get_child('__init__.py')
        found_pyname = get_definition_location(self.project, mod1.read(),
                                               mod1.read().index('pkg2') + 1)
        self.assertEquals(init_dot_py, found_pyname[0])
Exemplo n.º 14
0
    def test_get_definition_location_for_nested_packages(self):
        pycore = self.project.pycore
        mod1 = testutils.create_module(self.project, 'mod1')
        pkg1 = testutils.create_package(self.project, 'pkg1')
        pkg2 = testutils.create_package(self.project, 'pkg2', pkg1)
        mod2 = testutils.create_module(self.project, 'mod2', pkg2)
        mod1.write('import pkg1.pkg2.mod2')

        mod1_scope = pycore.resource_to_pyobject(mod1).get_scope()
        init_dot_py = pkg2.get_child('__init__.py')
        found_pyname = get_definition_location(self.project, mod1.read(),
                                               mod1.read().index('pkg2') + 1)
        self.assertEqual(init_dot_py, found_pyname[0])
Exemplo n.º 15
0
    def DefinitionLocationRequest(self, request, response):
        """
    Finds the definition location of the current symbol.
    """

        project, resource, source, offset = self._Context(request.context)
        resource, offset = codeassist.get_definition_location(
            project, source, offset, maxfixes=self.MAXFIXES, resource=resource)

        if resource is not None:
            response.file_path = resource.real_path

        if offset is not None:
            response.line = offset
Exemplo n.º 16
0
def goto():
    """ Goto definition. """

    with RopeContext() as ctx:
        source, offset = env.get_offset_params()

        found_resource, line = codeassist.get_definition_location(ctx.project, source, offset, ctx.resource, maxfixes=3)

        if not found_resource:
            env.error("Definition not found")
            return

        env.goto_file(found_resource.real_path, cmd=ctx.options.get("goto_definition_cmd"))
        env.goto_line(line)
Exemplo n.º 17
0
  def DefinitionLocationRequest(self, request, response):
    """
    Finds the definition location of the current symbol.
    """

    project, resource, source, offset = self._Context(request.context)
    resource, offset = codeassist.get_definition_location(
        project, source, offset,
        maxfixes=self.MAXFIXES, resource=resource)

    if resource is not None:
      response.file_path = resource.real_path
    
    if offset is not None:
      response.line = offset
Exemplo n.º 18
0
def goto():
    """ Goto definition. """
    with RopeContext() as ctx:
        source, offset = env.get_offset_params()

        found_resource, line = codeassist.get_definition_location(
            ctx.project, source, offset, ctx.resource, maxfixes=3)

        if not found_resource:
            env.error('Definition not found')
            return

        env.goto_file(
            found_resource.real_path,
            cmd=ctx.options.get('goto_definition_cmd'))
        env.goto_line(line)
    def execute(self):
        #TODO: support multiple matches
        if tm.PROJECT_DIRECTORY is None:
            tm.exit_show_tool_tip('You must create a project first!')

        project = self.get_project()
        caret_index = self.source.find(tm.CURRENT_LINE) + tm.LINE_INDEX
        try:
            resource, line = codeassist.get_definition_location(project, self.source, caret_index)
        except:
            resource = None

        if resource is not None:
            subprocess.Popen(['open', 'txmt://open?url=file://%s&line=%d' % (urllib.quote(resource.real_path), line)])
        else:
            tm.exit_show_tool_tip('Definition not found.')
Exemplo n.º 20
0
def goto():
    """ Goto definition. """

    with RopeContext() as ctx:
        source, offset = get_assist_params()

        found_resource, line = codeassist.get_definition_location(
            ctx.project, source, offset, ctx.resource, maxfixes=3)

        if not os.path.abspath(found_resource.path) == vim.current.buffer.name:
            vim.command("%s +%s %s" % (
                ctx.options.get('goto_definition_cmd'),
                line, found_resource.path))

        else:
            vim.current.window.cursor = (
                line, int(vim.eval('indent(%s)' % line)))
Exemplo n.º 21
0
    def goto_definition(self):
        """
        Tries to find the definition for the currently selected scope;

            * if the definition is found, returns a tuple containing the file path and the line number (e.g. ``('/Users/fabiocorneti/src/def.py', 23))``
            * if no definition is found, returns None

        """
        #TODO: support multiple matches
        if TM_PROJECT_DIRECTORY is None:
            return None
        project = Project(TM_PROJECT_DIRECTORY)
        caret_index = self.source.find(TM_CURRENT_LINE) + TM_LINE_INDEX
        resource, line = codeassist.get_definition_location(project, self.source, caret_index)
        if resource is not None:
            return 'txmt://open?url=file://%s&line=%d' % (urllib.quote(resource.real_path), line)
        else:
            return ''
Exemplo n.º 22
0
    def goto_definition(self):
        project = self.project_manager.project

        project.validate()

        current_resource = self.get_rope_resource(project)

        from rope.contrib import codeassist

        source, offset = self.get_source_and_offset()
        try:
            resource, line = codeassist.get_definition_location(
                project, source, offset,
                resource=current_resource, maxfixes=3)
        except Exception, e:
            import traceback
            traceback.print_exc()
            self.editor.message(str(e), 5000)
            return
Exemplo n.º 23
0
def goto_definition():
    with ropemate.context as context:
        offset = caret_position(context.input)
        found_resource, line = None, None
        try:
            found_resource, line = codeassist.get_definition_location(
                context.project, context.input, offset, context.resource)
        except rope.base.exceptions.BadIdentifierError as e:
            # fail silently -> the user selected empty space etc
            pass
        except Exception as e:
            tooltip(e)

        if found_resource is not None:
            path = os.path.join(context.project_dir, found_resource.path)
            return 'txmt://open?url=file://%s&line=%d' % (
                    urlquote(path), line)
        elif line is not None:
            return 'txmt://open?line=%d' % line
        return ''
Exemplo n.º 24
0
    def goto_definition(self, *args):
        project = self.project
        if not project:
            project = getattr(self.editor, 'ropeproject', None)
            if not project:
                self.editor.update_message(_("Can't find project path"), "no", 1)
                return False

        project.validate()

        current_resource = self.get_rope_resource(project)

        try:
            resource, line = codeassist.get_definition_location(
                project, *self.get_source_and_offset(),
                resource=current_resource)
        except Exception, e:
            self.editor.update_message(str(e), "no", 1)
            traceback.print_exc()
            return False
Exemplo n.º 25
0
    def definition_location(self, source, project_path, file_path, loc):
        """
        Get a global definition location and returns it back to the editor

        :param source: the document source
        :param project_path: the actual project_path
        :param file_path: the actual file path
        :param loc: the buffer location
        :returns: a tuple containing the path and the line number
        """

        project, resource = self._get_resource(project_path, file_path, source)

        try:
            def_resource, def_lineno = get_definition_location(project, source, loc, resource=resource, maxfixes=3)
        except ModuleSyntaxError:
            real_path, def_lineno = (None, None)
        finally:
            real_path = def_resource.real_path

        return real_path, def_lineno
Exemplo n.º 26
0
    def definition_location(self, source, project_path, file_path, loc):
        """
        Get a global definition location and returns it back to the editor

        :param source: the document source
        :param project_path: the actual project_path
        :param file_path: the actual file path
        :param loc: the buffer location
        :returns: a tuple containing the path and the line number
        """

        project, resource = self._get_resource(project_path, file_path, source)

        try:
            def_resource, def_lineno = get_definition_location(
                project, source, loc, resource=resource, maxfixes=3)
        except ModuleSyntaxError:
            real_path, def_lineno = (None, None)
        finally:
            real_path = def_resource.real_path

        return real_path, def_lineno
Exemplo n.º 27
0
    def goto_definition(self, *args):
        project = self.project
        if not project:
            project = getattr(self.editor, 'ropeproject', None)
            if not project:
                self.editor.update_message(_("Can't find project path"), "no",
                                           1)
                return False

        project.validate()

        current_resource = self.get_rope_resource(project)

        try:
            resource, line = codeassist.get_definition_location(
                project,
                *self.get_source_and_offset(),
                resource=current_resource)
        except Exception, e:
            self.editor.update_message(str(e), "no", 1)
            traceback.print_exc()
            return False
Exemplo n.º 28
0
 def test_if_scopes_in_other_scopes_for_get_definition_location(self):
     code = 'def f(a_var):\n    pass\na_var = 10\n' \
            'if True:\n    print a_var\n'
     result = get_definition_location(self.project, code, len(code) - 3)
     self.assertEquals((None, 3), result)
Exemplo n.º 29
0
 def test_get_definition_location_dot_line_break_inside_parens(self):
     code = 'class A(object):\n    def a_method(self):\n        pass\n' + \
            '(A.\na_method)'
     result = get_definition_location(self.project, code,
                                      code.rindex('a_method') + 1)
     self.assertEquals((None, 2), result)
Exemplo n.º 30
0
 def test_get_definition_location_dot_spaces(self):
     code = 'class AClass(object):\n    ' \
            '@staticmethod\n    def a_method():\n' \
            '        pass\nAClass.\\\n     a_method()'
     result = get_definition_location(self.project, code, len(code) - 3)
     self.assertEquals((None, 2), result)
Exemplo n.º 31
0
 def test_get_definition_location_unknown(self):
     code = 'a_func()\n'
     result = get_definition_location(self.project, code, len(code) - 3)
     self.assertEquals((None, None), result)
Exemplo n.º 32
0
 def test_get_definition_location_dotted_module_names(self):
     module_resource = testutils.create_module(self.project, 'mod')
     module_resource.write('def a_func():\n    pass\n')
     code = 'import mod\nmod.a_func()'
     result = get_definition_location(self.project, code, len(code) - 3)
     self.assertEquals((module_resource, 1), result)
Exemplo n.º 33
0
 def test_get_definition_location_underlined_names(self):
     code = 'def a_sample_func():\n    pass\na_sample_func()'
     result = get_definition_location(self.project, code, len(code) - 11)
     self.assertEquals((None, 1), result)
Exemplo n.º 34
0
 def test_get_definition_location_underlined_names(self):
     code = 'def a_sample_func():\n    pass\na_sample_func()'
     result = get_definition_location(self.project, code, len(code) - 11)
     self.assertEquals((None, 1), result)
Exemplo n.º 35
0
 def test_get_definition_location_for_builtins(self):
     code = 'import sys\n'
     result = get_definition_location(self.project, code, len(code) - 2)
     self.assertEquals((None, None), result)
Exemplo n.º 36
0
 def test_get_definition_location_for_builtins(self):
     code = 'import sys\n'
     result = get_definition_location(self.project, code,
                                      len(code) - 2)
     self.assertEquals((None, None), result)
Exemplo n.º 37
0
 def test_if_scopes_in_other_scopes_for_get_definition_location(self):
     code = 'def f(a_var):\n    pass\na_var = 10\nif True:\n    print a_var\n'
     result = get_definition_location(self.project, code, len(code) - 3)
     self.assertEquals((None, 3), result)
Exemplo n.º 38
0
 def test_get_definition_location_dot_line_break_inside_parens(self):
     code = 'class A(object):\n    def a_method(self):\n        pass\n' + \
            '(A.\na_method)'
     result = get_definition_location(self.project, code,
                                      code.rindex('a_method') + 1)
     self.assertEquals((None, 2), result)
Exemplo n.º 39
0
 def test_get_definition_location_unknown(self):
     code = 'a_func()\n'
     result = get_definition_location(self.project, code, len(code) - 3)
     self.assertEquals((None, None), result)
Exemplo n.º 40
0
 def test_get_definition_location_dotted_module_names(self):
     module_resource = testutils.create_module(self.project, 'mod')
     module_resource.write('def a_func():\n    pass\n')
     code = 'import mod\nmod.a_func()'
     result = get_definition_location(self.project, code, len(code) - 3)
     self.assertEquals((module_resource, 1), result)
Exemplo n.º 41
0
 def test_get_definition_location_dot_spaces(self):
     code = 'class AClass(object):\n    ' \
            '@staticmethod\n    def a_method():\n' \
            '        pass\nAClass.\\\n     a_method()'
     result = get_definition_location(self.project, code, len(code) - 3)
     self.assertEqual((None, 2), result)
Exemplo n.º 42
0
 def test_get_definition_location(self):
     code = 'def a_func():\n    pass\na_func()'
     result = get_definition_location(self.project, code, len(code) - 3)
     self.assertEquals((None, 1), result)
Exemplo n.º 43
0
 def test_get_definition_location(self):
     code = 'def a_func():\n    pass\na_func()'
     result = get_definition_location(self.project, code, len(code) - 3)
     self.assertEquals((None, 1), result)