Anonymous avatar Anonymous committed 0a8d8b0

codeassist: added in_hierarchy to find_occurrences

Comments (0)

Files changed (2)

rope/contrib/codeassist.py

 
 
 def find_occurrences(project, resource, offset, unsure=False, resources=None,
-                     task_handle=taskhandle.NullTaskHandle()):
+                     in_hierarchy=False, task_handle=taskhandle.NullTaskHandle()):
     """Return a list of `Location`\s
 
     If `unsure` is `True`, possible matches are returned, too.  You
     """
     name = rope.base.codeanalyze.get_name_at(resource, offset)
     this_pymodule = project.pycore.resource_to_pyobject(resource)
-    pyname = rope.base.evaluate.get_pyname_at(this_pymodule, offset)
+    primary, pyname = rope.base.evaluate.get_primary_and_pyname_at(
+        this_pymodule, offset)
     def is_match(occurrence):
         return unsure
     finder = occurrences.create_finder(
-        project.pycore, name, pyname, unsure=is_match)
+        project.pycore, name, pyname, unsure=is_match,
+        in_hierarchy=in_hierarchy, instance=primary)
     if resources is None:
         resources = project.pycore.get_python_files()
     job_set = task_handle.create_jobset('Finding Occurrences',
 def starting_expression(source_code, offset):
     """Return the expression to complete
 
-    For instance completing 
+    For instance completing
 
     """
     word_finder = WordRangeFinder(source_code)
             last_lineno = self._last_non_blank(start - 1)
             last_line = self.lines[last_lineno]
             if last_line.rstrip().endswith(':'):
-                indents = _get_line_indents(last_line) + 4            
+                indents = _get_line_indents(last_line) + 4
         self.lines[start] = ' ' * indents + 'pass'
         for line in range(start + 1, end + 1):
             self.lines[line] = self.lines[start]

ropetest/contrib/codeassisttest.py

         self.assertEquals(1, len(result))
         self.assertEquals((mod1, 0), (result[0].resource, result[0].offset))
 
+    def test_find_occurrences_and_class_hierarchies(self):
+        mod1 = testutils.create_module(self.project, 'mod1')
+        mod1.write('class A(object):\n    def f():\n        pass\n'
+                   'class B(A):\n    def f():\n        pass\n')
+        offset = mod1.read().rindex('f')
+        result1 = find_occurrences(self.project, mod1, offset)
+        result2 = find_occurrences(self.project, mod1,
+                                   offset, in_hierarchy=True)
+        self.assertEquals(1, len(result1))
+        self.assertEquals(2, len(result2))
+
     def test_fixing_errors_with_maxfixes_in_resources(self):
         mod = testutils.create_module(self.project, 'mod')
         code = 'def f():\n    sldj sldj\ndef g():\n    ran'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.