RefactoringRope/CodeAssistClient.py

branch
server_client_variant
changeset 217
874115c79ca7
parent 216
47fb0119f0be
child 228
e76a4991faef
--- a/RefactoringRope/CodeAssistClient.py	Sun Oct 08 19:11:45 2017 +0200
+++ b/RefactoringRope/CodeAssistClient.py	Sat Oct 14 19:31:11 2017 +0200
@@ -73,6 +73,7 @@
             "closeProject": self.__closeProject,
             "getCompletions": self.__getCompletions,
             "getCallTips": self.__getCallTips,
+            "getDocumentation": self.__getDocumentation,
             "reportChanged": self.__reportChanged,
         }
         
@@ -163,6 +164,7 @@
         offset = params["Offset"]
         maxfixes = params["MaxFixes"]
         
+        self.__project.prefs.set("python_path", params["SysPath"])
         if filename:
             resource = rope.base.libutils.path_to_resource(
                 self.__project, filename)
@@ -207,6 +209,7 @@
         offset = params["Offset"]
         maxfixes = params["MaxFixes"]
         
+        self.__project.prefs.set("python_path", params["SysPath"])
         if filename:
             resource = rope.base.libutils.path_to_resource(
                 self.__project, filename)
@@ -232,6 +235,94 @@
         
         self.sendJson("CallTipsResult", result)
     
+    def __getDocumentation(self, params):
+        """
+        Private method to get some source code documentation.
+        
+        @param params dictionary containing the method parameters
+        @type dict
+        """
+        filename = params["FileName"]
+        source = params["Source"]
+        offset = params["Offset"]
+        maxfixes = params["MaxFixes"]
+        
+        self.__project.prefs.set("python_path", params["SysPath"])
+        if filename:
+            resource = rope.base.libutils.path_to_resource(
+                self.__project, filename)
+        else:
+            resource = None
+        
+        errorDict = {}
+        documentation = ""
+        cts = None
+        
+        try:
+            cts = rope.contrib.codeassist.get_calltip(
+                self.__project, source, offset, resource, maxfixes=maxfixes,
+                remove_self=True)
+        except Exception:
+            pass
+        
+        if cts is not None:
+            while '..' in cts:
+                cts = cts.replace('..', '.')
+            if '(.)' in cts:
+                cts = cts.replace('(.)', '(...)')
+        
+        try:
+            documentation = rope.contrib.codeassist.get_doc(
+                self.__project, source, offset, resource, maxfixes=maxfixes)
+        except Exception as err:
+            errorDict = self.__handleRopeError(err)
+        
+        documentationDict = self.__processDocumentation(cts, documentation)
+        result = {
+            "DocumentationDict": documentationDict,
+        }
+        result.update(errorDict)
+        
+        self.sendJson("DocumentationResult", result)
+    
+    def __processDocumentation(self, cts, documentation):
+        """
+        Private method to process the call-tips and documentation.
+        
+        @param cts call-tips
+        @type str
+        @param documentation extracted source code documentation
+        @type str
+        @return dictionary containing document information
+        @rtype dictionary with keys "name", "argspec", "module" and
+            "docstring"
+        """
+        objectFullname = ''
+        calltip = ''
+        argspec = ''
+        module = ''
+
+        if cts:
+            cts = cts.replace('.__init__', '')
+            parenthesisPos = cts.find('(')
+            if parenthesisPos:
+                objectFullname = cts[:parenthesisPos]
+                objectName = objectFullname.split('.')[-1]
+                cts = cts.replace(objectFullname, objectName)
+                calltip = cts
+        if objectFullname and not objectFullname.startswith('self.'):
+            if calltip:
+                argspecStart = calltip.find('(')
+                argspec = calltip[argspecStart:]
+                moduleEnd = objectFullname.rfind('.')
+                module = objectFullname[:moduleEnd]
+
+        if not documentation and not calltip:
+            return None
+
+        return dict(name=objectFullname, argspec=argspec, module=module,
+                    docstring=documentation)
+    
     def __reportChanged(self, params):
         """
         Private method to register some changed sources.

eric ide

mercurial