Scala logo

Scala Plugin

Scala Plugin for IntelliJ IDEA and Android Studio

The X-Ray Mode

IntelliJ IDEA allows for displaying useful information directly in the editor, next to the code, in the form of inlay hints, that is, text that is slightly different from the actual code, but similar enough to be effortlessly read together with it. You can think of it as augmented code. You can see the line numbers, inferred result types, parameter names, indentation guidelines, and method separators. All that info doesn’t have to be typed explicitly in the Scala code – which is good because the code can be more concise, but on the other hand, if you read it for the first time, you might have trouble understanding what’s going on. In such cases, inlay hints can improve readability and greatly help the developer.

But such functionality comes at a price. Readability is a subjective issue, and instead of appreciating the help, you may feel that the editor is now too crowded and cluttered with unnecessary text. If you’re an experienced developer working on the code you already know, you may find little need for all that additional information displayed on the screen. So you decide to turn it off, and rightfully so. But it may happen after you do it that your work takes you to a place you don’t know so well, and now additional information displayed directly in the editor could be helpful again. So you need to go to the settings and re-enable it.

The constant switching between too little and too much information displayed on the screen and the fact that disabling and re-enabling the hints snaps the developer out of the coding flow is a common problem. The X-Ray mode is a way to solve it. With X-Ray, you can keep inlay hints and all the other additional information disabled or only partially enabled to the point where you feel comfortable, and then press down the Control / Command button two times and hold it – all the other hints will appear, and will be displayed only for the length of time you keep Control / Command down.

The X-Ray mode is also an alternative to two other actions: Show Implicit Hints and Type Info. In both cases, instead of memorizing the shortcuts for the respective actions, you can press Control / Command. On top of that, types displayed in hints are navigable – while pressing Control / Command down, you can hover the cursor over them and click to go to the type’s declaration.

Below, we will go through the types of additional information that X-Ray can display. You can switch them on and off in Settings | Languages & Frameworks | Scala | X-Ray Mode. But remember that each type can also be turned on permanently in Settings | Editor | Inlay Hints and in Settings | Editor | General | Appearance for methods separators and indent guides. If a given type of additional information is turned on permanently, it will be displayed regardless of your X-Ray settings.

Type hints

Type hints are inlay hints showing inferred types of fields, parameters, as well as values created via destructuring, lambda expressions, or stream operations. It might be particularly useful in Scala, where variable types are not always explicitly visible in the code.

Method chain hints

Chaining methods is an idiomatic way of writing complex data transformations in Scala. Method chain hints are a great way to learn intermediate data types as you write a chain of transformations and avoid mistakes. We wrote about this feature in the past.

Implicit hints

Scala supports implicit conversions and parameters, which can significantly reduce boilerplate code. However, this can also make it challenging to understand where values come from or how types are converted. Implicit hints show information about implicit arguments and implicit conversions that the data in question will undergo during compilation – but given their nature, they may render the code less readable. The X-Ray mode may prove to be particularly useful in this case, letting you see implicit hints only for a moment when you want to make sure what they are, and then they will disappear again.

By-name argument hints

It is a very simple hint, but it might be helpful sometimes to differentiate between regular arguments and those passed by name since, in the implementation part of the method, there is no distinction between the two in the Scala code. In such case, X-Ray will show inlay parentheses after a by-name argument.

Parameter name hints

These are the names of parameters taken from the method’s declaration. A parameter name can be valuable additional information about the role of data passed to the method, but writing them down explicitly is usually too wordy. X-Ray might turn out to be a perfect golden middle, letting you see the parameter names only when needed.

Here is a video with examples of all types of hints discussed above:

Indent guides

Indent guides are those delicate vertical lines showing where a given nesting level starts and ends. Together with methods separators, they might be helpful to you if you code in Scala 3 with the new brace-less syntax. But even if you use braces, you may still give it a try.

Methods separators

While indent guides outline nesting levels vertically, methods separators are, simply put, horizontal lines separating methods. Again, you might find it helpful in Scala 3 brace-less projects but also on the opposite end – when you have too many braces in your code.

As always, your feedback is very welcome. Please report any issues you find to YouTrack. If you have any questions, feel free to ask us on Discord.

Happy developing!

The IntelliJ Scala plugin team