Skip to content

Commit

Permalink
Router: implement spaces.beforeXxxArgInParens
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Aug 13, 2023
1 parent d67bc88 commit c7bc621
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -470,16 +470,25 @@ class FormatOps(

def insideInfixSplit(
app: Member.Infix,
isBeforeOp: Boolean,
ft: FormatToken
)(implicit style: ScalafmtConfig): Seq[Split] =
app match {
case t: Type.ApplyInfix
if style.spaces.neverAroundInfixTypes.contains(t.op.value) =>
Seq(Split(NoSplit, 0))
case t =>
def useSpace = isBeforeOp ||
style.spaces.beforeInfixArgInParens(app.op.value) ||
!(app.arg match {
case _: Lit.Unit => true
case x: Member.ArgClause if x.values.lengthCompare(1) != 0 => true
case x => isEnclosedInParens(x)
})
val afterInfix = style.breakAfterInfix(t)
if (afterInfix ne Newlines.AfterInfix.keep) {
if (ft.meta.leftOwner ne app.op) Seq(Split(Space, 0))
val spaceMod = Space(useSpace)
if (ft.meta.leftOwner ne app.op) Seq(Split(spaceMod, 0))
else {
val fullInfix = InfixSplits.findEnclosingInfix(app)
val ok = isEnclosedInParens(fullInfix) || fullInfix.parent.forall {
Expand All @@ -488,13 +497,16 @@ class FormatOps(
case _ => true
}
if (ok)
InfixSplits(app, ft, fullInfix).getBeforeLhsOrRhs(afterInfix)
else Seq(Split(Space, 0))
InfixSplits(app, ft, fullInfix)
.getBeforeLhsOrRhs(afterInfix, spaceMod = spaceMod)
else Seq(Split(spaceMod, 0))
}
} else {
// we don't modify line breaks generally around infix expressions
// TODO: if that ever changes, modify how rewrite rules handle infix
Seq(InfixSplits.withNLIndent(Split(getMod(ft), 0))(app, ft))
val mod = getMod(ft)
val modOrNoSplit = if (mod != Space || useSpace) mod else NoSplit
Seq(InfixSplits.withNLIndent(Split(modOrNoSplit, 0))(app, ft))
}
}

Expand Down Expand Up @@ -700,7 +712,8 @@ class FormatOps(

def getBeforeLhsOrRhs(
afterInfix: Newlines.AfterInfix,
newStmtMod: Option[Modification] = None
newStmtMod: Option[Modification] = None,
spaceMod: Modification = Space
): Seq[Split] = {
val beforeLhs = ft.meta.leftOwner ne app.op
val maxPrecedence =
Expand Down Expand Up @@ -768,7 +781,7 @@ class FormatOps(
.withSingleLine(singleLineExpire)
.andPolicyOpt(singleLinePolicy)
.andPolicyOpt(delayedBreak)
val spaceSingleLine = Split(Space, 0)
val spaceSingleLine = Split(spaceMod, 0)
.onlyIf(newStmtMod.isEmpty)
.withSingleLine(singleLineExpire)
.andPolicyOpt(singleLinePolicy)
Expand Down Expand Up @@ -799,7 +812,7 @@ class FormatOps(
.andPolicyOpt(breakAfterClose)
.withIndent(nlIndent)
.withPolicy(nlPolicy)
val singleLineSplit = Split(Space, 0)
val singleLineSplit = Split(spaceMod, 0)
.notIf(noSingleLine)
.withSingleLine(endOfNextOp.fold(close)(_.left))
.andPolicyOpt(breakAfterClose)
Expand All @@ -817,7 +830,7 @@ class FormatOps(
val exclude =
if (breakMany) TokenRanges.empty
else insideBracesBlock(nextFT, expire, true)
Split(ModExt(newStmtMod.getOrElse(Space)), cost)
Split(ModExt(newStmtMod.getOrElse(spaceMod)), cost)
.withSingleLine(expire, exclude)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ class Router(formatOps: FormatOps) {
case Term.Name(name) =>
style.spaces.afterTripleEquals && name == "===" ||
(rightOwner match {
case _: Term.ArgClause =>
style.spaces.beforeApplyArgInParens(name)
case _: Member.ParamClause =>
style.spaces.afterSymbolicDefs && isSymbolicName(name)
case _ => false
Expand Down Expand Up @@ -1944,7 +1946,7 @@ class Router(formatOps: FormatOps) {
Seq(
Split(NoSplit, 0)
)
case FormatToken(op @ T.Ident(_), right, _) if leftOwner.parent.exists {
case FormatToken(op: T.Ident, right, _) if leftOwner.parent.exists {
case unary: Term.ApplyUnary =>
unary.op.tokens.head == op
case _ => false
Expand Down Expand Up @@ -2275,9 +2277,9 @@ class Router(formatOps: FormatOps) {

// Infix operator.
case FormatToken(_: T.Ident, _, FormatToken.LeftOwner(AsInfixOp(app))) =>
insideInfixSplit(app, formatToken)
insideInfixSplit(app, false, formatToken)
case FormatToken(_, _: T.Ident, FormatToken.RightOwner(AsInfixOp(app))) =>
insideInfixSplit(app, formatToken)
insideInfixSplit(app, true, formatToken)

// Case
case FormatToken(_: T.KwCase, _, _)
Expand Down
18 changes: 9 additions & 9 deletions scalafmt-tests/src/test/resources/unit/Apply.stat
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ object a {
>>>
object a {
+()
===()
bar()
=== ()
bar ()
+(baz)
===(baz)
bar(baz)
=== (baz)
bar (baz)
+(baz, qux)
===(baz, qux)
bar(baz, qux)
=== (baz, qux)
bar (baz, qux)
}
<<< #3607 beforeApplyArgInParens=never
spaces.beforeApplyArgInParens = never
Expand Down Expand Up @@ -131,12 +131,12 @@ object a {
>>>
object a {
+()
===()
=== ()
bar()
+(baz)
===(baz)
=== (baz)
bar(baz)
+(baz, qux)
===(baz, qux)
=== (baz, qux)
bar(baz, qux)
}
24 changes: 12 additions & 12 deletions scalafmt-tests/src/test/resources/unit/ApplyInfix.stat
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ object a {
foo + baz
foo === baz
foo bar baz
foo + ()
foo === ()
foo bar ()
foo + (baz)
foo === (baz)
foo bar (baz)
foo + (baz, qux)
foo === (baz, qux)
foo bar (baz, qux)
foo +()
foo ===()
foo bar()
foo +(baz)
foo ===(baz)
foo bar(baz)
foo +(baz, qux)
foo ===(baz, qux)
foo bar(baz, qux)
}
<<< #3607 beforeInfixArgInParens=aftersymbolic
spaces.beforeInfixArgInParens = aftersymbolic
Expand All @@ -269,11 +269,11 @@ object a {
foo bar baz
foo + ()
foo === ()
foo bar ()
foo bar()
foo + (baz)
foo === (baz)
foo bar (baz)
foo bar(baz)
foo + (baz, qux)
foo === (baz, qux)
foo bar (baz, qux)
foo bar(baz, qux)
}

0 comments on commit c7bc621

Please sign in to comment.