RefactoringRope/CodeAssistClient.py

branch
eric7
changeset 370
9d246420f284
parent 365
f740b50380df
child 374
958f34e97952
--- a/RefactoringRope/CodeAssistClient.py	Sun Jul 25 12:16:21 2021 +0200
+++ b/RefactoringRope/CodeAssistClient.py	Fri Jul 30 16:38:13 2021 +0200
@@ -54,6 +54,7 @@
             "getCallTips": self.__getCallTips,
             "getDocumentation": self.__getDocumentation,
             "gotoDefinition": self.__gotoDefinition,
+            "gotoReferences": self.__getReferences,
             "reportChanged": self.__reportChanged,
         }
         
@@ -397,6 +398,7 @@
         filename = params["FileName"]
         offset = params["Offset"]
         source = params["Source"]
+        uid = params["Uuid"]
         
         self.__project.prefs.set("python_path", params["SysPath"])
         resource = (
@@ -421,10 +423,63 @@
                 "ModulePath": location.resource.real_path,
                 "Line": location.lineno,
             }
+        result["Uuid"] = uid
         result.update(errorDict)
         
         self.sendJson("GotoDefinitionResult", result)
     
+    def __getReferences(self, params):
+        """
+        Private method to get the places a parameter is referenced.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        import rope.base.libutils
+        
+        filename = params["FileName"]
+        offset = params["Offset"]
+        line = params["Line"]
+        uid = params["Uuid"]
+        
+        self.__project.prefs.set("python_path", params["SysPath"])
+        resource = (
+            rope.base.libutils.path_to_resource(self.__project, filename)
+            if filename else
+            None
+        )
+        
+        errorDict = {}
+        gotoReferences = []
+        
+        import rope.contrib.findit
+        try:
+            occurrences = rope.contrib.findit.find_occurrences(
+                self.__project, resource, offset, in_hierarchy=True)
+            for occurrence in occurrences:
+                if (
+                    occurrence.lineno == line and
+                    occurrence.resource.real_path == filename
+                ):
+                    continue
+                gotoReferences.append({
+                    'ModulePath': occurrence.resource.real_path,
+                    'Line': occurrence.lineno,
+                    'Code': occurrence.resource.read().splitlines()[
+                        occurrence.lineno - 1],
+                })
+        except Exception as err:
+            errorDict = self.__handleRopeError(err)
+        
+        result = {
+            "GotoReferencesList": gotoReferences,
+            "Uuid": uid,
+        }
+        result.update(errorDict)
+        
+        self.sendJson("GotoReferencesResult", result)
+    
     def __reportChanged(self, params):
         """
         Private method to register some changed sources.

eric ide

mercurial