IntelliJ IDEA 2019.3 Help

Work with Scala code in the editor

IntelliJ IDEA lets you use different Scala intention actions, convert your code from Java to Scala, and use different Scala templates while working in the IntelliJ IDEA editor.

Strings in Scala

You can add different intentions for strings, perform different actions, and set a different format for multi-line strings.

Check the following examples:

  • Insert gap with concatenation ("+ +") into a string.

  • Convert a string into a multi-line string using the Convert to """string""" intention and vice versa.
    1. Enter your string, press Alt+Enter and from the list of intentions, select Convert to """string""".

    2. You also can convert the multi-line string into the regular string. Press Alt+Enter to open the list of intentions. Select Convert to "string" and press Enter. View the result.

    3. To enter a multi-line string, type triple quotes in your editor. If you press Enter, it will automatically invoke the stripMargin method. The stripMargin method removes the left-hand part of a multi-line string up to a specified delimiter. The white spaces are also preserved.

  • Add the .replace("\r"," ") intention. This intention lets you keep the caret at the correct place on the next line in the multi-line strings regardless of what operating system you have at the moment.

    Enter a multi-line string, press Alt+Enter and select the appropriate intention from the list.

    the replace intention
  • Use the Inject Language/Reference intention to insert a language or a reference into your multi-line string literals. For more information, refer to the Language Injections documentation.

    You can inject languages into multiline string literals with margins.

  • Use the Multi-line stings tab in Scala settings to set a different format for multi-line strings' options such as Margin char indent or disable a multi-line strings support.
    1. In the main menu, select File | Setting | Editor | Code Style | Scala.

    2. On the Scala page, select the Multi-line strings tab.

      the Multi-line strings settings

    3. Edit the settings and click OK.

  • Turn simple string into the interpolated one adding a variable reference.

Implicit hints

IntelliJ IDEA lets you enable, expand and collapse editor hints for implicit conversions and arguments to help you read your code.

  1. On the main toolbar, select View | Show Implicit Hints.

  2. In the editor, right-click the hint and from the popup menu, select the appropriate action in order to expand the existing hint, disable the mode, or to see the implicit arguments.

    Implicit hints popup

Alternatively, while in the editor, you can press Ctrl+Shift+Alt++ to enable the implicit hints. If you press the same shortcut again, IntelliJ IDEA expands the implicit hints to show you more detailed information. Press Ctrl+Shift+Alt+- to collapse the hints.

Type hints for method chains

IntelliJ IDEA automatically displays type hints for long expression chains.

Inlay hints for Scala method chains

You can disable the inlay hints if you right-click the hint and from the context menu uncheck the Show method chain inlay hints option. From the same menu you can control how to display the inlay hints and what hints to hide.

Access the inlay hints settings

  1. In the Settings/Preferences dialog Ctrl+Alt+S, go to Editor | Inlay Hints | Scala.

  2. From the list, select Method chains and select or clear the following options:
    • Show method chain hints: clear this option to disable the hints.

    • Align type hints in method chains: by default, IntelliJ IDEA displays the hints as a separate column, which helps you easily view the type flow.

    • Hide identical types in method chains: with this option you can omit hints when the type is obvious.

    • Minimal unique type to show method chains: you can change the number of unique types in a chain that are required to show the hints.

Find Usages for implicits

You can navigate from implicits definitions to their usages using the Find Usages action.

This capability relies on bytecode indexes, and can also be used to locate the following hidden elements that are not present in the source code “as is”:

  • apply / unapply method calls

  • Single Abstract Method (SAM) type instantiations

  • foreach / map/ flatMap / filter / withFilter calls via a for-comprehension

  1. In the editor, select the implicits definition and from the context menu, select Find Usages Alt+F7.

  2. View the result in the Find tool window.

    Find usages for implicits

Implicit conversions

IntelliJ IDEA lets you invoke implicit conversion methods and arguments.

Convert method implicitly

  1. Select an expression and press Shift+Ctrl+Q (Ctrl+Q for macOS) to invoke the list of applicable implicit conversions. The list shows the regular scope displayed on the top and the expanded scope that is displayed on the bottom of the list.
    Choose implicit conversion
  2. If you need, make the implicit conversion method explicit. Press Alt+Enter and select Make explicit or Make explicit (Import method).
    Make explicit
    • If you select Make explicit then IntelliJ IDEA returns a method call with the class name. It might be helpful if you need to make sure that the compiler imports a particular implicit conversion method that you originally wanted:

      Make explicit result

    • If you select Make explicit (Import method) then the method is imported statically and IntelliJ IDEA returns just its call without the class name. Also, the next time you open the list of useful implicit conversions you will see this method in the regular scope:

      Make explicit import method

