Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indentOperator doesn't apply to union and intersection type expressions #3153

Closed
martingd opened this issue Mar 8, 2022 · 5 comments · Fixed by #3164
Closed

indentOperator doesn't apply to union and intersection type expressions #3153

martingd opened this issue Mar 8, 2022 · 5 comments · Fixed by #3164

Comments

@martingd
Copy link

martingd commented Mar 8, 2022

Configuration

version = "3.4.3"
runner.dialect = scala3

newlines.source = keep

indentOperator.excludeRegex = "^(&|\\||&&|\\|\\|)$"
indentOperator.exemptScope = aloneArgOrBody

Problem

Given the above configuration and given this code:

val x =
  1 |
  2 |
  3 |
  4

type T =
  Byte |
  Short |
  Int |
  Long |
  Double

when I run scalafmt via CLI like this: scalafmt test.scala --stdout

The output is:

val x =
  1 |
  2 |
  3 |
  4

type T =
  Byte |
    Short |
    Int |
    Long |
    Double

Expectation

I would like the formatted output to keep its original indentation:

val x =
  1 |
  2 |
  3 |
  4

type T =
  Byte |
  Short |
  Int |
  Long |
  Double

However, it seems the indentOperator configuration only applies to value expressions and not type expressions (e.g., union types).

Workaround

None.

@kitbellew
Copy link
Collaborator

@martingd what if you used scala213 dialect instead? will that be formatted differently?

@martingd
Copy link
Author

martingd commented Mar 8, 2022

@kitbellew Does that make sense? Scala 2 does not have union types? I cannot compile this code with the Scala 2 compiler (even if I wrap it in an object).

% scalac test2.scala
test2.scala:12: error: not found: type |
  Long |

@martingd
Copy link
Author

martingd commented Mar 8, 2022

@kitbellew But I tried anyway:

% scalafmt test2.scala --stdout
object Test {
  val x =
    1 |
    2 |
    3 |
    4

  type T =
    Byte |
    Short |
    Int |
    Long |
    Double

But the code is still not valid Scala 2. Isn't the reason it works that scalafmt sees this as a binary or operator and not a type union?

@kitbellew
Copy link
Collaborator

scalafmt is not a compiler. it uses scalameta to parse the code, and for scala2 the parser produces a different tree which is handled correctly, which you confirmed for me. so, need to change the parser.

@martingd
Copy link
Author

martingd commented Mar 9, 2022

@kitbellew I understand it's not a compiler. :)
I think I misunderstood what it was you would obtain by trying out the scala213 dialect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants