Example #1
0
 def rename(self):
     """
     Renames word under cursor.
     """
     editor = api.editor.get_current_editor()
     if editor is None:
         return
     editor.file.save()
     assert isinstance(editor, PyCodeEdit)
     module = libutils.path_to_resource(self._main_project,
                                        editor.file.path)
     self._main_project.validate()
     cursor_position = self._get_real_position(
         editor.textCursor().position())
     try:
         renamer = Rename(self._main_project, module, cursor_position)
     except RopeError:
         return
     if not renamer.get_old_name():
         return
     preview, replacement = DlgRope.rename(self.main_window,
                                           renamer.get_old_name())
     if preview is None and replacement is None:
         return
     multiproj = self._has_multiple_projects()
     other_projects = self._get_other_projects()
     main_project = self._main_project
     self._preview = preview
     api.tasks.start(_('Refactoring: rename'),
                     rename_symbol,
                     self._on_changes_available,
                     args=(main_project, multiproj, other_projects,
                           editor.file.path, cursor_position, replacement),
                     cancellable=True,
                     use_thread=True)
Example #2
0
 def rename(self):
     """
     Renames word under cursor.
     """
     editor = api.editor.get_current_editor()
     if editor is None:
         return
     editor.file.save()
     assert isinstance(editor, PyCodeEdit)
     module = libutils.path_to_resource(
         self._main_project, editor.file.path)
     self._main_project.validate()
     cursor_position = self._get_real_position(
         editor.textCursor().position())
     try:
         renamer = Rename(self._main_project, module, cursor_position)
     except RopeError:
         return
     if not renamer.get_old_name():
         return
     preview, replacement = DlgRope.rename(
         self.main_window, renamer.get_old_name())
     if preview is None and replacement is None:
         return
     multiproj = self._has_multiple_projects()
     other_projects = self._get_other_projects()
     main_project = self._main_project
     self._preview = preview
     api.tasks.start(_('Refactoring: rename'), rename_symbol,
                     self._on_changes_available,
                     args=(
                         main_project, multiproj, other_projects,
                         editor.file.path, cursor_position, replacement),
                     cancellable=True, use_thread=True)
Example #3
0
def rename_all_api_modules():
    print('API modules are renaming (drop redundant "_api" suffix).')
    for api_resource in api_resources.get_folders():
        if api_resource.name.endswith('_api'):
            rename = Rename(project, api_resource)
            new_name = rename.get_old_name().split('_api')[0]

            print('\told: ' + api_resource.name + ' new: ' + new_name)

            changes = rename.get_changes(new_name)
            project.do(changes)
Example #4
0
def main():
    from rope.base.project import Project
    from rope.base import libutils
    from rope.refactor.rename import Rename
    from rope.base.exceptions import RopeError

    path = editor.get_path()
    selection = editor.get_selection()

    if not path or not selection:
        console.hud_alert('Not a Python file', 'error')
        return

    tab.save()

    project = None
    try:
        project = Project(os.path.dirname(path), ropefolder=None)
        resource = libutils.path_to_resource(project, path)
        if not libutils.is_python_file(project, resource):
            console.hud_alert('Not a Python file', 'error')
            return

        renamer = Rename(project, resource, selection[0])
        old_name = renamer.get_old_name()

        if not old_name:
            console.hud_alert('Unable to get identifier name', 'error')
            return

        new_name = _ask_for_new_name(old_name)

        change_set = renamer.get_changes(new_name,
                                         docs=True,
                                         resources=[resource])
        if not change_set:
            console.hud_alert('No changes required')
            return

        if refactoring.ask_if_apply_change_set(change_set):
            refactoring.apply_change_set(change_set, path, selection)
            console.hud_alert('Identifier renamed')

    except RopeError as e:
        console.hud_alert(str(e), 'error')

    except KeyboardInterrupt:
        pass

    finally:
        if project:
            project.close()