RefactoringRope/Refactoring.py

changeset 8
fc14525f0501
parent 7
5ecf9ef23d39
child 10
0fdfae822ca7
--- a/RefactoringRope/Refactoring.py	Sun Jan 30 14:02:31 2011 +0100
+++ b/RefactoringRope/Refactoring.py	Sun Jan 30 14:25:34 2011 +0100
@@ -18,7 +18,7 @@
 
 import rope.refactor.rename
 import rope.refactor.extract
-##import rope.refactor.usefunction
+import rope.refactor.usefunction
 import rope.refactor.inline
 import rope.refactor.move
 ##import rope.refactor.change_signature
@@ -54,6 +54,7 @@
 from InlineDialog import InlineDialog
 from MoveMethodDialog import MoveMethodDialog
 from MoveModuleDialog import MoveModuleDialog
+from UseFunctionDialog import UseFunctionDialog
 
 import Utilities
 
@@ -107,12 +108,8 @@
             """<b>Rename</b>"""
             """<p>Rename the highlighted Python object.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringRenameAct.triggered[()].connect(
-                self.__rename)
-        else:
-            self.connect(self.refactoringRenameAct, SIGNAL('triggered()'), 
-                self.__rename)
+        self.refactoringRenameAct.triggered[()].connect(
+            self.__rename)
         self.actions.append(self.refactoringRenameAct)
         
         self.refactoringRenameLocalAct = E5Action(self.trUtf8('Local Rename'),
@@ -126,12 +123,8 @@
             """<p>Rename the highlighted Python object in the current"""
             """ module only.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringRenameLocalAct.triggered[()].connect(
-                self.__renameLocal)
-        else:
-            self.connect(self.refactoringRenameLocalAct, 
-                SIGNAL('triggered()'), self.__renameLocal)
+        self.refactoringRenameLocalAct.triggered[()].connect(
+            self.__renameLocal)
         self.actions.append(self.refactoringRenameLocalAct)
         
         self.refactoringRenameModuleAct = E5Action(
@@ -145,12 +138,8 @@
             """<b>Rename Current Module</b>"""
             """<p>Rename the current module.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringRenameModuleAct.triggered[()].connect(
-                self.__renameModule)
-        else:
-            self.connect(self.refactoringRenameModuleAct, 
-                SIGNAL('triggered()'), self.__renameModule)
+        self.refactoringRenameModuleAct.triggered[()].connect(
+            self.__renameModule)
         self.actions.append(self.refactoringRenameModuleAct)
         
         self.refactoringChangeOccurrencesAct = E5Action(
@@ -164,12 +153,8 @@
             """<b>Change Occurrences</b>"""
             """<p>Change all occurrences in the local scope.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringChangeOccurrencesAct.triggered[()].connect(
-                self.__changeOccurrences)
-        else:
-            self.connect(self.refactoringChangeOccurrencesAct, 
-                SIGNAL('triggered()'), self.__changeOccurrences)
+        self.refactoringChangeOccurrencesAct.triggered[()].connect(
+            self.__changeOccurrences)
         self.actions.append(self.refactoringChangeOccurrencesAct)
         
         #####################################################
@@ -187,12 +172,8 @@
             """<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.refactoringExtractMethodAct.triggered[()].connect(
+            self.__extractMethod)
         self.actions.append(self.refactoringExtractMethodAct)
         
         self.refactoringExtractLocalVariableAct = E5Action(
@@ -206,12 +187,8 @@
             """<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.refactoringExtractLocalVariableAct.triggered[()].connect(
+            self.__extractLocalVariable)
         self.actions.append(self.refactoringExtractLocalVariableAct)
         
         #####################################################
@@ -229,12 +206,8 @@
             """<b>Inline</b>"""
             """<p>Inlines the selected local variable or method.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringInlineAct.triggered[()].connect(
-                self.__inline)
-        else:
-            self.connect(self.refactoringInlineAct,
-                SIGNAL('triggered()'), self.__inline)
+        self.refactoringInlineAct.triggered[()].connect(
+            self.__inline)
         self.actions.append(self.refactoringInlineAct)
         
         #####################################################
@@ -252,12 +225,8 @@
             """<b>Move method</b>"""
             """<p>Move the highlighted method to another class.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringMoveMethodAct.triggered[()].connect(
-                self.__moveMethod)
-        else:
-            self.connect(self.refactoringMoveMethodAct,
-                SIGNAL('triggered()'), self.__moveMethod)
+        self.refactoringMoveMethodAct.triggered[()].connect(
+            self.__moveMethod)
         self.actions.append(self.refactoringMoveMethodAct)
         
         self.refactoringMoveModuleAct = E5Action(
@@ -271,15 +240,30 @@
             """<b>Move current module</b>"""
             """<p>Move the current module to another package.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringMoveModuleAct.triggered[()].connect(
-                self.__moveModule)
-        else:
-            self.connect(self.refactoringMoveModuleAct,
-                SIGNAL('triggered()'), self.__moveModule)
+        self.refactoringMoveModuleAct.triggered[()].connect(
+            self.__moveModule)
         self.actions.append(self.refactoringMoveModuleAct)
         
         #####################################################
+        ## Use function refactoring action
+        #####################################################
+        
+        self.refactoringUseFunctionAct = E5Action(
+                self.trUtf8('Use Function'),
+                self.trUtf8('Use Function'),
+                0, 0,
+                self,'refactoring_use_function')
+        self.refactoringUseFunctionAct.setStatusTip(self.trUtf8(
+            'Use a function wherever possible.'))
+        self.refactoringUseFunctionAct.setWhatsThis(self.trUtf8(
+            """<b>Use function</b>"""
+            """<p>Tries to use a function wherever possible.</p>"""
+        ))
+        self.refactoringUseFunctionAct.triggered[()].connect(
+            self.__useFunction)
+        self.actions.append(self.refactoringUseFunctionAct)
+        
+        #####################################################
         ## Undo/Redo actions
         #####################################################
         
@@ -293,12 +277,8 @@
             """<b>Undo</b>"""
             """<p>Undo the last refactoring.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringUndoAct.triggered[()].connect(
-                self.__undo)
-        else:
-            self.connect(self.refactoringUndoAct, 
-                SIGNAL('triggered()'), self.__undo)
+        self.refactoringUndoAct.triggered[()].connect(
+            self.__undo)
         self.actions.append(self.refactoringUndoAct)
         
         self.refactoringRedoAct = E5Action(self.trUtf8('Redo'),
@@ -311,12 +291,8 @@
             """<b>Redo</b>"""
             """<p>Redo the last refactoring.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringRedoAct.triggered[()].connect(
-                self.__redo)
-        else:
-            self.connect(self.refactoringRedoAct, 
-                SIGNAL('triggered()'), self.__redo)
+        self.refactoringRedoAct.triggered[()].connect(
+            self.__redo)
         self.actions.append(self.refactoringRedoAct)
         
         self.refactoringUndoHistoryAct = \
@@ -331,12 +307,8 @@
             """<p>Opens a dialog to show the undo history list of"""
             """ the project.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringUndoHistoryAct.triggered[()].connect(
-                self.__showProjectUndoHistory)
-        else:
-            self.connect(self.refactoringUndoHistoryAct,
-                SIGNAL('triggered()'), self.__showProjectUndoHistory)
+        self.refactoringUndoHistoryAct.triggered[()].connect(
+            self.__showProjectUndoHistory)
         self.actions.append(self.refactoringUndoHistoryAct)
         
         self.refactoringRedoHistoryAct = \
@@ -351,12 +323,8 @@
             """<p>Opens a dialog to show the redo history list of"""
             """ the project.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringRedoHistoryAct.triggered[()].connect(
-                self.__showProjectRedoHistory)
-        else:
-            self.connect(self.refactoringRedoHistoryAct,
-                SIGNAL('triggered()'), self.__showProjectRedoHistory)
+        self.refactoringRedoHistoryAct.triggered[()].connect(
+            self.__showProjectRedoHistory)
         self.actions.append(self.refactoringRedoHistoryAct)
         
         self.refactoringUndoFileHistoryAct = \
@@ -371,12 +339,8 @@
             """<p>Opens a dialog to show the undo history list of"""
             """ the current file.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringUndoFileHistoryAct.triggered[()].connect(
-                self.__showFileUndoHistory)
-        else:
-            self.connect(self.refactoringUndoFileHistoryAct,
-                SIGNAL('triggered()'), self.__showFileUndoHistory)
+        self.refactoringUndoFileHistoryAct.triggered[()].connect(
+            self.__showFileUndoHistory)
         self.actions.append(self.refactoringUndoFileHistoryAct)
         
         self.refactoringRedoFileHistoryAct = \
@@ -391,12 +355,8 @@
             """<p>Opens a dialog to show the redo history list of"""
             """ the current file.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringRedoFileHistoryAct.triggered[()].connect(
-                self.__showFileRedoHistory)
-        else:
-            self.connect(self.refactoringRedoFileHistoryAct,
-                SIGNAL('triggered()'), self.__showFileRedoHistory)
+        self.refactoringRedoFileHistoryAct.triggered[()].connect(
+            self.__showFileRedoHistory)
         self.actions.append(self.refactoringRedoFileHistoryAct)
         
         self.refactoringClearHistoryAct = \
@@ -410,12 +370,8 @@
             """<b>Clear History</b>"""
             """<p>Clears the refactoring history.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringClearHistoryAct.triggered[()].connect(
-                self.__clearHistory)
-        else:
-            self.connect(self.refactoringClearHistoryAct,
-                SIGNAL('triggered()'), self.__clearHistory)
+        self.refactoringClearHistoryAct.triggered[()].connect(
+            self.__clearHistory)
         self.actions.append(self.refactoringClearHistoryAct)
         
         #####################################################
@@ -433,12 +389,8 @@
             """<p>Find occurrences of the highlighted class, method,"""
             """ function or variable.</p>"""
         ))
-        if self.__newStyle:
-            self.queryReferencesAct.triggered[()].connect(
-                self.__queryReferences)
-        else:
-            self.connect(self.queryReferencesAct, SIGNAL('triggered()'), 
-                self.__queryReferences)
+        self.queryReferencesAct.triggered[()].connect(
+            self.__queryReferences)
         self.actions.append(self.queryReferencesAct)
         
         self.queryDefinitionAct = E5Action(self.trUtf8('Find definition'),
@@ -452,12 +404,8 @@
             """<p>Find the definition of the highlighted class, method,"""
             """ function or variable.</p>"""
         ))
