scalameta / scalafmt

Code formatter for Scala
http://scalameta.org/scalafmt
Apache License 2.0
1.42k stars 276 forks source link

Wrong indentation with scala213source3 when short-circuiting boolean operators start a line in final position of a block #4084

Closed ex-ratt closed 1 month ago

ex-ratt commented 2 months ago

Configuration

version = 3.8.2
runner.dialect = scala213source3 

Command-line parameters

When I run scalafmt via CLI like this: scalafmt Test.scala

Steps

Given code like this:

case class Test(condition1: Boolean, condition2: Boolean, condition3: Boolean) {
  def test: Boolean = {
    condition1
      && condition2
      && condition3
  }
}

Problem

Scalafmt formats code like this (changes indentation in lines starting with operators):

case class Test(condition1: Boolean, condition2: Boolean, condition3: Boolean) {
  def test: Boolean = {
    condition1
    && condition2
    && condition3
  }
} 

This happens with operator || as well (but not with other operators).

Expectation

I would not expect the indentation to change. Instead, I would have assumed no change at all.

Workaround

The indentation stays the same if:

kitbellew commented 2 months ago

@ex-ratt does this lead to incorrect code? if not, it works exactly as configured.

ex-ratt commented 2 months ago

Yes, the code still compiles and seems to work as well.

You don't find it odd that the indentation is not consistent across different operators and situations?

kitbellew commented 1 month ago

if you don't mind, i will close this. what you're describing is default yet configurable behavior. the configuration parameters are well documented.

ex-ratt commented 1 month ago

Hi, I don't mind. Had a look through the config to figure out what I was missing. In case someone else stumbles across this:

There is a configuration indentOperator.excludeRegex with a default value of ^(&&|\\|\\|)$, which excludes && and || from indentation. I would not have thought that the default handles these operators different from others, which is why I didn't even think about checking.

My bad. Thanks for pointing this out :)