IntelliJ IDEA 2016.2 Help

Managing Imports in Scala

IntelliJ IDEA lets you manage your imports in Scala the same way as it does in other languages. You can configure imports in the Project Settings and in the IDE Settings. You can also optimize your imports and exclude classes from auto-import in the editor.

Managing Scala Imports through the Scala Settings

You can format your imports using Project Settings.

  1. Select File | Settings | Code Style | Scala.
  2. On the Scala page, select Imports tab.
    sc_imports_settings
  3. In the Imports tab, configure the Importing settings and press OK.

Managing Scala Imports through Auto Import Settings

You can configure the behavior of the imports in your workspace through the Auto Import settings.

  1. Select File | Settings | Editor | General | Auto Import.
    sc_auto_import
  2. On the Auto Import page, in the Scala section, select from the following options:
    • Insert imports on paste - use this drop-down list to define how IntelliJ IDEA will insert imports for pasted blocks of code if they contain references to classes that are not imported into the target class.
      You can select from the following options:
      • All - select this option to have IntelliJ IDEA automatically add import statements for all classes that are found in the pasted block of code and are not imported in the current class yet.
      • Ask - if this option is selected, when pasting code blocks, IntelliJ IDEA will open a dialog box, where you can choose the desired imports.
      • None - select this option to suppress import.

      If you skip an import suggested in the Ask mode or choose the None mode, the non-imported classes will be red-highlighted and an import pop-up window will appear to help you create import statements using the Alt + Enter keyboard shortcut.

    • Optimize imports on the fly - select this check box to have the Optimize Imports operation automatically performed for your files.
      If you clear this check box, you can manually optimize your imports selecting Code | Optimize Imports.
    • Add unambiguous imports on the fly - select this check box to have IntelliJ IDEA automatically add imports that can be added without user intervention.

Optimizing Imports

You can optimize imports on the directory selecting Code | Optimize Imports command. In this dialog box, specify from where you want IntelliJ IDEA to remove unused import statements, in order to optimize the import procedure.

optimize_imports_directory

  • Process only VCS changed files - if this check box is selected, then reformatting will apply only to the files that have been changed locally, but not yet checked in to the repository.
    This check box is only available for the files under version control.

Excluding Classes from Auto-Import

If the list of suggested imports is too wide, you can exclude unnecessary classes on the fly using intention actions.

  1. Start typing a name in the editor.
    sc_exclude_classes
  2. In the Class to Import suggestion list, click Alt + Enter, and click the right arrow to reveal the nested list of intention actions.
    IntelliJ IDEA suggests you to exclude specific class or the whole containing package.
    sc_class_to_import
  3. In the dialog that opens perform necessary changes and click OK.
    sc_auto_import_dialog
Last modified: 23 November 2016