-        if self.__newStyle:
-            self.queryDefinitionAct.triggered[()].connect(
-                self.__queryDefinition)
-        else:
-            self.connect(self.queryDefinitionAct, SIGNAL('triggered()'), 
-                self.__queryDefinition)
+        self.queryDefinitionAct.triggered[()].connect(
+            self.__queryDefinition)
         self.actions.append(self.queryDefinitionAct)
         
         self.queryImplementationsAct = E5Action(
@@ -471,12 +419,8 @@
             """<b>Find implementations</b>"""
             """<p>Find places where the selected method is overridden.</p>"""
         ))
-        if self.__newStyle:
-            self.queryImplementationsAct.triggered[()].connect(
-                self.__queryImplementations)
-        else:
-            self.connect(self.queryImplementationsAct, SIGNAL('triggered()'), 
-                self.__queryImplementations)
+        self.queryImplementationsAct.triggered[()].connect(
+            self.__queryImplementations)
         self.actions.append(self.queryImplementationsAct)
         
         #####################################################
@@ -493,12 +437,8 @@
             """<b>Configure Rope</b>"""
             """<p>Opens the rope configuration file in an editor.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringEditConfigAct.triggered[()].connect(
-                self.__editConfig)
-        else:
-            self.connect(self.refactoringEditConfigAct, SIGNAL('triggered()'), 
-                self.__editConfig)
+        self.refactoringEditConfigAct.triggered[()].connect(
+            self.__editConfig)
         self.actions.append(self.refactoringEditConfigAct)
         
         self.refactoringHelpAct = E5Action(self.trUtf8('Rope help'),
@@ -511,12 +451,8 @@
             """<b>Rope help</b>"""
             """<p>Show some help text about the rope refactorings.</p>"""
         ))
-        if self.__newStyle:
-            self.refactoringHelpAct.triggered[()].connect(
-                self.__showRopeHelp)
-        else:
-            self.connect(self.refactoringHelpAct, SIGNAL('triggered()'), 
-                self.__showRopeHelp)
+        self.refactoringHelpAct.triggered[()].connect(
+            self.__showRopeHelp)
         self.actions.append(self.refactoringHelpAct)
         
         for act in self.actions:
@@ -543,11 +479,7 @@
         smenu.addAction(self.queryImplementationsAct)
         
         smenu = menu.addMenu(self.trUtf8("&Refactoring"))
-        if self.__newStyle:
-            smenu.aboutToShow.connect(self.__showRefactoringMenu)
-        else:
-            self.connect(smenu, SIGNAL("aboutToShow()"), 
-                         self.__showRefactoringMenu)
+        smenu.aboutToShow.connect(self.__showRefactoringMenu)
         smenu.addAction(self.refactoringRenameAct)
         smenu.addAction(self.refactoringRenameLocalAct)
         smenu.addAction(self.refactoringChangeOccurrencesAct)
@@ -559,6 +491,8 @@
         smenu.addSeparator()
         smenu.addAction(self.refactoringExtractLocalVariableAct)
         smenu.addSeparator()
+        smenu.addAction(self.refactoringUseFunctionAct)
+        smenu.addSeparator()
         smenu.addAction(self.refactoringRenameModuleAct)
         smenu.addAction(self.refactoringMoveModuleAct)
         smenu.addSeparator()
@@ -1011,6 +945,45 @@
         self.dlg.show()
     
     #####################################################
+    ## Use function refactoring
+    #####################################################
+    
+    def __useFunction(self):
+        """
+        Private slot to use a function wherever possible.
+        """
+        aw = e5App().getObject("ViewManager").activeWindow()
+        
+        if aw is None:
+            return
+        
+        title = self.trUtf8("Use Function")
+        if not aw.hasSelectedText():
+            # no selection available
+            E5MessageBox.warning(self.__ui, title,
+                self.trUtf8("Highlight a global function 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:
+            user = rope.refactor.usefunction.UseFunction(
+                self.__project, resource, offset)
+        except Exception as err:
+            self.handleRopeError(err, title)
+            return
+        
+        self.dlg = UseFunctionDialog(self, title, user, parent = self.__ui)
+        self.dlg.show()
+    
+    #####################################################
     ## Undo/Redo refactorings
     #####################################################
     

eric ide

mercurial