scala-ide / scalariform

Scala source code formatter
http://scala-ide.github.com/scalariform/
MIT License
527 stars 148 forks source link

Support trailing commas in parameter declarations. Include newline after trailing comma. #296

Open hubertta opened 1 year ago

hubertta commented 1 year ago

Hi, the initial trailing comma support (#262) is very limited. Even the "expected" code in the test case is not valid (it expects the trailing comma in the same line as closing paren which is forbidden in Scala). The other problem with current master is that whenever the trailing comma appears in parameter declaration (eg. case class fields or function declaration), an assertion fails with java.lang.IllegalArgumentException: requirement failed: Parse tokens differ from expected. (SpecificFormatter.scala:54).

My commit fixes both of these issues.

  1. The assert is fixed by adding the trailing comma token back to the tokens field in classes ImportClause and ParamClause in AstNodes.scala. This also prevents the trailing comma from disappearing in the formatted code.
  2. I enforce a newline after a trailing comma even if the preference DanglingCloseParenthesis would not normally result in a newline. This is to prevent Scala compiler errors.

I added a bunch of test cases.

Please let me know if you can merge this and make a new release, so that this fix could be propagated to sbt-scalariform.