RefactoringRope/Refactoring.py

changeset 10
0fdfae822ca7
parent 8
fc14525f0501
child 11
562f9758d2e1
--- a/RefactoringRope/Refactoring.py	Sun Jan 30 14:49:03 2011 +0100
+++ b/RefactoringRope/Refactoring.py	Sun Jan 30 14:49:37 2011 +0100
@@ -22,8 +22,8 @@
 import rope.refactor.inline
 import rope.refactor.move
 ##import rope.refactor.change_signature
-##import rope.refactor.introduce_factory
-##import rope.refactor.introduce_parameter
+import rope.refactor.introduce_factory
+import rope.refactor.introduce_parameter
 ##import rope.refactor.method_object
 ##import rope.refactor.encapsulate_field
 ##import rope.refactor.localtofield
@@ -55,6 +55,8 @@
 from MoveMethodDialog import MoveMethodDialog
 from MoveModuleDialog import MoveModuleDialog
 from UseFunctionDialog import UseFunctionDialog
+from IntroduceFactoryDialog import IntroduceFactoryDialog
+from IntroduceParameterDialog import IntroduceParameterDialog
 
 import Utilities
 
@@ -264,6 +266,40 @@
         self.actions.append(self.refactoringUseFunctionAct)
         
         #####################################################
+        ## Introduce refactorings actions
+        #####################################################
+        
+        self.refactoringIntroduceFactoryAct = E5Action(
+                self.trUtf8('Introduce Factory Method'),
+                self.trUtf8('Introduce &Factory Method'),
+                0, 0,
+                self,'refactoring_introduce_factory_method')
+        self.refactoringIntroduceFactoryAct.setStatusTip(self.trUtf8(
+            'Introduce a factory method or function'))
+        self.refactoringIntroduceFactoryAct.setWhatsThis(self.trUtf8(
+            """<b>Introduce Factory Method</b>"""
+            """<p>Introduce a factory method or function.</p>"""
+        ))
+        self.refactoringIntroduceFactoryAct.triggered[()].connect(
+            self.__introduceFactoryMethod)
+        self.actions.append(self.refactoringIntroduceFactoryAct)
+        
+        self.refactoringIntroduceParameterAct = E5Action(
+                self.trUtf8('Introduce Parameter'),
+                self.trUtf8('Introduce &Parameter'),
+                0, 0,
+                self,'refactoring_introduce_parameter_method')
+        self.refactoringIntroduceParameterAct.setStatusTip(self.trUtf8(
+            'Introduce a parameter in a function'))
+        self.refactoringIntroduceParameterAct.setWhatsThis(self.trUtf8(
+            """<b>Introduce Parameter</b>"""
+            """<p>Introduce a parameter in a function.</p>"""
+        ))
+        self.refactoringIntroduceParameterAct.triggered[()].connect(
+            self.__introduceParameter)
+        self.actions.append(self.refactoringIntroduceParameterAct)
+        
+        #####################################################
         ## Undo/Redo actions
         #####################################################
         
@@ -493,8 +529,21 @@
         smenu.addSeparator()
         smenu.addAction(self.refactoringUseFunctionAct)
         smenu.addSeparator()
+##        smenu.addAction(self.refactoringChangeSignatureAct)
+##        smenu.addAction(self.refactoringInlineArgumentDefaultAct)
+        smenu.addSeparator()
+##        smenu.addAction(self.refactoringRestructureAct)
+        smenu.addSeparator()
+        smenu.addAction(self.refactoringIntroduceFactoryAct)
+        smenu.addAction(self.refactoringIntroduceParameterAct)
+##        smenu.addAction(self.refactoringMethodToMethodObjectAct)
+        smenu.addSeparator()
+##        smenu.addAction(self.refactoringEncapsulateAttributeAct)
+##        smenu.addAction(self.refactoringLocalVariableToAttributeAct)
+        smenu.addSeparator()
         smenu.addAction(self.refactoringRenameModuleAct)
         smenu.addAction(self.refactoringMoveModuleAct)
+##        smenu.addAction(self.refactoringTransformModuleAct)
         smenu.addSeparator()
         
         smenu.addSeparator()
@@ -793,7 +842,7 @@
         """
         Private method to perform the extract refactoring.
         
-        @param title title of the refactoring (QString)
+        @param title title of the refactoring (string)
         @param kind kind of extraction to be done (string,
             "method" or "variable")
         """
@@ -984,6 +1033,85 @@
         self.dlg.show()
     
     #####################################################
+    ## Introduce refactorings
+    #####################################################
+    
+    def __introduceFactoryMethod(self):
+        """
+        Private slot to introduce a factory method or global function.
+        """
+        aw = e5App().getObject("ViewManager").activeWindow()
+        
+        if aw is None:
+            return
+        
+        title = self.trUtf8("Introduce Factory Method")
+        if not aw.hasSelectedText():
+            # no selection available
+            E5MessageBox.warning(self.__ui, title,
+                self.trUtf8("Highlight the class to introduce a factory"
+                    " method for and try again."))
+            return 
+        
+        if not self.confirmAllBuffersSaved():
+            return
+        
+        filename = aw.getFileName()
+        line, index, line1, index1 = aw.getSelection()
+        offset = aw.positionFromLineIndex(line, index)
+        
+        resource = rope.base.libutils.path_to_resource(
+            self.__project, filename)
+        try:
+            introducer = \
+                rope.refactor.introduce_factory.IntroduceFactoryRefactoring(
+                self.__project, resource, offset)
+        except Exception as err:
+            self.handleRopeError(err, title)
+            return 
+        
+        self.dlg = IntroduceFactoryDialog(self, title, introducer, 
+                                          parent=self.__ui)
+        self.dlg.show()
+    
+    def __introduceParameter(self):
+        """
+        Private slot to introduce a parameter in a function.
+        """
+        aw = e5App().getObject("ViewManager").activeWindow()
+        
+        if aw is None:
+            return
+        
+        title = self.trUtf8("Introduce Parameter")
+        if not aw.hasSelectedText():
+            # no selection available
+            E5MessageBox.warning(self.__ui, title,
+                self.trUtf8("Highlight the code for the new parameter"
+                    " and try again."))
+            return 
+        
+        if not self.confirmAllBuffersSaved():
+            return
+        
+        filename = aw.getFileName()
+        line, index, line1, index1 = aw.getSelection()
+        offset = aw.positionFromLineIndex(line, index)
+        
+        resource = rope.base.libutils.path_to_resource(
+            self.__project, filename)
+        try:
+            introducer = rope.refactor.introduce_parameter.IntroduceParameter(
+            self.__project, resource, offset)
+        except Exception as err:
+            self.handleRopeError(err, title)
+            return 
+        
+        self.dlg = IntroduceParameterDialog(self, title, introducer, 
+                                            parent=self.__ui)
+        self.dlg.show()
+    
+    #####################################################
     ## Undo/Redo refactorings
     #####################################################
     

eric ide

mercurial