antikantian
antikantian

Reputation: 620

How to specify indentations on multiline parameter lists in IntelliJ Scala?

This has been bothering me for a while, but I can't seem to figure out how to change this formatting. Let's take a case class as an example:

How I like my code formatted

I prefer two tabs after a line-continuation; however, IntelliJ seems to force this style:

How IntelliJ wants to format code

This behavior seems to be controlled by Preferences -> Editor -> Code Style -> Scala -> Other -> Alternate indentation for constructor args and parameter declarations, which specifies a minimum of 0 spaces, and that simply brings the arg list inline with the opening parentheses. This isn't a big deal by itself, but whenever I copy/paste blocks of code, it reformats everything and I have to go back and shift-tab ad nauseam. Is there a style field that I'm missing somewhere?

Upvotes: 6

Views: 2374

Answers (5)

cubuspl42
cubuspl42

Reputation: 8410

IntelliJ-only, tested in IntelliJ IDEA 2024.3.3 (Ultimate Edition)

In Settings > Editor > Code Style > Scala...

What worked: Wrapping and Braces > Method declaration parameters > Indent first parameter if on new line set to false (unchecked) [default = true]

What didn't work: Wrapping and Braces > Method declaration parameters > Use normal indent for parameters set to true (checked) [default = false]

Upvotes: 0

Kurt
Kurt

Reputation: 545

I just fought this beast and discovered turning this off provided better results:

enter image description here

Upvotes: 0

tswei
tswei

Reputation: 453

There's an Intellij only solution:

Under Wrapping and Braces, disable Method declaration parameters > Use normal indent for parameters.

Then, under Other, enable Alternate indentation for constructor args and parameter declarations and set to the number of spaces you want to indent from the declaration level (in your case 4).

Upvotes: 10

I See Voices
I See Voices

Reputation: 852

You can also look into scalari-form plugin. It gives you much more and IDE independent. You are particuliary interested in alignArguments=true and if I remember correctly defaults should make the indentation as you want.

*Note, that it formats code after some sbt task, for instance sbt test, not when you press Ctrl+Alt+L or similiar in IntelliJ

Upvotes: 1

Dima
Dima

Reputation: 40510

You want "Align when multiline" checked in the code style settings

enter image description here

Upvotes: 1

Related Questions