RubyMine 2019.2 Help

RuboCop

RubyMine supports integration with RuboCop and enables you to fix its offenses right inside the IDE, for example, in the code editor.

RuboCop offense

You can also check the entire project and display all RuboCop warnings in a single report.

RuboCop inspection results

If necessary, you can enable the Standard wrapper and use it for analyzing project sources.

Install RuboCop/Standard gems

The RuboCop inspection is enabled in RubyMine by default and requires the 'rubocop' gem to be installed in the project’s SDK. If this gem is not installed, RubyMine will suggest doing this.

Install Rubocop

If you want to use Standard to analyze your project, add the 'standard' gem to your Gemfile and install it.

Enable/Disable RuboCop and Standard inspections

To enable or disable RuboCop and Standard inspections, do the following:

  1. Open the Settings/Preferences dialog (Ctrl+Alt+S).

  2. Go to the Editor | Inspections page and enable/disable the RuboCop inspection.

  3. If necessary, enable the Standard option to use the 'standard' gem.

    Enable Standard

Fix RuboCop offenses in the editor

When RuboCop inspections are enabled, RubyMine highlights its offenses in the code editor. To see the warning description and fix it, do the following:

  1. Place the caret at the highlighted area and press Ctrl+F1.

    RuboCop warning in the editor
  2. To fix the offense, press Alt+Enter. You can fix all suggested offenses in a file, or you can apply only specific fixes by a class offense or cop department.

    Fix RuboCop offense

Run Rubocop inspection

RubyMine enables you to find RuboCop offenses for the entire project by running a corresponding inspection. To do this, follow the steps below:

  1. From the main menu, go to Code | Run inspection by name (Ctrl+Shift+Alt+I).

  2. In the popup, find RuboCop and press Enter.

  3. In the Run 'RuboCop' dialog, specify the scope of files that you want to analyze. If necessary, enable Use 'standard' gem. Click OK.

  4. Wait until RubyMine analyzes your project. In the Inspection Results tool window, you can explore RuboCop offenses and fix auto-correctable ones using the Fix all auto-correctable RuboCop offenses button.

    RuboCop inspection results

    You can also explore individual files and apply specific fixes by a class offense or cop department.

    RuboCop inspection results

.rubocop.yml

RubyMine takes into account all settings specified in the .rubocop.yml file. For example, if you disable some checks for a specified cop department, the editor will not show corresponding offenses. Moreover, these offenses won't be shown in the RuboCop inspection report.

RuboCop severity settings

Note that severity settings specified for the RuboCop inspection are not in effect when displaying inspection results. RubyMine uses the following severities for RuboCop inspections.

RuboCop Severity

RubyMine Severity

Refactor
Convention

Weak Warning

Warning

Warning

Error
Fatal

Error

How to: Run RuboCop for the opened file with custom options

Sometimes it is necessary to run RuboCop for the currently opened file with custom options. For example, this can be useful when RuboCop inspections are disabled, or you want to assign a shortcut for a specific RuboCop action.

In this example, we'll show how to use RuboCop as an external tool to correct layout offenses in the currently opened file.

Create a custom RuboCop action

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), select Tools | External Tools.

  2. Click the Add button and specify the following settings:

    The Edit Tool dialog
    • Name: The menu command that will be displayed in the RubyMine interface (the Tools menu and context menus).

    • Group: The name of the group to which the command belongs. You can select an existing group or type the name of a new group.

    • Program: The name of the RuboCop executable.

    • Arguments: The arguments passed to the executable file, as you would specify them on the command line. Here we use -x RuboCop option to run only layout cops for the current file.

    • Working directory: The path to the current working directory from which the tool is executed.

  3. Click OK to add the tool and then apply the changes.

  4. (Optional) Assign a shortcut for the created action. In the Settings/Preferences dialog (Ctrl+Alt+S), select Keymap, find the Fix Layout action under the RuboCop node and specify a shortcut.

    Configure shortcut

Run a custom RuboCop action

  • To run the created RuboCop action, go to the Tools main menu or use the context menu of a file.

    Run custom action

    When the command runs, its output is displayed in the Run tool window:

    Custom action output
Last modified: 5 August 2019

See Also