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

FormatOps: no blank within infix if not enclosed #4370

Merged
merged 3 commits into from
Sep 30, 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 @@ -484,8 +484,10 @@ class FormatOps(
rtidx >= 0 && (atokens(rtidx) eq ft.right) // no rewritten tokens
}
}
val useSpace = ft.noBreak || !okToBreak
val split = Split(if (useSpace) spaceMod else Newline2x(ft), 0)
val mod =
if (ft.noBreak || !okToBreak) spaceMod
else Newline2x(isFullInfixEnclosed && ft.hasBlankLine)
val split = Split(mod, 0)
if (isBeforeOp && isFewerBracesRhs(app.arg)) Seq(split)
else Seq(InfixSplits.withNLIndent(split, app, fullInfix))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2308,8 +2308,7 @@ class Router(formatOps: FormatOps) {
val lastToken = getLastToken(leftOwner.asInstanceOf[Term.ForYield].body)
val indent = Indent(style.indent.main, lastToken, ExpiresOn.After)
if (style.newlines.avoidAfterYield && !rightOwner.is[Term.If]) {
val nextFt = nextNonCommentSameLine(ft)
val noIndent = nextFt.eq(ft) || nextFt.noBreak
val noIndent = !isRightCommentWithBreak(ft)
Seq(Split(Space, 0).withIndent(indent, noIndent))
} else Seq(
// Either everything fits in one line or break on =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9676,3 +9676,62 @@ rdd.map(
{case (id, count) => (count, id)})
>>>
rdd.map({ case (id, count) => (count, id) })
<<< #4133 RedundantParens around infix with an embedded blank lin
maxColumn = 80
rewrite {
rules = [RedundantParens, RedundantBraces]
redundantBraces.maxLines = 100
redundantParens.infixSide = all
}
===
class ClassEmitter {

def genScalaClassConstructor = {

if (useESClass) {
for {
chainProto <- chainProtoWithGlobals
} yield {
(
// Real constructor
js.JSDocConstructor(realCtorDef.head) ::
realCtorDef.tail :::
chainProto :::
(genIdentBracketSelect(prototypeFor(ctorVar), "constructor") := ctorVar) ::

// Inheritable constructor
js.JSDocConstructor(inheritableCtorDef.head) ::
inheritableCtorDef.tail :::
(globalVar(VarField.h, className).prototype := prototypeFor(ctorVar)) :: Nil
)
}
}
}

}
>>>
class ClassEmitter {

def genScalaClassConstructor =

if (useESClass) {
for {
chainProto <- chainProtoWithGlobals
} yield
// Real constructor
js.JSDocConstructor(realCtorDef.head) ::
realCtorDef.tail :::
chainProto :::
(genIdentBracketSelect(
prototypeFor(ctorVar),
"constructor"
) := ctorVar) ::
// Inheritable constructor
js.JSDocConstructor(inheritableCtorDef.head) ::
inheritableCtorDef.tail :::
(globalVar(VarField.h, className).prototype := prototypeFor(
ctorVar
)) :: Nil
}

}
55 changes: 55 additions & 0 deletions scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -9075,3 +9075,58 @@ rdd.map(
{case (id, count) => (count, id)})
>>>
rdd.map({ case (id, count) => (count, id) })
<<< #4133 RedundantParens around infix with an embedded blank lin
maxColumn = 80
rewrite {
rules = [RedundantParens, RedundantBraces]
redundantBraces.maxLines = 100
redundantParens.infixSide = all
}
===
class ClassEmitter {

def genScalaClassConstructor = {

if (useESClass) {
for {
chainProto <- chainProtoWithGlobals
} yield {
(
// Real constructor
js.JSDocConstructor(realCtorDef.head) ::
realCtorDef.tail :::
chainProto :::
(genIdentBracketSelect(prototypeFor(ctorVar), "constructor") := ctorVar) ::

// Inheritable constructor
js.JSDocConstructor(inheritableCtorDef.head) ::
inheritableCtorDef.tail :::
(globalVar(VarField.h, className).prototype := prototypeFor(ctorVar)) :: Nil
)
}
}
}

}
>>>
class ClassEmitter {

def genScalaClassConstructor =

if (useESClass) {
for {
chainProto <- chainProtoWithGlobals
} yield
// Real constructor
js.JSDocConstructor(realCtorDef.head) :: realCtorDef.tail :::
chainProto :::
(genIdentBracketSelect(prototypeFor(ctorVar), "constructor") :=
ctorVar) ::
// Inheritable constructor
js.JSDocConstructor(inheritableCtorDef.head) ::
inheritableCtorDef.tail :::
(globalVar(VarField.h, className).prototype :=
prototypeFor(ctorVar)) :: Nil
}

}
60 changes: 60 additions & 0 deletions scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -9473,3 +9473,63 @@ rdd.map(
{case (id, count) => (count, id)})
>>>
rdd.map({ case (id, count) => (count, id) })
<<< #4133 RedundantParens around infix with an embedded blank lin
maxColumn = 80
rewrite {
rules = [RedundantParens, RedundantBraces]
redundantBraces.maxLines = 100
redundantParens.infixSide = all
}
===
class ClassEmitter {

def genScalaClassConstructor = {

if (useESClass) {
for {
chainProto <- chainProtoWithGlobals
} yield {
(
// Real constructor
js.JSDocConstructor(realCtorDef.head) ::
realCtorDef.tail :::
chainProto :::
(genIdentBracketSelect(prototypeFor(ctorVar), "constructor") := ctorVar) ::

// Inheritable constructor
js.JSDocConstructor(inheritableCtorDef.head) ::
inheritableCtorDef.tail :::
(globalVar(VarField.h, className).prototype := prototypeFor(ctorVar)) :: Nil
)
}
}
}

}
>>>
class ClassEmitter {

def genScalaClassConstructor =

if (useESClass) {
for {
chainProto <- chainProtoWithGlobals
} yield
// Real constructor
js.JSDocConstructor(realCtorDef.head) ::
realCtorDef.tail :::
chainProto :::
(genIdentBracketSelect(
prototypeFor(ctorVar),
"constructor"
) := ctorVar) ::
// Inheritable constructor
js.JSDocConstructor(inheritableCtorDef.head) ::
inheritableCtorDef.tail :::
(globalVar(
VarField.h,
className
).prototype := prototypeFor(ctorVar)) :: Nil
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9755,3 +9755,58 @@ rdd.map(
rdd.map({ case (id, count) =>
(count, id)
})
<<< #4133 RedundantParens around infix with an embedded blank lin
maxColumn = 80
rewrite {
rules = [RedundantParens, RedundantBraces]
redundantBraces.maxLines = 100
redundantParens.infixSide = all
}
===
class ClassEmitter {

def genScalaClassConstructor = {

if (useESClass) {
for {
chainProto <- chainProtoWithGlobals
} yield {
(
// Real constructor
js.JSDocConstructor(realCtorDef.head) ::
realCtorDef.tail :::
chainProto :::
(genIdentBracketSelect(prototypeFor(ctorVar), "constructor") := ctorVar) ::

// Inheritable constructor
js.JSDocConstructor(inheritableCtorDef.head) ::
inheritableCtorDef.tail :::
(globalVar(VarField.h, className).prototype := prototypeFor(ctorVar)) :: Nil
)
}
}
}

}
>>>
class ClassEmitter {

def genScalaClassConstructor =

if (useESClass) {
for {
chainProto <- chainProtoWithGlobals
} yield
// Real constructor
js.JSDocConstructor(realCtorDef.head) :: realCtorDef.tail :::
chainProto :::
(genIdentBracketSelect(prototypeFor(ctorVar), "constructor") :=
ctorVar) ::
// Inheritable constructor
js.JSDocConstructor(inheritableCtorDef.head) ::
inheritableCtorDef.tail :::
(globalVar(VarField.h, className).prototype :=
prototypeFor(ctorVar)) :: Nil
}

}
Loading