Ejemplo n.º 1
0
 def findDefinition(self):
     saved = self.editorTabWidget.saveProject()
     if saved:
         offset = self.getOffset()
         path = self.editorTabWidget.getEditorData("filePath")
         project = self.getProject()
         resource = project.get_file(path)
         try:
             result = find_definition(project,
                                      self.editorTabWidget.getSource(), offset, resource)
             if result is None:
                 self.editorTabWidget.showNotification(
                     "No definition found.")
             else:
                 start, end = result.region
                 offset = result.offset
                 line = result.lineno
                 result_path = result.resource.path
                 sourcePath = self.editorTabWidget.pathDict["sourcedir"]
                 if not os.path.isabs(result_path):
                     result_path = os.path.join(sourcePath, result_path)
                 if os.path.samefile(result_path, path):
                     pass
                 else:
                     self.editorTabWidget.loadfile(result_path)
                 editor = self.editorTabWidget.focusedEditor()
                 start = editor.lineIndexFromPosition(start)
                 end = editor.lineIndexFromPosition(end)
                 editor.setSelection(start[0], start[1], end[0], end[1])
                 editor.ensureLineVisible(line - 1)
         except Exception as err:
             self.editorTabWidget.showNotification(str(err))
Ejemplo n.º 2
0
 def test_find_definition_in_other_modules(self):
     mod1 = testutils.create_module(self.project, 'mod1')
     mod1.write('var = 1\n')
     code = 'import mod1\nprint(mod1.var)\n'
     result = find_definition(self.project, code, code.index('var'))
     self.assertEquals(mod1, result.resource)
     self.assertEquals(0, result.offset)
Ejemplo n.º 3
0
    def run(self, buffer, offset):
        mp = ModuleParser(self.document.filename,
                          project=self.document.project)
        buffer = buffer + ('\n' * 20)

        from rope.contrib.findit  import find_definition
        from rope.base.exceptions import RopeError

        try:
            dl = find_definition(mp.project, buffer, offset, maxfixes=MAX_FIXES)
        except RopeError:
            return

        if not dl:
            return

        if dl.resource is not None:
            file_name = dl.resource.path
        else:
            file_name = self.document.filename


        rv = Definition(file_name=file_name, offset=dl.offset,
                        line=dl.lineno, length=(dl.region[1]-dl.region[0]))

        yield rv
Ejemplo n.º 4
0
 def test_trivial_find_definition(self):
     code = 'def a_func():\n    pass\na_func()'
     result = find_definition(self.project, code, code.rindex('a_func'))
     start = code.index('a_func')
     self.assertEquals(start, result.offset)
     self.assertEquals(None, result.resource)
     self.assertEquals(1, result.lineno)
     self.assertEquals((start, start + len('a_func')), result.region)
Ejemplo n.º 5
0
def getDefinitionLocation( fileName, editor ):
    " Provides the definition location or None "
    try:
        GlobalData().validateRopeProject()
        ropeProject = GlobalData().getRopeProject( fileName )
        position = editor.currentPosition()
        text = editor.text()

        resource = None
        if os.path.isabs( fileName ):
            resource = path_to_resource( ropeProject, fileName )

        return find_definition( ropeProject, text, position,
                                resource, maxfixes = 7 )
    except:
        return None