Work with Scala code in the editor
Supported Scala versions: Scala 2 and Scala 3 (Dotty)
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.
You can add different intentions for strings, perform different actions, and set a different format for multi-line strings.
Check the following examples:
note
Use a Scala worksheet to quickly evaluate your results.
Insert gap with concatenation ("+ +") into a string.
Convert a string into a multi-line string using the Convert to """string""" intention and vice versa.
note
Converting to multi-line strings removes escaped sequences such as '\\' or '\n'.
Enter your string, press AltEnter and from the list of intentions, select Convert to """string""".
You also can convert the multi-line string into the regular string. Press AltEnter to open the list of intentions. Select Convert to "string" and press Enter. View the result.
GifTo enter a multi-line string, type triple quotes in your editor. If you press Enter, it will automatically invoke the
stripMargin
method. ThestripMargin
method removes the left-hand part of a multi-line string up to a specified delimiter. The white spaces are also preserved.Gif
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 AltEnter and select the appropriate intention from the list.
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.
GifUse the Multi-line strings 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.
In the main menu, select File | Setting | Editor | Code Style | Scala.
On the Scala page, select the Multi-line strings tab.
Edit the settings and click OK.
Turn simple string into the interpolated one adding a variable reference.
IntelliJ IDEA lets you enable, expand and collapse editor hints for implicit conversions and arguments to help you read your code.
On the main toolbar, select View | Show Implicit Hints.
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.
Alternatively, while in the editor, you can press Ctrl+Alt+Shift+ + 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+Alt+Shift+ - to collapse the hints.
IntelliJ IDEA automatically displays type hints for long expression 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.

In the Settings dialog (CtrlAlt0S), go to Editor | Inlay Hints | Scala.
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.
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 callsSingle Abstract Method (SAM) type instantiations
foreach
/map
/flatMap
/filter
/withFilter
calls via a for-comprehension
In the editor, select the implicits definition and from the context menu, select Find Usages AltF7.
View the result in the Find tool window.
IntelliJ IDEA supports the auto-import for unresolved implicit conversions.
Enter your code in the editor. Place the caret at the unresolved expression and press AltEnter.
In the list of the suggested options, select the conversion to import.
As a result, IntelliJ IDEA adds the necessary import statements.
note
When you place the caret at the unresolved expression, IntelliJ IDEA displays a popup suggesting the importing option. You can disable the popup notification in the Auto Import settings.
IntelliJ IDEA lets you invoke implicit conversion methods and arguments.
Select an expression and press CTRL+Shift+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.
note
IntelliJ IDEA highlights an implicit conversion that was used for the selected expression. If IntelliJ IDEA cannot find the implicit conversion or if it finds more than one match then the list of Introduce Variable opens.
If you need, make the implicit conversion method explicit. Press AltEnter and select Make explicit or Make explicit (Import method).
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:
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:
Place the caret at the method where implicit conversion was used and press CtrlShift0P 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.
note
IntelliJ IDEA highlights the method call where implicit arguments were used.
If IntelliJ IDEA cannot find method calls where implicit parameters were passed, it displays a popup message:
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 CtrlShift0P.
note
If you selected the Show type info on mouse hover after, ms checkbox on the Editor tab in Settings | Languages & Frameworks | Scala, you can navigate with the mouse to a value to see its type information.
You can also see the type information on a value definition. Place the caret at a value definition and press CtrlShift0P:
note
You can use the same shortcuts to see the type information on expressions.
To add a type annotation, highlight the value, press ShiftEnter and from the context menu select Add type annotation to value definition: As a result, the type annotation is added.
GifTo remove the type annotation, press ShiftEnter and select Remove type annotation from value definition.
You can wrap or unwrap expressions in Scala code automatically as you type.
note
To control the editor behavior in Scala, refer to the smart keys settings.
Enter code in the editor.
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.
Gif
You can use code completion for the following actions:
To import classes, press ShiftEnter 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.
GifA name-based type suggestion for parameters. IntelliJ IDEA lets you automatically complete both the name and the type before actually adding a type reference.
Invoke the Convert to interpolated string intention.
You can easily convert a regular string into the interpolated one using code completion after
$
.GifAlternatively, select a value with concatenation in your string, press AltEnter and select Convert to interpolated string.
note
The Convert to formatted string option will get you basic Java formatted 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.
As a result, the compiler checks a pattern match for all possible members of a sealed type.
You can complete code not only inside case clauses, but you can complete the whole case clause as well.
Gif
IntelliJ IDEA lets you create new code elements without declaring them first:
In the editor, type a name of a new code element and press AltEnter.
From the list of intentions, select the one you need.
Press Enter.
IntelliJ IDEA lets you view a structure of your code:
To open the Structure tool window, press Alt07.
To navigate from the Structure tool window to the code item in the editor, press F4.
To highlight the code, press CtrlEnter.
IntelliJ IDEA lets you convert Java code into Scala.
Copy your Java code (expression, method, class) and paste it into a Scala file.
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.
In this case IntelliJ IDEA will create a Scala file with the converted code.
If you open a class that is not in your codebase but comes from one of the dependencies, and the source code of that class is not available, IntelliJ IDEA will ask you if it should try to decompile the JVM bytecode of that class to Java.

Click Decompile to Java to see the result.
Use AltInsert to generate actions such as override, delegate, or implement methods.

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.

The corresponding completion works when you type the override
keyword.

IntelliJ IDEA lets you use predefined Scala templates.
In the editor start entering your code, press Ctrl0J. IntelliJ IDEA displays the list of available Live templates for Scala.
Select the one you need and press Enter.
Gif
You can also define a new template or edit the existing one.
Select Settings | Editor | Live Templates.
From options on the right, open the list of Scala templates.
If you want to add a new template, click
.
If you want to edit the existing template, select the one you need and change the default definitions.
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 | Editor | General.
You can check the available inspections for Scala on the Inspections page in Settings | Editor CtrlAlt0S.
Since IntelliJ IDEA also supports Akka, there are several Akka inspections available.

Just as in the case of many other programming languages supported by IntelliJ IDEA, Search Everywhere is a powerful feature that lets Scala developers navigate their code quickly and effortlessly. You can open it from the top menu: Navigate | Search Everywhere but we would suggest that you go to Settings | Keymap, find Search Everywhere on the list of actions there, and bind it with a simple shortcut of your choice.

By default, Search Everywhere is opened in a popup window and lets you search your codebase by the names of classes (including traits and objects), the names of files, the names of all symbols used in your code (fields, methods, variables, and so on), and by all of them at once. You can also search among actions of IntelliJ IDEA – there are many of them, and this is actually a good way to get to know what IntelliJ IDEA can do for you.
This list can be extended by plugins – for example, the Git plugin adds the ability to search for git references – and by clicking on the icon the top-right corner of the popup window, you can move Search Everywhere to the bottom of the main screen where you can also see how the symbol you search for looks in the code.

Besides regular code completion features available for Scala code, you can enable the Scala code completion based on machine learning. It applies rules learned from the gathered data, which results in better suggestions.
note
The Machine Learning Code Completion plugin must be enabled.
In the Settings dialog (CtrlAlt0S), go to Editor | General | Code Completion.
From the options on the right, under the Machine Learning-Assisted Completion section, select Sort completion suggestions based on machine learning | Scala.
Configure sorting options if needed to see how machine learning affects the order of elements. However, since this feature is experimental, ranking may not change noticeably.
Click OK to save the changes.
Thanks for your feedback!