Indentation in IntelliJ Scala mode

Does anyone know whether it is possible to convince the IntelliJ Scala mode to indent (what I would consider as) more reasonably? Here is an example:
50

I would like all the elements of the List(...) to be vertically aligned as follows.
It would like there to never be an ( to the left of an outer ( containing it.

58

Of course I can insert spaces to force the indentation, but as soon as I type a } later, the code reindents. Reindentation is great when it does the right thing, but not when it makes the code less readable.

There is a scalafmt plugin for IntelliJ, and scalafmt can be configured (though personally I am happy with the scalafmt defaults).

regards,
Siddhartha

Note that with recent versions of Intellij’s Scala plugin, you no longer need a separate scalafmt plugin - it’s directly supported by the Scala plugin. It can be configured in Preferences > Editor > Code Style > Scala, where you can choose to use either scalafmt or the Intellij formatter.

1 Like

The requirement that all elements of a List are vertically aligned can be achieved by putting each element, including the first one, on a line all by itself, i.e. make a new line after “Line(”.

The requirement that an open paren must never be to the left of an enclosing open paren seems harder to achieve. I would imagine you can get excessive indentation if you try to apply that consistently.

FWIW, I like to be conformist when it comes to code formatting. I just let IntelliJ auto-format everything.

@curoli, notice in the first image I showed above, when I put a after List( as you suggest, then IntelliJ indents the list items at the same indentation as the variable declarations below it, which is not only wrong but misleading, in my opinion.

@shawjef3, thanks for the pointer. Unfortunately, that option seems buggy.
It does indeed indent List(...) more sanely, as I requested, but it also
sadly indents other stuff really bizarrely.

This is what IntelliJ auto-formats for me by default (I hope this gets delivered unmangled):

def test(): Unit = {
val list = List(
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1)
)
val somethingElse = “yo”
println(list)
println(somethingElse)
}

1 Like

@curoli, Not sure if it was mangled, perhaps you could send a screenshot?

@jimka don’t be afraid to play with the settings. Below the one I showed you, I think you have “Do not align block expression parameters” turned off, which did that for me.

@curoli to include a preformatted block of code, quote it in triple backticks. ``` before the first line and again after the last line.

image.png

1 Like

this looks nice :slightly_smiling_face:

If you have multiple contributors, it is nice to have a consistent format that every one can agree to.