RefactoringRope/CodeAssistClient.py

changeset 232
65d7d745543d
parent 228
e76a4991faef
child 234
a9d03af34d28
--- a/RefactoringRope/CodeAssistClient.py	Sun Nov 05 15:47:47 2017 +0100
+++ b/RefactoringRope/CodeAssistClient.py	Sun Nov 05 17:23:30 2017 +0100
@@ -277,7 +277,11 @@
         except Exception as err:
             errorDict = self.__handleRopeError(err)
         
-        documentationDict = self.__processDocumentation(cts, documentation)
+        typeName = self.__getObjectTypeAnName(
+                self.__project, source, offset, resource, maxfixes=maxfixes)
+        
+        documentationDict = self.__processDocumentation(cts, documentation,
+                                                        typeName)
         result = {
             "DocumentationDict": documentationDict,
         }
@@ -285,7 +289,7 @@
         
         self.sendJson("DocumentationResult", result)
     
-    def __processDocumentation(self, cts, documentation):
+    def __processDocumentation(self, cts, documentation, typeName):
         """
         Private method to process the call-tips and documentation.
         
@@ -293,6 +297,8 @@
         @type str
         @param documentation extracted source code documentation
         @type str
+        @param typeName type and name of the object
+        @type tuple of (str, str)
         @return dictionary containing document information
         @rtype dictionary with keys "name", "argspec", "module" and
             "docstring"
@@ -312,6 +318,7 @@
                 calltip = cts
             else:
                 objectFullname = cts
+        
         if objectFullname and not objectFullname.startswith("self."):
             if calltip:
                 argspecStart = calltip.find("(")
@@ -319,12 +326,62 @@
             
             moduleEnd = objectFullname.rfind('.')
             module = objectFullname[:moduleEnd]
-
-        if not documentation and not calltip:
-            return None
+        
+        if not objectFullname and typeName[1] not in ["", "<unknown>"]:
+            objectFullname = typeName[1]
 
         return dict(name=objectFullname, argspec=argspec, module=module,
-                    docstring=documentation)
+                    docstring=documentation, typ=typeName[0])
+    
+    def __getObjectTypeAnName(self, project, sourceCode, offset,
+                              resource=None, maxfixes=1):
+        """
+        Private method to determine an object type and name for the given
+        location.
+        
+        @param project reference to the rope project object
+        @type rope.base.project.Project
+        @param sourceCode source code
+        @type str
+        @param offset offset to base the calculation on
+        @type int
+        @param resource reference to the rope resource object
+        @type rope.base.resources.Resource
+        @param maxfixes number of fixes to be done
+        @type int
+        """
+        from rope.base import pyobjects, pyobjectsdef, pynames
+        from rope.contrib import fixsyntax
+        
+        fixer = fixsyntax.FixSyntax(project, sourceCode, resource, maxfixes)
+        pyname = fixer.pyname_at(offset)
+        if pyname is None:
+            return "<unknown>", "<unknown>"
+        
+        pyobject = pyname.get_object()
+        if isinstance(pyobject, pyobjectsdef.PyPackage):
+            typ = "package"
+            if isinstance(pyname, pynames.ImportedModule):
+                name = pyname.module_name
+            else:
+                name = "<unknown>"
+        elif isinstance(pyobject, pyobjectsdef.PyModule):
+            typ = "module"
+            name = pyobject.get_name()
+        elif isinstance(pyobject, pyobjectsdef.PyClass):
+            typ = "class"
+            name = pyobject.get_name()
+        elif isinstance(pyobject, pyobjectsdef.PyFunction):
+            typ = pyobject.get_kind()
+            name = pyobject.get_name()
+        elif isinstance(pyobject, pyobjects.PyObject):
+            typ = "object"
+            name = ""
+        else:
+            typ = ""
+            name = ""
+        
+        return(typ, name)
     
     def __reportChanged(self, params):
         """

eric ide

mercurial