Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Redirect
locationhttps://www.jetbrains.com/help/idea/managing-imports-in-scala.html

IntelliJ IDEA lets you manage your imports in Scala the same way as it does in other languages.

You can configure the imports in the Project Settings and in the IDE Setting.Settings. You can also optimize your imports and exclude classes from auto-import in the editor.

1.Project Settings

You can format your imports using Project Settings.

span
classshortcut
Project Settings
span
classshortcut
Code
span
classshortcut
Scala Imports
In the Settings dialog, select
Wiki Markup
{span:class=shortcut}Project Settings{span}
Wiki Markup
{span:class=shortcut}Code{span}
Wiki Markup
{span:class=shortcut}Scala{span}
Wiki Markup
{span:class=shortcut}Imports{span}
.

  • Class count to use import with '_' - in this text field, specify the number of classes to be imported from a single package until all statements importing a single class are substituted with a statement importing an entire package.

...

  • Sort imports lexicographically (for optimize imports) - use this check box if you want IntelliJ IDEA to sort your imports in lexicographical order. HoweverNote, that if Add fully qualified imports check box is selected then fully qualified imports are sorted first.
  • Collect imports with the same prefix into one import - use this check box if you want IntelliJ IDEA to insert all imports with the same prefix into one. 
  • Import members using '_' - If you select this check box to import box then IntelliJ IDEA imports all members of the package (this is analogous to import members using '*' in Java)included in the object. If the check box is cleared, IntelliJ IDEA imports only a specific member such as method or a field from the object.
  • Use the shortest path when trying to import reference with already imported name - select this check box to let IntelliJ IDEA find the shortest available path when you import the reference with already imported name with a using code compleationcompletion.
  • Classes to use only with prefix - this area lets you view the list of classes with prefixes. You can add a pattern to use appropriate classes only with prefix or you can remove a class from the list.
  • Import Layout - this area lets you view the layout of the import statements, add or remove blank lines between them, and add or remove package names. You can also move the rows in the layout up or down .

2.IDE Settings

You can configure the behavior of the imports in the editor your workspace through the IDE Settings.

span
classshortcut
IDE Settings
span
classshortcut
Editor
span
classshortcut
Auto Import
In the Settings dialog, select
Wiki Markup
{span:class=shortcut}IDE Settings{span}
Wiki Markup
{span:class=shortcut}Editor{span}
Wiki Markup
{span:class=shortcut}Auto Import{span}
.

 You can select 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.
    The available options are 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.
Note

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.

  •  → span
    classshortcut
    Optimize Imports

    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

    span
    classshortcut
    Code
    Wiki Markup
    {span:class=shortcut}Code{span}
     →
    Wiki Markup
    {span:class=shortcut}Optimize Imports{span}
    .

  • Add unambiguous imports on the fly - select this check box to have IntelliJ IDEA automatically add imports that can be added without user intervention.

3.Optimizing Imports

You can optimize imports using

span
classshortcut
Code
 → span
classshortcut
Optimize Importsselecting
Wiki Markup
{span:class=shortcut}Code{span}
 →
Wiki Markup
{span:class=shortcut}Optimize Imports{span}
command.

Image RemovedImage Added

In this dialog box, specify from where you want IntelliJ IDEA to remove unused import statements, in order to optimize the import procedure.

  • File - click this option to have unused imports removed from the current file. This option is selected by default, if a file in the editor has the focus, or if it is selected in the Project tool window.
  • All files in directory <directory name> - click this option to have unused imports removed from all files in the current directory at once. This option is selected by default, if a directory is selected in the Project tool window.
  • Rearrange entries - select this check box to reorder your code entries.
  • Only VCS changed text - 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.
  • Do not show this dialog in the future - select this check box to perform import optimization in the future silently.

4.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.

Start typing a name in the editor.

Image Added

In the Class to Import suggestion list, click

Wiki Markup
{span:class=shortcut}Alt{span}
+
Wiki Markup
{span:class=shortcut}Enter{span}
, 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.

Image Added

In the dialog that opens perform necessary changes and click OK.

Image Added