--- a/eric7/JediInterface/JediClient.py Sun Oct 03 18:36:41 2021 +0200 +++ b/eric7/JediInterface/JediClient.py Mon Oct 04 19:59:06 2021 +0200 @@ -7,6 +7,7 @@ Module implementing the Jedi client of eric7. """ +import contextlib import sys SuppressedException = Exception @@ -55,11 +56,17 @@ "hoverHelp": self.__getHoverHelp, "gotoDefinition": self.__getAssignment, "gotoReferences": self.__getReferences, + + "renameVariable": self.__renameVariable, + "applyRefactoring": self.__applyRefactoring, + "cancelRefactoring": self.__cancelRefactoring, } self.__id = idString self.__project = None + + self.__refactorings = {} def handleCall(self, method, params): """ @@ -404,6 +411,81 @@ result.update(errorDict) self.sendJson("GotoReferencesResult", result) + + def __renameVariable(self, params): + """ + Private method to rename the variable under the cursor. + + @param params dictionary containing the method parameters + @type dict + """ + filename = params["FileName"] + source = params["Source"] + line = params["Line"] + index = params["Index"] + uid = params["Uuid"] + newName = params["NewName"] + + errorDict = {} + diff = "" + + script = jedi.Script(source, path=filename, project=self.__project) + + try: + refactoring = script.rename(line, index, new_name=newName) + 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("RenameVariableDiff", result) + + def __applyRefactoring(self, params): + """ + Private method to apply a refactoring. + + @param params dictionary containing the method parameters + @type dict + """ + uid = params["Uuid"] + + errorDict = {} + + try: + refactoring = self.__refactorings[uid] + refactoring.apply() + ok = True + except KeyError: + ok = False + except SuppressedException as err: + errorDict = self.__handleError(err) + + result = { + "result": ok, + } + result.update(errorDict) + + self.sendJson("RefactoringApplyResult", result) + + with contextlib.suppress(KeyError): + del self.__refactorings[uid] + + def __cancelRefactoring(self, params): + """ + Private method to cancel a refactoring. + + @param params dictionary containing the method parameters + @type dict + """ + uid = params["Uuid"] + with contextlib.suppress(KeyError): + del self.__refactorings[uid] if __name__ == '__main__':