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

Newlines: add method okSpaceForSource(newlines) #3909

Merged
merged 1 commit into from
Apr 6, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ case class Newlines(
@inline
def sourceIgnored: Boolean = source.ignoreSourceSplit

def okSpaceForSource(newlines: Int, forFold: => Boolean = true): Boolean =
source match {
case Newlines.fold => forFold
case Newlines.unfold => false
case _ => newlines == 0
}

@inline
def keepBreak(newlines: => Int): Boolean = source.eq(Newlines.keep) &&
newlines != 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1275,21 +1275,11 @@ class FormatOps(
): (Boolean, NewlineT) = style.newlines.afterCurlyLambdaParams match {
case Newlines.AfterCurlyLambdaParams.squash => (true, Newline)
case Newlines.AfterCurlyLambdaParams.never =>
val space = style.newlines.source match {
case Newlines.fold => true
case Newlines.unfold => false
case _ => newlines == 0
}
(space, Newline)
(style.newlines.okSpaceForSource(newlines), Newline)
case Newlines.AfterCurlyLambdaParams.always => (false, Newline2x)
case Newlines.AfterCurlyLambdaParams.preserve =>
val blanks = newlines >= 2
val space = style.newlines.source match {
case Newlines.fold => !blanks
case Newlines.unfold => false
case _ => newlines == 0
}
(space, Newline2x(blanks))
(style.newlines.okSpaceForSource(newlines, !blanks), Newline2x(blanks))
}

def getNoSplit(ft: FormatToken, spaceOk: Boolean)(implicit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1962,14 +1962,15 @@ class Router(formatOps: FormatOps) {
Seq(Split(Space.orNL(!nlOnly), 0))

case FormatToken(T.RightBrace(), T.KwYield(), _) => Seq(Split(Space, 0))
case FormatToken(_, kw @ (_: T.KwElse | _: T.KwYield), _) =>
val expire = getLastToken(rightOwner)
val noSpace = shouldBreak(formatToken)
def exclude = insideBracesBlock(formatToken, expire)
val noSyntaxNL = kw.is[T.KwYield]
Seq(
Split(Space, 0).notIf(noSpace)
.withSingleLineNoOptimal(expire, exclude, noSyntaxNL = noSyntaxNL),
case FormatToken(_, kw @ (_: T.KwElse | _: T.KwYield), _) => Seq(
if (style.newlines.okSpaceForSource(newlines)) {
val expire = getLastToken(rightOwner)
Split(Space, 0).withSingleLineNoOptimal(
expire,
exclude = insideBracesBlock(formatToken, expire),
noSyntaxNL = kw.is[T.KwYield],
)
} else Split.ignored,
Split(Newline, 1),
)
case ft @ FormatToken(_, _: T.KwThen | _: T.KwDo, _) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.scalafmt.util

import org.scalafmt.config.Newlines
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.internal.FormatToken
import org.scalafmt.internal.Modification
Expand Down Expand Up @@ -138,13 +137,6 @@ object TokenOps {
!head.isLetter && head != '_'
}

def shouldBreak(ft: FormatToken)(implicit style: ScalafmtConfig): Boolean =
style.newlines.source match {
case Newlines.classic | Newlines.keep => ft.hasBreak
case Newlines.fold => false
case Newlines.unfold => true
}

def getXmlLastLineIndent(tok: Xml.Part): Option[Int] = {
val part = tok.value
val afterLastNL = part.lastIndexOf('\n') + 1
Expand Down
Loading