RefactoringRope/RefactoringClient.py

branch
server_client_variant
changeset 172
c8890f852917
parent 170
05ef7c12a6d4
child 173
2cdb7c48b719
--- a/RefactoringRope/RefactoringClient.py	Sun Sep 17 20:03:39 2017 +0200
+++ b/RefactoringRope/RefactoringClient.py	Mon Sep 18 20:05:28 2017 +0200
@@ -56,6 +56,10 @@
             "PerformSoa": self.__performSOA,
             "ReportChanged": self.__reportChanged,
             "History": self.__processHistory,
+            "PreviewChanges": self.__previewChanges,
+            "ApplyChanges": self.__applyChanges,
+            "ClearChanges": self.__clearChanges,
+            "CalculateRenameChanges": self.__calculateRenameChanges,
         }
         
         from FileSystemCommands import RefactoringClientFileSystemCommands
@@ -420,6 +424,153 @@
                 del self.__changes["History"]
             except KeyError:
                 pass
+    
+    def __clearChanges(self, params):
+        """
+        Private method to clear the changes cache of a given change group.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        try:
+            del self.__changes[params["ChangeGroup"]]
+        except KeyError:
+            pass
+    
+    def __applyChanges(self, params):
+        """
+        Private method to apply the changes of a given change group.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        errorDict = {}
+        
+        from ProgressHandle import ProgressHandle
+        self.__progressHandle = ProgressHandle(self, params["Title"], False)
+        try:
+            changes = self.__changes[params["ChangeGroup"]]
+            self.___project.do(changes, self.__progressHandle)
+        except Exception as err:
+            errorDict = self.__handleRopeError(err)
+        self.__progressHandle.reset()
+        self.__progressHandle = None
+        
+        result = {
+            "Subcommand": "ChangesApplied",
+            "ChangeGroup": params["ChangeGroup"],
+            "ChangedFiles": [
+                res.real_path for res in changes.get_changed_resources()
+            ],
+        }
+        result.update(errorDict)
+        
+        self.sendJson("Changes", result)
+    
+    def __previewChanges(self, params):
+        """
+        Private method to determine the changes data for a preview.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        changes = self.__changes[params["ChangeGroup"]]
+        
+        changesData = []
+        for change in changes:
+            changeTitle = str(change)
+            try:
+                changeText = change.get_description()
+            except AttributeError:
+                changeText = None
+            changesData.append([changeTitle, changeText])
+        
+        result = {
+            "Subcommand": "PreviewChanges",
+            "ChangeGroup": params["ChangeGroup"],
+            "Description": changes.description,
+            "Changes": changesData,
+        }
+        
+        self.sendJson("Changes", result)
+    
+    def __calculateRenameChanges(self, params):
+        """
+        Private method to calculate the rename changes based on the parameters
+        sent by the server.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        title = params["Title"]
+        filename = params["FileName"]
+        offset = params["Offset"]
+        isLocal = params["LocalRename"]
+        newName = params["NewName"]
+        renameHierarchy = params["RenameHierarchy"]
+        renameInStrings = params["RenameInStrings"]
+        
+        errorDict = {}
+        changes = []
+        result = {
+            "ChangeGroup": params["ChangeGroup"],
+        }
+        
+        import rope.refactor.rename
+        resource = rope.base.libutils.path_to_resource(
+            self.__project, filename)
+        
+        try:
+            renamer = rope.refactor.rename.Rename(
+                self.__project, resource, offset)
+        except Exception as err:
+            errorDict = self.__handleRopeError(err)
+            result.update(errorDict)
+            self.sendJson("Changes", result)
+            return
+        
+        if isLocal:
+            resources = [resource]
+        else:
+            resources = None
+        
+        from ProgressHandle import ProgressHandle
+        self.__progressHandle = ProgressHandle(self, title, True)
+        try:
+            changes = renamer.get_changes(
+                newName,
+                resources=resources,
+                in_hierarchy=renameHierarchy,
+                unsure=self.__confirmUnsure,
+                docs=renameInStrings,
+                task_handle=self.__progressHandle)
+        except Exception as err:
+            errorDict = self.__handleRopeError(err)
+        self.__progressHandle.reset()
+        self.__progressHandle = None
+        
+        if changes:
+            self.__changes[params["ChangeGroup"]] = changes
+        
+        result["Subcommand"] = "ChangesCalculated"
+        result.update(errorDict)
+        
+        self.sendJson("Changes", result)
+    
+    def __confirmUnsure(self, occurrence):
+        """
+        Private method to confirm unsure occurrences.
+        
+        @parameter occurrence reference to the occurrence object
+        @type rope.refactor.occurrences.Occurrence
+        @return flag indicating an occurrence
+        @rtype bool
+        """
+        # TODO: implement this method; synchronous poll
 
 if __name__ == '__main__':
     if len(sys.argv) != 4:

eric ide

mercurial