Sat, 23 Sep 2017 12:16:59 +0200
Implemented the distributed "Introduce Factory Method" method.
--- a/RefactoringRope/IntroduceFactoryDialog.py Sat Sep 23 12:01:48 2017 +0200 +++ b/RefactoringRope/IntroduceFactoryDialog.py Sat Sep 23 12:16:59 2017 +0200 @@ -20,27 +20,35 @@ """ Class implementing the Introduce Factory dialog. """ - def __init__(self, refactoring, title, introducer, parent=None): + def __init__(self, refactoring, title, filename, offset, parent=None): """ Constructor @param refactoring reference to the main refactoring object - (Refactoring) - @param title title of the dialog (string) - @param introducer reference to the factory introducer object - (rope.refactor.introduce_factory.IntroduceFactoryRefactoring) - @param parent reference to the parent widget (QWidget) + @type Refactoring + @param title title of the dialog + @type str + @param filename file name to be worked on + @type str + @param offset offset within file + @type int or None + @param parent reference to the parent widget + @type QWidget """ RefactoringDialogBase.__init__(self, refactoring, title, parent) self.setupUi(self) - self.__introducer = introducer + self._changeGroupName = "IntroduceFactory" + + self.__filename = filename + self.__offset = offset self.__okButton = self.buttonBox.button(QDialogButtonBox.Ok) self.__okButton.setEnabled(False) self.__previewButton = self.buttonBox.addButton( self.tr("Preview"), QDialogButtonBox.ActionRole) self.__previewButton.setDefault(True) + self.__previewButton.setEnabled(False) self.nameEdit.setText("create") self.nameEdit.selectAll() @@ -55,34 +63,32 @@ @param text text entered into the edit (string) """ - self.__okButton.setEnabled(text != "") + enable = bool(text) + self.__okButton.setEnabled(enable) + self.__previewButton.setEnabled(enable) @pyqtSlot(QAbstractButton) def on_buttonBox_clicked(self, button): """ Private slot to act on the button pressed. - @param button reference to the button pressed (QAbstractButton) + @param button reference to the button pressed + @type QAbstractButton """ if button == self.__previewButton: - self.previewChanges() + self.requestPreview() elif button == self.__okButton: self.applyChanges() - def _calculateChanges(self, handle): + def _calculateChanges(self): """ - Protected method to calculate the changes. - - @param handle reference to the task handle - (rope.base.taskhandle.TaskHandle) - @return reference to the Changes object (rope.base.change.ChangeSet) + Protected method to initiate the calculation of the changes. """ - try: - changes = self.__introducer.get_changes( - self.nameEdit.text(), - global_factory=self.globalButton.isChecked(), - task_handle=handle) - return changes - except Exception as err: - self._refactoring.handleRopeError(err, self._title, handle) - return None + self._refactoring.sendJson("CalculateIntroduceFactoryChanges", { + "ChangeGroup": self._changeGroupName, + "Title": self._title, + "FileName": self.__filename, + "Offset": self.__offset, + "Name": self.nameEdit.text(), + "GlobalFactory": self.globalButton.isChecked(), + })
--- a/RefactoringRope/Refactoring.py Sat Sep 23 12:01:48 2017 +0200 +++ b/RefactoringRope/Refactoring.py Sat Sep 23 12:16:59 2017 +0200 @@ -1170,7 +1170,6 @@ ## Introduce refactorings ##################################################### - # TODO: continue from here def __introduceFactoryMethod(self): """ Private slot to introduce a factory method or global function. @@ -1196,22 +1195,16 @@ line, index, line1, index1 = aw.getSelection() offset = self.__getOffset(aw, line, index) - import rope.refactor.introduce_factory - 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 - from IntroduceFactoryDialog import IntroduceFactoryDialog - self.dlg = IntroduceFactoryDialog(self, title, introducer, + dlg = IntroduceFactoryDialog(self, title, filename, offset, parent=self.__ui) - self.dlg.show() + changeGroup = dlg.getChangeGroupName() + self.__refactoringDialogs[changeGroup] = dlg + dlg.finished.connect( + lambda: self.__refactoringDialogClosed(changeGroup)) + dlg.show() + # TODO: continue from here def __introduceParameter(self): """ Private slot to introduce a parameter in a function.
--- a/RefactoringRope/RefactoringClient.py Sat Sep 23 12:01:48 2017 +0200 +++ b/RefactoringRope/RefactoringClient.py Sat Sep 23 12:16:59 2017 +0200 @@ -70,6 +70,8 @@ "CalculateMoveChanges": self.__calculateMoveChanges, "RequestUseFunction": self.__requestUseFunction, "CalculateUseFunctionChanges": self.__calculateUseFunctionChanges, + "CalculateIntroduceFactoryChanges": + self.__calculateIntroduceFactoryChanges, } from FileSystemCommands import RefactoringClientFileSystemCommands @@ -953,6 +955,53 @@ result.update(errorDict) self.sendJson("Changes", result) + + def __calculateIntroduceFactoryChanges(self, params): + """ + Private method to calculate the 'Introduce Factory' changes based on + the parameters sent by the server. + + @param params dictionary containing the method parameters sent by + the server + @type dict + """ + changeGroup = params["ChangeGroup"] + title = params["Title"] + filename = params["FileName"] + offset = params["Offset"] + factoryName = params["Name"] + globalFactory = params["GlobalFactory"] + + errorDict = {} + changes = [] + result = { + "ChangeGroup": changeGroup, + "Title": title, + } + + import rope.refactor.introduce_factory + resource = rope.base.libutils.path_to_resource( + self.__project, filename) + self.__progressHandle = ProgressHandle(self, title, True) + try: + introducer = \ + rope.refactor.introduce_factory.IntroduceFactoryRefactoring( + self.__project, resource, offset) + changes = introducer.get_changes( + factoryName, global_factory=globalFactory, + task_handle=self.__progressHandle) + except Exception as err: + errorDict = self.__handleRopeError(err) + self.__progressHandle.reset() + self.__progressHandle = None + + self.__changes[changeGroup] = changes + + result["Subcommand"] = "ChangesCalculated" + result.update(errorDict) + + self.sendJson("Changes", result) + if __name__ == '__main__': if len(sys.argv) != 4: