RefactoringRope/Refactoring.py

changeset 5
49a4abfc1f89
parent 4
2e2463ef1aae
child 6
07dabc6bb157
--- a/RefactoringRope/Refactoring.py	Sat Jan 29 19:16:29 2011 +0100
+++ b/RefactoringRope/Refactoring.py	Sat Jan 29 19:40:01 2011 +0100
@@ -17,7 +17,7 @@
 import rope.base.exceptions
 
 import rope.refactor.rename
-##import rope.refactor.extract
+import rope.refactor.extract
 ##import rope.refactor.usefunction
 ##import rope.refactor.inline
 ##import rope.refactor.move
@@ -50,6 +50,7 @@
 from RenameDialog import RenameDialog
 from ChangeOccurrencesDialog import ChangeOccurrencesDialog
 from HistoryDialog import HistoryDialog
+from ExtractDialog import ExtractDialog
 
 import Utilities
 
@@ -169,6 +170,48 @@
         self.actions.append(self.refactoringChangeOccurrencesAct)
         
         #####################################################
+        ## Extract refactoring actions
+        #####################################################
+        
+        self.refactoringExtractMethodAct = E5Action(
+                self.trUtf8('Extract method'),
+                self.trUtf8('Extract &Method'),
+                0, 0,
+                self,'refactoring_extract_method')
+        self.refactoringExtractMethodAct.setStatusTip(self.trUtf8(
+            'Extract the highlighted area as a method'))
+        self.refactoringExtractMethodAct.setWhatsThis(self.trUtf8(
+            """<b>Extract method</b>"""
+            """<p>Extract the highlighted area as a method or function.</p>"""
+        ))
+        if self.__newStyle:
+            self.refactoringExtractMethodAct.triggered[()].connect(
+                self.__extractMethod)
+        else:
+            self.connect(self.refactoringExtractMethodAct,
+                SIGNAL('triggered()'), self.__extractMethod)
+        self.actions.append(self.refactoringExtractMethodAct)
+        
+        self.refactoringExtractLocalVariableAct = E5Action(
+                self.trUtf8('Extract local variable'),
+                self.trUtf8('&Extract Local Variable'),
+                0, 0,
+                self,'refactoring_extract_variable')
+        self.refactoringExtractLocalVariableAct.setStatusTip(self.trUtf8(
+            'Extract the highlighted area as a local variable'))
+        self.refactoringExtractLocalVariableAct.setWhatsThis(self.trUtf8(
+            """<b>Extract local variable</b>"""
+            """<p>Extract the highlighted area as a local variable.</p>"""
+        ))
+        if self.__newStyle:
+            self.refactoringExtractLocalVariableAct.triggered[()].connect(
+                self.__extractLocalVariable)
+        else:
+            self.connect(self.refactoringExtractLocalVariableAct,
+                SIGNAL('triggered()'), self.__extractLocalVariable)
+        self.actions.append(self.refactoringExtractLocalVariableAct)
+        
+        #####################################################
         ## Undo/Redo actions
         #####################################################
         
@@ -441,6 +484,13 @@
         smenu.addAction(self.refactoringRenameLocalAct)
         smenu.addAction(self.refactoringChangeOccurrencesAct)
         smenu.addSeparator()
+        smenu.addAction(self.refactoringExtractMethodAct)
+##        smenu.addAction(self.refactoringMoveMethodAct)
+        smenu.addSeparator()
+##        smenu.addAction(self.refactoringInlineAct)
+##        smenu.addSeparator()
+        smenu.addAction(self.refactoringExtractLocalVariableAct)
+        smenu.addSeparator()
         smenu.addAction(self.refactoringRenameModuleAct)
         smenu.addSeparator()
         
@@ -721,6 +771,68 @@
         self.dlg.show()
     
     #####################################################
+    ## Extract refactorings
+    #####################################################
+    
+    def __extractMethod(self):
+        """
+        Private slot to handle the Extract Method action.
+        """
+        self.__doExtract(self.trUtf8("Extract Method"), "method")
+    
+    def __extractLocalVariable(self):
+        """
+        Private slot to handle the Extract Local Variable action.
+        """
+        self.__doExtract(self.trUtf8("Extract Local Variable"), "variable")
+    
+    def __doExtract(self, title, kind):
+        """
+        Private method to perform the extract refactoring.
+        
+        @param title title of the refactoring (QString)
+        @param kind kind of extraction to be done (string,
+            "method" or "variable")
+        """
+        aw = e5App().getObject("ViewManager").activeWindow()
+        
+        if aw is None:
+            return
+        
+        if not aw.hasSelectedText():
+            # no selection available
+            E5MessageBox.warning(self.__ui, title,
+                self.trUtf8("Highlight the region of code you want to extract"
+                    " and try again."))
+            return 
+        
+        if not self.confirmBufferIsSaved(aw):
+            return
+        
+        filename = aw.getFileName()
+        startline, startcolumn, endline, endcolumn = aw.getSelection()
+        startOffset = aw.positionFromLineIndex(startline, startcolumn)
+        endOffset = aw.positionFromLineIndex(endline, endcolumn)
+        
+        resource = rope.base.libutils.path_to_resource(
+            self.__project, filename)
+        try:
+            if kind == "variable":
+                extractor = rope.refactor.extract.ExtractVariable(
+                    self.__project, resource, startOffset, endOffset)
+            elif kind == "method":
+                extractor = rope.refactor.extract.ExtractMethod(
+                    self.__project, resource, startOffset, endOffset)
+            else:
+                raise Exception("Invalid extraction kind <{0}>.".format(kind))
+        except Exception as err:
+            self.handleRopeError(err, title)
+            return 
+        
+        self.dlg = ExtractDialog(self, title, extractor, parent = self.__ui)
+        self.dlg.show()
+    
+    #####################################################
     ## Undo/Redo refactorings
     #####################################################
     

eric ide

mercurial