Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij Scala class definition formatting

How can I get Intellij format my Scala class definition like this:

sealed class Traffic(
    private[this] val javaTraffic: Traffic.JavaTraffic,
    private[this] val sanitizer: Sanitizer)
  extends Serializable with Logger {

Basically 4 indents for member declaration on each line, and 2 indents for class inheritance.

like image 804
elgoog Avatar asked Dec 03 '15 22:12

elgoog


People also ask

How can I beautify code in IntelliJ?

Go to Settings/Preferences | Editor | Code Style, select your programming language, and open the Wrapping and Braces tab. In the Keep when reformatting section, select the formatting rules which you want to ignore and deselect those which should be applied. Reformat your code ( Ctrl+Alt+L ).

How do I enable auto format in IntelliJ?

You can use ⌘⌥L (macOS), or Ctrl+Alt+L (Windows/Linux) to reformat a selection of code according to your reformat settings. You can also display the Reformat File dialog with ⌥⇧⌘L (macOS), or Ctrl+Alt+Shift+L (Windows/Linux).

How do I format a Scala code?

From the main menu, select Code | Reformat Ctrl+Alt+L . If you need to add additional options before reformatting, from the main menu, select Code | Reformat File Ctrl+Alt+Shift+L and in the Reformat File dialog make the required settings.


1 Answers

Starting from version 2016.2 this can be done by making the next changes from the default under Settings > Editor > Code Style > Scala:

  1. Uncheck Method declaration parameters > Align when multiline under Wrapping and Braces.
  2. Check Alternate indentation for constructor args and parameter declarations under Other and set it to 4.

Downside is that this applies not only to constructor parameters, but to normal method declarations as well, so you can't have method params aligned with opening parenthesis. But I guess we'll have to live with that.

like image 93
Vasiliy Ivashin Avatar answered Sep 20 '22 12:09

Vasiliy Ivashin