|
20 | 20 |
|
21 | 21 | from rope.base import project, libutils, exceptions, change, worder # noqa |
22 | 22 | from rope.base.fscommands import FileSystemCommands # noqa |
23 | | -from rope.contrib import autoimport as rope_autoimport, codeassist, findit # noqa |
24 | | -from rope.refactor import ModuleToPackage, ImportOrganizer, rename, extract, inline, usefunction, move, change_signature # noqa |
| 23 | +from rope.contrib import autoimport as rope_autoimport, codeassist, findit, generate # noqa |
| 24 | +from rope.refactor import ModuleToPackage, ImportOrganizer, rename, extract, inline, usefunction, move, change_signature, importutils # noqa |
25 | 25 | from rope.base.taskhandle import TaskHandle # noqa |
26 | 26 |
|
27 | 27 |
|
@@ -337,11 +337,11 @@ def autoimport(): |
337 | 337 |
|
338 | 338 | source, _ = get_assist_params() |
339 | 339 | if len(modules) == 1: |
340 | | - _insert_import(word, modules[0], ctx, source) |
| 340 | + _insert_import(word, modules[0], ctx) |
341 | 341 |
|
342 | 342 | else: |
343 | 343 | module = pymode_inputlist('Wich module to import:', modules) |
344 | | - _insert_import(word, module, ctx, source) |
| 344 | + _insert_import(word, module, ctx) |
345 | 345 |
|
346 | 346 | return True |
347 | 347 |
|
@@ -756,6 +756,32 @@ def get_changes(self, refactor, input_string): |
756 | 756 | return refactor.get_changes(changers) |
757 | 757 |
|
758 | 758 |
|
| 759 | +class GenerateElementRefactoring(Refactoring): |
| 760 | + |
| 761 | + """ Class description. """ |
| 762 | + |
| 763 | + def __init__(self, kind, *args, **kwargs): |
| 764 | + """ Function description. """ |
| 765 | + self.kind = kind |
| 766 | + super(GenerateElementRefactoring, self).__init__(*args, **kwargs) |
| 767 | + |
| 768 | + def get_refactor(self, ctx): |
| 769 | + """ Function description. |
| 770 | +
|
| 771 | + :return Rename: |
| 772 | +
|
| 773 | + """ |
| 774 | + _, offset = get_assist_params() |
| 775 | + return generate.create_generate( |
| 776 | + self.kind, ctx.project, ctx.resource, offset) |
| 777 | + |
| 778 | + def get_changes(self, refactor, input_str): |
| 779 | + """ Function description. """ |
| 780 | + |
| 781 | + print(refactor) |
| 782 | + return refactor.get_changes() |
| 783 | + |
| 784 | + |
759 | 785 | def reload_changes(changes): |
760 | 786 | """ Reload changed buffers. """ |
761 | 787 |
|
@@ -849,19 +875,40 @@ def complete_check(): |
849 | 875 |
|
850 | 876 | source, _ = get_assist_params() |
851 | 877 | if len(modules) == 1: |
852 | | - _insert_import(name, modules[0], ctx, source) |
| 878 | + _insert_import(name, modules[0], ctx) |
853 | 879 |
|
854 | 880 | else: |
855 | 881 | module = pymode_inputlist('With module to import:', modules) |
856 | 882 | if module: |
857 | | - _insert_import(name, module, ctx, source) |
| 883 | + _insert_import(name, module, ctx) |
858 | 884 |
|
859 | 885 | vim.command('call pymode#save()') |
860 | 886 | regenerate() |
861 | 887 |
|
862 | 888 |
|
863 | | -def _insert_import(name, module, ctx, source): |
864 | | - linenum = int(ctx.importer.find_insertion_line(source)) |
865 | | - line = 'from %s import %s' % (module, name) |
866 | | - vim.current.buffer[linenum - 1:linenum - 1] = [line] |
867 | | - vim.current.buffer[linenum:linenum] = [''] |
| 889 | +def _insert_import(name, module, ctx): |
| 890 | + pyobject = ctx.project.pycore.resource_to_pyobject(ctx.resource) |
| 891 | + import_tools = importutils.ImportTools(ctx.project.pycore) |
| 892 | + module_imports = import_tools.module_imports(pyobject) |
| 893 | + new_import = importutils.FromImport(module, 0, [[name, None]]) |
| 894 | + module_imports.add_import(new_import) |
| 895 | + changes = change.ChangeContents( |
| 896 | + ctx.resource, module_imports.get_changed_source()) |
| 897 | + |
| 898 | + action = pymode_inputlist( |
| 899 | + 'Choose what to do:', ['perform', 'preview']) |
| 900 | + |
| 901 | + if not action: |
| 902 | + return False |
| 903 | + |
| 904 | + if action == 'preview': |
| 905 | + print("\n ") |
| 906 | + print("-------------------------------") |
| 907 | + print("\n%s\n" % changes.get_description()) |
| 908 | + print("-------------------------------\n\n") |
| 909 | + if not pymode_confirm(False): |
| 910 | + return False |
| 911 | + |
| 912 | + progress = ProgressHandler('Apply changes ...') |
| 913 | + ctx.project.do(changes, task_handle=progress.handle) |
| 914 | + reload_changes(changes) |
0 commit comments