IntelliJ IDEA 2016.3 Help

Rename Refactorings

On this page:

Basics

Rename refactorings allow you to rename symbols with all the references to them in the code corrected automatically.

The following rename refactorings are available in IntelliJ IDEA:

  • Rename Package. The following usages are renamed:
    • Package statements
    • Import statements
    • Qualified names of classes
  • Rename Class. The following usages are renamed:
    • Import statements
    • Qualified names of classes
    • Variables with the selected class type
    • Class inheritors
  • This feature is supported only when the Ruby plugin is installed.

  • Ruby scripts. Renaming in Ruby scripts applies to all symbols and propagates changes to all their usages across the project.
  • Ruby classes. Results of renaming a Ruby class depends on the place of invocation and on the containing file name. First, if a Ruby class has been created from a template, the containing file name matches the specified class name. Thus, renaming a Ruby class from the editor results in generating the new file name according to the Ruby naming conventions. If the name of a containing file doesn't match class name, only the class and its usages are renamed.

    Second, if rename refactoring is invoked from the Project tool window, only file name will be changed; the name of the contained class will be left intact.

  • Components of Rails applications. Renaming in Rails applications applies to the application elements (classes, controllers, actions, helpers, tests, views) and their usages. It is advisable to use the Rails View of the Project tool window, or the editor, to rename elements of the Rails applications.

    Rename refactoring in Rails applications has certain subtleties mentioned below:

    • On renaming an action or a view template, all associated entities are renamed, including tests. However, on renaming a test, its related entities are not renamed.
    • When renaming, keep in mind that the new name should meet Rails naming conventions; otherwise the name will not be properly recognized. It means that when you rename, for example, MyController to YourController, you only have to change My to Your, leaving the Controller suffix intact.
    • On renaming a controller or view, its usages in an RSpec test will only be renamed if the options Search in comments and strings is enabled.
    • When renaming Rails models, all usages are renamed too: the underlying files, classes, test classes and fixtures. IntelliJ IDEA creates a migration with the instruction to rename the corresponding table. When a field in a model is renamed, IntelliJ IDEA creates a migration with the instruction to rename the corresponding column in a table, and its foreign key, if any.
  • Named scopes. So doing, the lines of code where the renamed scope is called as a method, are also renamed.
  • Sass selector
  • Rename Method. The following usages are renamed:
    • All calls of the method.
    • All overridden/implemented methods in subclasses.
  • Rename Field.
  • Rename Function.
  • Rename Variable.
  • Rename Parameter. The following usages are renamed:
    • All usages of the parameter.
    • The corresponding param tag in documentation comment.
  • Rename CSS color value.
  • Rename File.
  • Rename Directory.
  • Rename views and references to views in the Grails applications.

Examples

Renaming a class

BeforeAfter
public class MyClass { // some code here } ... public void myMethod() { MyClass myClass = new MyClass(); }
public class YourClass { // some code here } ... public void myMethod() { YourClass yourClass = new YourClass(); }

Renaming a method

This feature is supported only when the Python plugin is installed.

BeforeAfter
Renaming method
def was_published_today(self): return self.pub_date.date () == datetime.date.today()
def published_today(self): return self.pub_date.date () == datetime.date.today()

Renaming a template

This feature is supported only when the Python plugin is installed.

Rename a template:

/help/img/idea/2016.3/python_rename1.png

So doing, the following usages will be renamed:

/help/img/idea/2016.3/python_rename2.png

Renaming Ruby/Rails symbols

This feature is supported only when the Ruby plugin is installed.

BeforeAfter
Renaming Ruby class with the matching file name
MyClass - my_class.rb YourClass - your_class.rb
Renaming Rails method
def bar(a,b,c) return a * b + c * 123 end def foo a = 0 b = 1 c = 2 return bar (a,b,c) end
def do_smth (a,b,c) return a * b + c * 123 end def foo a = 0 b = 1 c = 2 return do_smth (a,b,c) end
Renaming Rails model
Model Library.

The following symbols should be renamed:

  • Class Library
  • Fixture library.yml
  • Test class library_test.rb
  • Test class LibraryTest.rb
  • File library.rb
Model Books.

As a result of performing the Rename refactoring, IntelliJ IDEA creates a migration. Execute the migration to have the following symbols actually renamed:

  • Class Books
  • Fixture books.yml
  • Test class books_test.rb
  • Test class BooksTest.rb
  • File books.rb
Renaming a scope and its usages
Class Word scope :word_length, lambda {|word_length| where :char_count => word_length} end assert_equal 0, Word.word_length(0).size
Class Word scope :word_length1, lambda {|word_length| where :char_count => word_length} end assert_equal 0, Word.word_length1(0).size

Renaming a symbol

To rename a symbol, follow these general steps

  1. Select the item to be renamed.
    • To select a file, click the desired file in the Project tool window.
    • To select a symbol in the editor, place the caret at the name of the symbol to be renamed.
    • To select a symbol in the Project tool window, make sure that the members are shown, and then click the desired symbol.
    • To select a symbol in the Structure view, click the desired symbol in the Structure tool window.
  2. Choose Refactor | Rename on the main menu or on the context menu, or press Shift+F6.
  3. The subsequent behavior depends on the check box Enable in-place mode (Settings/Preferences dialog, Editor).
    • If this check box is selected, the suggested name appears right below the symbol in question. You can either accept suggestion, or type a new name. However, if you press Shift+F6 once more, IntelliJ IDEA will display the Rename dialog box with more options.
    • If this check box is not selected, the Rename dialog box opens immediately.

    The set of controls and their names depend on the type of the symbol to be renamed.

  4. If you want IntelliJ IDEA to find and rename objects related to the renamed class, whose names contain the changed string, check one or more of the following options:
    • Rename variables to rename the variables of that class type.
    • Rename inheritors to rename class inheritors.
    • Rename bound forms to rename the GUI forms bound to the class.

    If you chose to rename any of the objects bound to the renamed class, IntelliJ IDEA searches for appropriate items and displays them in a sequence of dialogs, sorted by type. In each dialog you may select the items you want to change.

  5. Preview and apply changes.

Renaming a file or directory

To rename a file or directory

  1. Select a desired file in the Project tool window.
  2. Choose Refactor | Rename on the main or context menu or press Shift+F6.
  3. In Rename File dialog box that opens, specify the new file name. Select Search in comments and strings checkbox to let IntelliJ IDEA apply changes to comments and strings.
  4. Press Preview to observe possible changes in Find Tool Window. Press Refactor to proceed.

    IntelliJ IDEA finds all the occurrences of the file name and changes them respectively.

Important notes

  • Local variables are renamed in-place.
    /help/img/idea/2016.3/rename_local.png

    This feature is supported only when the Python plugin is installed.

    /help/img/idea/2016.3/python_rename_local.png

    To be able to use the Rename dialog when renaming local variables, you should disable in-place refactoring in the editor settings.

  • This feature is supported only when the Python plugin is installed.

    When renaming Gherkin steps, mind the following limitations:
    • Step definitions should not contain regular expressions
    • Step names should contain alphanumeric characters only.
    • A step definition should be only one in various frameworks.
    • There should be a "one-to-one" mapping between a step and a step definition.

See Also

Last modified: 21 March 2017