|
21 | 21 | from rope.base import project, libutils, exceptions, change, worder # noqa |
22 | 22 | from rope.base.fscommands import FileSystemCommands # noqa |
23 | 23 | from rope.contrib import autoimport as rope_autoimport, codeassist, findit # noqa |
24 | | -from rope.refactor import ModuleToPackage, ImportOrganizer, rename, extract, inline, usefunction, move # noqa |
| 24 | +from rope.refactor import ModuleToPackage, ImportOrganizer, rename, extract, inline, usefunction, move, change_signature # noqa |
25 | 25 | from rope.base.taskhandle import TaskHandle # noqa |
26 | 26 |
|
27 | 27 |
|
@@ -706,6 +706,56 @@ def get_refactor(ctx): |
706 | 706 | return move.create_move(ctx.project, ctx.resource, offset) |
707 | 707 |
|
708 | 708 |
|
| 709 | +class ChangeSignatureRefactoring(Refactoring): |
| 710 | + |
| 711 | + """ Change function signature (add/remove/sort arguments). """ |
| 712 | + |
| 713 | + @staticmethod |
| 714 | + def get_input_str(refactor, ctx): |
| 715 | + """ Get destination. |
| 716 | +
|
| 717 | + :return str: |
| 718 | +
|
| 719 | + """ |
| 720 | + args = refactor.get_args() |
| 721 | + default = ', '.join(a[0] for a in args) |
| 722 | + return pymode_input('Change the signature:', udefault=default) |
| 723 | + |
| 724 | + @staticmethod |
| 725 | + def get_refactor(ctx): |
| 726 | + """ Function description. |
| 727 | +
|
| 728 | + :return Rename: |
| 729 | +
|
| 730 | + """ |
| 731 | + _, offset = get_assist_params() |
| 732 | + return change_signature.ChangeSignature( |
| 733 | + ctx.project, ctx.resource, offset) |
| 734 | + |
| 735 | + def get_changes(self, refactor, input_string): |
| 736 | + """ Function description. """ |
| 737 | + |
| 738 | + args = re.sub(r'[\s\(\)]+', '', input_string).split(',') |
| 739 | + olds = [arg[0] for arg in refactor.get_args()] |
| 740 | + |
| 741 | + changers = [] |
| 742 | + for arg in [a for a in olds if not a in args]: |
| 743 | + changers.append(change_signature.ArgumentRemover(olds.index(arg))) |
| 744 | + olds.remove(arg) |
| 745 | + |
| 746 | + order = [] |
| 747 | + for index, arg in enumerate(args): |
| 748 | + if arg not in olds: |
| 749 | + changers.append(change_signature.ArgumentAdder(index, arg)) |
| 750 | + olds.insert(index, arg) |
| 751 | + order.append(olds.index(arg)) |
| 752 | + |
| 753 | + changers.append(change_signature.ArgumentReorderer( |
| 754 | + order, autodef='None')) |
| 755 | + |
| 756 | + return refactor.get_changes(changers) |
| 757 | + |
| 758 | + |
709 | 759 | def reload_changes(changes): |
710 | 760 | """ Reload changed buffers. """ |
711 | 761 |
|
|
0 commit comments