Indentation in IntelliJ Scala mode


#1

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.


#2

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

regards,
Siddhartha


#3

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.


#4


#5

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.


#6

@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.


#7

@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.


#8

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)
}


#9

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


#10

@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.


#11

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


#12

image.png


#13

this looks nice :slightly_smiling_face:


#14

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