diff -r 562f9758d2e1 -r 75fff1da56b6 RefactoringRope/Refactoring.py --- a/RefactoringRope/Refactoring.py Sun Jan 30 17:02:15 2011 +0100 +++ b/RefactoringRope/Refactoring.py Sun Jan 30 17:24:58 2011 +0100 @@ -25,9 +25,9 @@ import rope.refactor.introduce_factory import rope.refactor.introduce_parameter ##import rope.refactor.method_object -##import rope.refactor.encapsulate_field -##import rope.refactor.localtofield -##import rope.refactor.topackage +import rope.refactor.encapsulate_field +import rope.refactor.localtofield +import rope.refactor.topackage from rope.refactor.importutils import ImportOrganizer import rope.contrib.findit @@ -61,6 +61,7 @@ from RestructureDialog import RestructureDialog from ChangeSignatureDialog import ChangeSignatureDialog from InlineArgumentDefaultDialog import InlineArgumentDefaultDialog +from GetterSetterDialog import GetterSetterDialog import Utilities @@ -440,6 +441,52 @@ self.__inlineArgumentDefault) self.actions.append(self.refactoringInlineArgumentDefaultAct) + self.refactoringTransformModuleAct = E5Action( + self.trUtf8('Transform Module to Package'), + self.trUtf8('Transform Module to Package'), + 0, 0, + self,'refactoring_transform_module_to_package') + self.refactoringTransformModuleAct.setStatusTip(self.trUtf8( + 'Transform the current module to a package')) + self.refactoringTransformModuleAct.setWhatsThis(self.trUtf8( + """<b>Transform Module to Package</b>""" + """<p>Transform the current module to a package.</p>""" + )) + self.refactoringTransformModuleAct.triggered[()].connect( + self.__transformModuleToPackage) + self.actions.append(self.refactoringTransformModuleAct) + + self.refactoringEncapsulateAttributeAct = E5Action( + self.trUtf8('Encapsulate Attribute'), + self.trUtf8('Encap&sulate Attribute'), + 0, 0, + self,'refactoring_encapsulate_attribute') + self.refactoringEncapsulateAttributeAct.setStatusTip(self.trUtf8( + 'Generate a getter/setter for an attribute')) + self.refactoringEncapsulateAttributeAct.setWhatsThis(self.trUtf8( + """<b>Encapsulate Attribute</b>""" + """<p>Generate a getter/setter for an attribute and changes""" + """ its occurrences to use them.</p>""" + )) + self.refactoringEncapsulateAttributeAct.triggered[()].connect( + self.__encapsulateAttribute) + self.actions.append(self.refactoringEncapsulateAttributeAct) + + self.refactoringLocalVariableToAttributeAct = E5Action( + self.trUtf8('Local Variable to Attribute'), + self.trUtf8('Local Varia&ble to Attribute'), + 0, 0, + self,'refactoring_local_variable_to_attribute') + self.refactoringLocalVariableToAttributeAct.setStatusTip(self.trUtf8( + 'Change a local variable to an attribute')) + self.refactoringLocalVariableToAttributeAct.setWhatsThis(self.trUtf8( + """<b>Local Variable to Attribute</b>""" + """<p>Change a local variable to an attribute.</p>""" + )) + self.refactoringLocalVariableToAttributeAct.triggered[()].connect( + self.__convertLocalToAttribute) + self.actions.append(self.refactoringLocalVariableToAttributeAct) + ##################################################### ## Undo/Redo actions ##################################################### @@ -679,12 +726,12 @@ smenu.addAction(self.refactoringIntroduceParameterAct) ## smenu.addAction(self.refactoringMethodToMethodObjectAct) smenu.addSeparator() -## smenu.addAction(self.refactoringEncapsulateAttributeAct) -## smenu.addAction(self.refactoringLocalVariableToAttributeAct) + smenu.addAction(self.refactoringEncapsulateAttributeAct) + smenu.addAction(self.refactoringLocalVariableToAttributeAct) smenu.addSeparator() smenu.addAction(self.refactoringRenameModuleAct) smenu.addAction(self.refactoringMoveModuleAct) -## smenu.addAction(self.refactoringTransformModuleAct) + smenu.addAction(self.refactoringTransformModuleAct) smenu.addSeparator() imenu = smenu.addMenu(self.trUtf8("Im&ports")) @@ -1423,6 +1470,126 @@ parent=self.__ui) self.dlg.show() + def __transformModuleToPackage(self): + """ + Private slot to transform a module to a package. + """ + aw = e5App().getObject("ViewManager").activeWindow() + + if aw is None: + return + + title = self.trUtf8("Transform Module to Package") + + if not self.confirmAllBuffersSaved(): + return + + filename = aw.getFileName() + + resource = rope.base.libutils.path_to_resource( + self.__project, filename) + try: + changes = rope.refactor.topackage.ModuleToPackage( + self.__project, resource).get_changes() + self.__project.do(changes) + if self.__e5project.isDirty(): + self.__e5project.saveProject() + except Exception as err: + self.handleRopeError(err, title) + return + + def __encapsulateAttribute(self): + """ + Private slot to encapsulate an attribute. + """ + aw = e5App().getObject("ViewManager").activeWindow() + + if aw is None: + return + + title = self.trUtf8("Encapsulate Attribute") + if not aw.hasSelectedText(): + # no selection available + E5MessageBox.warning(self.__ui, title, + self.trUtf8("Highlight the attribute to encapsulate" + " 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: + encapsulateField = \ + rope.refactor.encapsulate_field.EncapsulateField( + self.__project, resource, offset) + except Exception as err: + self.handleRopeError(err, title) + return + + dlg = GetterSetterDialog(encapsulateField.get_field_name()) + if dlg.exec_() == QDialog.Accepted: + getter, setter = dlg.getData() + handle = ProgressHandle(title, False, self.__ui) + handle.show() + QApplication.processEvents() + try: + changes = encapsulateField.get_changes( + getter=getter, setter=setter, task_handle=handle) + handle.reset() + dlg = ConfirmationDialog(changes, self.__ui) + if dlg.exec_() == QDialog.Accepted: + self.__project.do(changes) + self.refreshEditors(changes) + if self.__e5project.isDirty(): + self.__e5project.saveProject() + except Exception as err: + self.handleRopeError(err, title, handle) + + def __convertLocalToAttribute(self): + """ + Private slot to convert a local variable to an attribute. + """ + aw = e5App().getObject("ViewManager").activeWindow() + + if aw is None: + return + + title = self.trUtf8("Local Variable to Attribute") + if not aw.hasSelectedText(): + # no selection available + E5MessageBox.warning(self.__ui, title, + self.trUtf8("Highlight the local variable to make an attribute" + " 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: + changes = rope.refactor.localtofield.LocalToField( + self.__project, resource, offset).get_changes() + dlg = ConfirmationDialog(changes, self.__ui) + if dlg.exec_() == QDialog.Accepted: + self.__project.do(changes) + self.refreshEditors(changes) + if self.__e5project.isDirty(): + self.__e5project.saveProject() + except Exception as err: + self.handleRopeError(err, title) + ##################################################### ## Undo/Redo refactorings #####################################################