RubyMine 2018.1 Help

Move and Copy refactorings

Basics

Move refactorings allow you to move packages and classes between the source roots of a project, class members to other classes and inner classes to upper hierarchy levels. The move refactorings automatically correct all references to the moved packages, classes and members in the source code.

The following move refactorings are available:

  • Move Package moves a package and its contents to another package or directory under the specified source root. When you move a package, you can choose between the following refactorings:
    • Move package to another package.
    • Move directory to another source root.
    • Move directory to another directory.
  • Move Class refactoring enables you to:
    • Move a class to a package under the specified source root.
    • Make a class an inner class.
  • Move Static Members moves a static field, method or inner class to another class.
  • Move Inner to Upper Level:
    • In Java, this refactoring moves an inner class to a higher level.
    • In ActionScript, this refactoring moves out-of-package classes, functions, variables, constants and namespaces into a package. (In this case the word inner is used to refer to entities (classes, functions, etc.) that are declared outside of packages. The upper level means a package.)
  • Move Instance Method refactoring moves an instance method to another class.
  • Move File refactoring moves a file to another directory.

The Move refactorings allow you to move classes, modules, members, files and directories within a project. So doing, RubyMine tracks these movements and automatically corrects all references to the moved symbols in the source code.

The following Move refactorings are available:

  • The Move File refactoring moves a file to another directory.
  • The Move Directory refactoring moves a directory to another directory.
  • The Move Static Member refactoring moves a static method, variable, or constant from one class to another.
  • The Move Class or Module refactoring moves a Ruby class or module to its own class, creates a hierarchy of directories and adds 'require' statement to the source file.

Performing Move refactoring

To perform a Move refactoring:

  1. Select the symbol to be moved and do one of the following:
    • On the main menu, or on the context menu, point to Refactor, and then choose Move.
    • Press F6.
    • In the Project Tool Window, drag the symbol to the new location.

    The dialog that opens depends on the type of the selected symbol.

  2. Specify the move options according to the type of the item to be moved. See option descriptions in the Move dialog box reference.
  3. Preview and apply the changes.

Moving a package

To move a package:

  1. Select the package in the Project tool window, and press F6, or just drag the selected package.
  2. In the Select Refactoring dialog box, click one of the options to specify which refactoring should be performed.
    ij move package
    • To move the directory to another source root, select the second option, click OK, and specify the destination source root.
    • To move the directory to another directory, select the third option, click OK, and specify the destination directory.

Moving a file or a directory to another directory

  1. Select the file or directory in the Project tool window.
  2. Do one of the following:
    • On the main menu, choose Refactor | Move.
    • On the context menu of the selection, choose Refactor | Move
    • Press F6.

    The dialog that opens depends on the type of the selected symbol.

  3. In the To directory field, specify the folder to move the selected file or folder to. Choose an existing folder from the drop-down list or type the full path to the parent folder to be created.
  4. To have the references to the selected file or folder updated according to the refactoring result, select the Search for references checkbox.

Moving a class/module

To move a class/module into its own file:

  1. In the editor, place the caret at the name of a class/module to be moved, and do one of the following:
    • On the main menu, or on the context menu, point to Refactor, and then choose Move.
    • Press F6.
  2. In the Move Class or Module Dialog box that opens, specify the following:
    • New module name
    • New file path
    • Path to the directory where the new module will reside.
  3. Click Refactor.

The Move Module refactoring moves a Ruby module to its own class, creates a hierarchy of directories and adds 'require' statement to the source file.

Examples

Create a Ruby file with the name refactoring_sample.rb that contains the following code:

module AModule class AClass1 class AClass2 < AModule::AClass1 end def foo end end end

Example 1

Place the caret at the module name AModule. The following dialog box opens:

ruby move module1

Accept defaults and click Refactor. This action results in the following:

  • The directory refactoring_sample is created.
  • Under this directory, the file a_module is created. This file contains the initial source code specified above.
  • The file refactoring_sample.rb now contains the code:
    class Refactoring_Sample require File.expand_path(File.dirname(__FILE__) + '/refactoring_sample/a_module.rb') end

Example 2

Place the caret at the class name AClass1. The dialog box opens:

ruby move module2

Accept defaults and click Refactor. This action results in the following:

  • The directory structure refactoring_sample/a_module is created.
  • Under this directory, the file a_class1 is created. This file contains the initial source code specified above.
  • The file refactoring_sample.rb now contains the code:
    class Refactoring_Sample module AModule require File.expand_path(File.dirname(__FILE__) + '/refactoring_sample/a_module/a_class1.rb') end end

Copy refactoring

The Copy refactoring lets you create a copy of a class, file or directory in a different or the same directory.

To perform the Copy refactoring:

  1. Select the item of interest in a tool window (e.g. the Project tool window). Alternatively, open the necessary class or file in the editor.
  2. Do one of the following:
    • Choose Refactor | Copy on the main menu or the context menu.
    • Press F5.
    • In the Project tool window, press and hold the Ctrl key, and drag the item to the target location.
  3. In the Copy dialog that opens, specify the name and location for the copy that you are creating, and click OK.
Last modified: 1 August 2018

See Also