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

FormatWriter: implement align.delayUntilSpace #3616

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,9 @@ Apart from a few special cases, the way alignment works is as follows:
- for each token that has matches in the surrounding lines:
- we'll determine the amount of extra space needed to be added _before_
that token, to align it _on the right_ with matching tokens
- however, if there was no space before the token, that extra space will be
added to the next space on its line, aligning subsequent token _on the left_.
- however, if there was no space before the token, and `align.delayUntilSpace`
is set, that extra space will be added to the next space on its line, thus
aligning subsequent token _on the left_.

Align has several nested fields, which you can customize. However, it comes with
four possible presets: none, some, more, & most.
Expand Down Expand Up @@ -1441,6 +1442,48 @@ object a {
}
```

### `align.delayUntilSpace`

If this flag is set, the formatter will not forcefully pull apart two successive
non-whitespace tokens that would otherwise be formatted without a space between
them.

Instead, the extra alignment spaces will be added to the next space on the same line.

> Since v3.7.13. Prior to that, this behaviour was always enabled.

```scala mdoc:defaults
align.delayUntilSpace
```

```scala mdoc:scalafmt
align.preset = more
align.delayUntilSpace = true
align.tokens."+" = [ { code = ":" }, { code = "(" }, { code = ")" }, { code = "=" } ]
---
object a {
def meeethod1(pram1: AnyRef): Any = ???
def methd2(paaaaaram2: Any): Any = ???
def meth3(param333333: Any): Any = ???
def md4(param4: Any): Any = ???
}
```

vs

```scala mdoc:scalafmt
align.preset = more
align.delayUntilSpace = false
align.tokens."+" = [ { code = ":" }, { code = "(" }, { code = ")" }, { code = "=" } ]
---
object a {
def meeethod1(pram1: AnyRef): Any = ???
def methd2(paaaaaram2: Any): Any = ???
def meth3(param333333: Any): Any = ???
def md4(param4: Any): Any = ???
}
```

## Newlines

The `newlines.*` options are used to configure when and where `scalafmt` should
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import metaconfig.generic.Surface
*/
case class Align(
allowOverflow: Boolean = false,
delayUntilSpace: Boolean = true,
multiline: Boolean = false,
stripMargin: Boolean = true,
closeParenSite: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ class FormatWriter(formatOps: FormatOps) {

case p: Provided => p.betweenText

case NoSplit =>
case NoSplit if style.align.delayUntilSpace =>
return delayedAlign + currentAlign // RETURNING!

case _ => getIndentation(mod.length + currentAlign + delayedAlign)
Expand Down
21 changes: 20 additions & 1 deletion scalafmt-tests/src/test/resources/align/DefaultWithAlign.stat
Original file line number Diff line number Diff line change
Expand Up @@ -2054,8 +2054,9 @@ object a {
def meth3( param333333: Any ): Any = ???
def md4( param4: Any ): Any = ???
}
<<< #3614 !spaces.inParentheses
<<< #3614 !spaces.inParentheses align.delayUntilSpace
spaces.inParentheses = false
align.delayUntilSpace = true
align.tokens."+" = [ { code = ":" }, { code = "(" }, { code = ")" }, { code = "=" } ]
===
object a {
Expand All @@ -2071,3 +2072,21 @@ object a {
def meth3(param333333: Any): Any = ???
def md4(param4: Any): Any = ???
}
<<< #3614 !spaces.inParentheses !align.delayUntilSpace
spaces.inParentheses = false
align.delayUntilSpace = false
align.tokens."+" = [ { code = ":" }, { code = "(" }, { code = ")" }, { code = "=" } ]
===
object a {
def meeethod1(pram1: AnyRef): Any = ???
def methd2(paaaaaram2: Any): Any = ???
def meth3(param333333: Any): Any = ???
def md4(param4: Any): Any = ???
}
>>>
object a {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I would have thought it would work like this always with alignment, though I don't use so never tested. I guess since this changes the default, we need the additional flag.

def meeethod1(pram1 : AnyRef): Any = ???
def methd2 (paaaaaram2 : Any ): Any = ???
def meth3 (param333333: Any ): Any = ???
def md4 (param4 : Any ): Any = ???
}
Loading