Anonymous avatar Anonymous committed fcb0751

codeassist: get_calltip() includes module name

Comments (0)

Files changed (2)

rope/contrib/codeassist.py

 from rope.base import pyobjects, pynames, taskhandle, builtins, exceptions
 from rope.base.codeanalyze import (ArrayLinesAdapter, LogicalLineFinder,
                                    SourceLinesAdapter, WordRangeFinder)
-from rope.refactor import occurrences, functionutils
+from rope.refactor import occurrences, functionutils, importutils
 
 
 def code_assist(project, source_code, offset, resource=None,
         except exceptions.AttributeNotFoundError:
             return None
         if isinstance(pyobject, pyobjects.AbstractFunction):
-            return self._get_function_signature(pyobject)
+            return self._get_function_signature(pyobject, add_module=True)
 
     def _get_class_docstring(self, pyclass):
         contents = self._trim_docstring(pyclass.get_doc(), 2)
             result.extend(self._get_super_methods(super_class, name))
         return result
 
-    def _get_function_signature(self, pyfunction):
+    def _get_function_signature(self, pyfunction, add_module=False):
         if isinstance(pyfunction, pyobjects.PyFunction):
-            if self._is_method(pyfunction):
-                prefix = pyfunction.parent.get_name() + '.'
-            else:
-                prefix = ''
+            prefixes = []
+            module = pyfunction.get_module()
+            if add_module:
+                prefixes.append(self._get_module(pyfunction))
+            parent = pyfunction.parent
+            while not isinstance(parent, pyobjects.AbstractModule):
+                prefixes.append(parent.get_name())
+                prefixes.append('.')
+                parent = parent.parent
             info = functionutils.DefinitionInfo.read(pyfunction)
-            return prefix + info.to_string()
+            return ''.join(prefixes) + info.to_string()
         else:
             return '%s(%s)' % (pyfunction.get_name(),
                                ', '.join(pyfunction.get_param_names()))
 
+    def _get_module(self, pyfunction):
+        module = pyfunction.get_module()
+        if module is not None:
+            resource = module.get_resource()
+            if resource is not None:
+                return importutils.get_module_name(
+                    pyfunction.pycore, resource) + '.'
+        return ''
+
     def _trim_docstring(self, docstring, indents=0):
         """The sample code from :PEP:`257`"""
         if not docstring:

ropetest/contrib/codeassisttest.py

         doc = get_calltip(self.project, src, src.rindex('c'))
         self.assertEquals('C.__call__(self, p)', doc)
 
+    def test_get_calltips_and_including_module_name(self):
+        src = 'class C(object):\n' \
+              '    def __call__(self, p):\n        pass\n' \
+              'c = C()\n'
+        mod = testutils.create_module(self.project, 'mod')
+        mod.write(src)
+        doc = get_calltip(self.project, src, src.rindex('c'), mod)
+        self.assertEquals('mod.C.__call__(self, p)', doc)
+
 
 class CodeAssistInProjectsTest(unittest.TestCase):
 
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.