Convert argument implicitly

  1. Place a cursor to the method where implicit conversion was used and press Ctrl+Shift+P to invoke implicit arguments. It might be helpful for code analyzing when you want to find out what implicit arguments were passed to the particular call. IntelliJ IDEA also lets you view the recursive implicit arguments.
    Implicit arguments
  2. If IntelliJ IDEA cannot find method calls where implicit parameters were passed, it displays a popup message:

    No implicit arguments popup

Type Info action

IntelliJ IDEA lets you work with type inferences using the Scala Show Type Info action:

  • To invoke the Show Type Info action in the editor, navigate to the value and press Alt+Equals or Ctrl+Shift+P (for Mac OS):
    Show type info for string
  • You can also see the type information on a value definition. Put the caret at a value definition and press Alt+Equals or Ctrl+Shift+P (for Mac OS):
    Show type info for integer
  • To add a type annotation, highlight the value, press Shift+Enter and from the context menu select Add type annotation to value definition: As a result, the type annotation is added.

    To remove the type annotation, press Shift+Enter and select Remove type annotation from value definition.

Wrap / unwrap expressions

You can wrap or unwrap expressions in Scala code automatically as you type.

  1. Enter code in the editor.

  2. Add an opening brace before an expression you want to wrap, IntelliJ IDEA adds a closing brace automatically at the end of the expression. To unwrap an expression, delete the opening brace, IntelliJ IDEA deletes the closing brace automatically.

Code completion

You can use code completion for the following actions:

  • To import classes, press Shift+Enter on the code, select Import class. However, in lots of cases IntelliJ IDEA recognizes what you need to import and displays a list of suggestions.

  • A name-based type suggestion for parameters. IntelliJ IDEA lets you automatically complete both the name and the type before actually adding a type reference.

    Type suggestions for parameters

  • Invoke the Convert to interpolated string intention.
    You can easily convert a regular string into the interpolated one using code completion after $.
    Alternatively, select a value with concatenation in your string, press Alt+Enter and select Convert to interpolated string.
  • If you have sealed types with inheritors, Java Enums, and Scala Enumerations, you can generate an exhaustive match checking for them. Note that auto-completion is available.

    the exhaustive match checking
    As a result, the compiler checks a pattern match for all possible members of a sealed type.
    the result of the exhaustive match checking

  • You can complete code not only inside case clauses, but you can complete the whole case clause as well.

Create from usage

IntelliJ IDEA lets you create new code elements without declaring them first:

  1. In the editor, type a name of a new code element and press Alt+Enter.

  2. From the list of intentions, select the one you need.

    Create from usage
    Press Enter.

Structure view

IntelliJ IDEA lets you view a structure of your code:

  • To open the Structure tool window, press Alt+7.

    View code structure

  • To navigate from the Structure tool window to the code item in the editor, press F4.
    To highlight the code, press Ctrl+Enter.

Java-to-Scala code conversion

IntelliJ IDEA lets you convert Java code into Scala.

  1. Copy your Java code (expression, method, class) and paste it into a Scala file.

  2. IntelliJ IDEA displays the Convert the code from Java dialog suggesting a conversion. Click OK.

If you do not want to use the copy/paste actions, you can open your Java file in the editor and select Refactor | Convert to Scala or press Ctrl+Shift+G.

In this case IntelliJ IDEA will create a Scala file with the converted code.

Decompile Scala code to Java

You can decompile your Scala code to Java to see how a certain piece of code is translated and implemented in Java.

  1. In the Project tool window, right-click a Scala class that you want to decompile.

  2. From the context menu, select Decompile Scala to Java.

IntelliJ IDEA converts code to Java and opens the converted file in the editor.

You can also open a Scala class in the editor and use its context menu for the conversion.

Scala generate actions

Use Alt+Insert to generate actions such as override, delegate, or implement methods.

Generate action menu

For example, select the Override methods action. IntelliJ IDEA displays a dialog with the list of methods that can be overridden. Select the one you need and click OK.

Override method

The corresponding completion works when you type the override keyword.

Completion for the override action

Scala templates

IntelliJ IDEA lets you use predefined Scala templates.

  1. In the editor start entering your code, press Ctrl+J.
    IntelliJ IDEA displays the list of available Live templates for Scala.

  2. Select the one you need and press Enter.

You can also define a new template or edit the existing one.

  1. Select Settings/Preferences | Editor | Live Templates.

  2. From options on the right, open the list of Scala templates.

    Scala templates settings

  3. If you want to add a new template, click the Add button.
    If you want to edit the existing template, select the one you need and change the default definitions.

Scala postfix completion

You can transform an already typed expression to another one based on the postfix you type after a dot.

To enable/disable the postfix completion or to see a list of postfix-specific predefined templates, their descriptions and code samples, open the Postfix Completion page located in Settings/Preferences | Editor | General.

Scala inspections

You can check the available inspections for Scala on the Inspections page in Settings/Preferences | Editor Ctrl+Alt+S.

Since IntelliJ IDEA also supports Akka, there are several Akka inspections available.

Akka inspections settings
Last modified: 26 April 2020