Tue, 05 Oct 2021 19:36:02 +0200
Finished implementing the Jedi refactoring interface.
--- a/docs/changelog Tue Oct 05 18:13:40 2021 +0200 +++ b/docs/changelog Tue Oct 05 19:36:02 2021 +0200 @@ -18,6 +18,7 @@ -- integrated the Assistant Jedi plugin as a fixed part of eric -- added code to jump to references when clicked on a definition -- added support for mouse hover help + -- added support for simple refactorings to the editor context menu - Plugin Repository -- added an integrated plugin repository viewer (right side) - Plugin Uninstall Dialog
--- a/eric7/JediInterface/AssistantJedi.py Tue Oct 05 18:13:40 2021 +0200 +++ b/eric7/JediInterface/AssistantJedi.py Tue Oct 05 19:36:02 2021 +0200 @@ -232,6 +232,10 @@ self.__menu.addAction( self.tr("Inline Variable"), self.__jediServer.refactoringInlineVariable) + self.__menu.addSeparator() + self.__menu.addAction( + self.tr("Extract Function"), + self.__jediServer.refactoringExtractFunction) def __editorShowMenu(self, menuName, menu, editor): """
--- a/eric7/JediInterface/JediClient.py Tue Oct 05 18:13:40 2021 +0200 +++ b/eric7/JediInterface/JediClient.py Tue Oct 05 19:36:02 2021 +0200 @@ -41,10 +41,6 @@ """ super().__init__(host, port, idString) - # TODO: add additional methods for these topics - # - extract function - # - extract variable - # - inline variable self.__methodMapping = { "openProject": self.__openProject, "closeProject": self.__closeProject, @@ -59,6 +55,7 @@ "renameVariable": self.__renameVariable, "extractVariable": self.__extractVariable, "inlineVariable": self.__inlineVariable, + "extractFunction": self.__extractFunction, "applyRefactoring": self.__applyRefactoring, "cancelRefactoring": self.__cancelRefactoring, } @@ -519,6 +516,45 @@ self.sendJson("RefactoringDiff", result) + def __extractFunction(self, params): + """ + Private method to extract an expression to a new function. + + @param params dictionary containing the method parameters + @type dict + """ + filename = params["FileName"] + source = params["Source"] + line = params["Line"] + index = params["Index"] + endLine = params["EndLine"] + endIndex = params["EndIndex"] + uid = params["Uuid"] + newName = params["NewName"] + + errorDict = {} + diff = "" + + script = jedi.Script(source, path=filename, project=self.__project) + + try: + refactoring = script.extract_function( + line, index, new_name=newName, + until_line=endLine, until_column=endIndex + ) + self.__refactorings[uid] = refactoring + diff = refactoring.get_diff() + except SuppressedException as err: + errorDict = self.__handleError(err) + + result = { + "Diff": diff, + "Uuid": uid, + } + result.update(errorDict) + + self.sendJson("RefactoringDiff", result) + def __applyRefactoring(self, params): """ Private method to apply a refactoring.
--- a/eric7/JediInterface/JediServer.py Tue Oct 05 18:13:40 2021 +0200 +++ b/eric7/JediInterface/JediServer.py Tue Oct 05 19:36:02 2021 +0200 @@ -596,6 +596,45 @@ "Uuid": euuid, }, idString=idString) + @pyqtSlot() + def refactoringExtractFunction(self): + """ + Public slot to extract an expression to a function. + """ + editor = self.__vm.activeWindow() + if editor: + idString = self.__idString(editor) + if not idString: + return + + newName, ok = QInputDialog.getText( + None, + self.tr("Extract Function"), + self.tr("Enter the name for the function:"), + QLineEdit.EchoMode.Normal + ) + + if ok and newName: + filename = editor.getFileName() + sLine, sIndex, eLine, eIndex = editor.getSelection() + source = editor.text() + + self.__ensureActive(idString) + + euuid = str(uuid.uuid4()) + self.__editors[euuid] = editor + + self.sendJson("extractFunction", { + "FileName": filename, + "Source": source, + "Line": sLine + 1, + "Index": sIndex, + "EndLine": eLine + 1, + "EndIndex": eIndex, + "Uuid": euuid, + "NewName": newName, + }, idString=idString) + def __showRefactoringDiff(self, result): """ Private method to show the diff of a refactoring.