Scala logo

Scala Plugin

Scala Plugin for IntelliJ IDEA and Android Studio

Features News Scala Scala programming

IntelliJ Scala Plugin 2020.2: Indentation-based Brace Handling

Because Scala is a curly-bracket language, adding and removing curly braces is an integral part of Scala programmers’ everyday life. Those actions compound, so every improvement in this area matters. To understand the new advances, let us first take a take a look at it from historical perspective (or just watch this).

Before

At the beginning, there was no IDE assistance, so you had to tinker with curly braces manually:

Not only did this require more keystrokes, but the code was not valid for long. It was not good…

But now, thanks to Editor | General | Smart Keys | Insert paired brackets, IntelliJ IDEA can add a complementary brace automatically:

However, you have to do this in advance, as it does not work for already existing code:

In such cases, you are right back to square one.

As a workaround, you could use the Add braces quick fix (or select the expression and press {):

But you have to know about these features in the first place, and it requires an explicit action on your part.

Fortunately, in Scala plugin 2019.2 we have introduced automatic handling of complementary braces (Editor | General | Smart Keys | Scala | Wrap single expression):

A complementary brace can be added and removed automatically, regardless of existing code in between. (By the way, we received a lot of overwhelmingly positive feedback for this supposedly small feature.) But you still had to add or remove a single brace… that is… until now.

After

Now we have taken it even further – both braces can be added and removed automatically and on the fly based on code indents:

With this, you can make use of significant indentation while still keeping the braces, even in Scala 2.x. To take advantage of this feature, just don’t think about the braces and let the IDE do the work. And it’s not just a catchphrase – to use the new feature, you must not use the legacy ones: don’t add a pair of {}, don’t invoke the "wrap" quick fix, and don’t insert an opening { manually.

The plugin can correctly handle appending or inserting a new line, keywords, and so on (and if braces are not needed, you can either press Enter twice or press Backspace to delete the indent):

At the moment, the plugin doesn’t remove braces by default, but you can enable it in Editor | General | Smart Keys | Scala.

To try this now, select Nightly builds as your Settings | Languages | Scala | Updates | Channel (you can revert to a more stable build in the same way at any time). We’re announcing this feature early, before the official release, in order to gather more information and polish off the rough edges. We need your feedback! Please report any bugs to YouTrack. If you have any questions, feel free to ask them in our Gitter.