--- a/RefactoringRope/RefactoringClient.py Sat Sep 23 12:39:00 2017 +0200 +++ b/RefactoringRope/RefactoringClient.py Sat Sep 23 14:17:49 2017 +0200 @@ -74,6 +74,7 @@ self.__calculateIntroduceFactoryChanges, "CalculateIntroduceParameterChanges": self.__calculateIntroduceParameterChanges, + "CalculateImportsChanges": self.__calculateImportsChanges, } from FileSystemCommands import RefactoringClientFileSystemCommands @@ -1042,6 +1043,63 @@ result.update(errorDict) self.sendJson("Changes", result) + + def __calculateImportsChanges(self, params): + """ + Private method to calculate the 'Import' 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"] + methodName = params["MethodName"] + + errorDict = {} + changes = [] + result = { + "ChangeGroup": changeGroup, + "Title": title, + } + + from rope.refactor.importutils import ImportOrganizer + method = { + "organize_imports": ImportOrganizer.organize_imports, + "expand_star_imports": ImportOrganizer.expand_star_imports, + "relatives_to_absolutes": ImportOrganizer.relatives_to_absolutes, + "froms_to_imports": ImportOrganizer.froms_to_imports, + "handle_long_imports": ImportOrganizer.handle_long_imports, + }[methodName] + importOrganizer = ImportOrganizer(self.__project) + resource = rope.base.libutils.path_to_resource( + self.__project, filename) + try: + changes = method(importOrganizer, resource, offset=offset) + except Exception as err: + errorDict = self.__handleRopeError(err) + + self.__changes[changeGroup] = changes + + # send the change description first + if changes: + description = changes.description + else: + description = "" + self.sendJson("Changes", { + "ChangeGroup": changeGroup, + "Title": title, + "Subcommand": "ChangeDescription", + "Description": description, + }) + + result["Subcommand"] = "ChangesCalculated" + result.update(errorDict) + + self.sendJson("Changes", result) if __name__ == '__main__':