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: use milder expire token for if-else #3423

Merged
merged 2 commits into from
Jan 5, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2014,8 +2014,7 @@ class Router(formatOps: FormatOps) {
)
case FormatToken(_: T.KwIf, right, _) if leftOwner.is[Term.If] =>
val owner = leftOwner.asInstanceOf[Term.If]
val expireTree = if (ifWithoutElse(owner)) owner else owner.elsep
val expire = rhsOptimalToken(tokens.getLast(expireTree))
val expire = getLastToken(owner)
val mod =
if (style.newlines.keepBreak(newlines)) Newline
else Space(style.spaces.isSpaceAfterKeyword(right))
Expand Down Expand Up @@ -2089,7 +2088,7 @@ class Router(formatOps: FormatOps) {
Split(Space, 0)
)
case FormatToken(_, T.KwElse() | T.KwYield(), _) =>
val expire = rhsOptimalToken(tokens.getLast(rightOwner))
val expire = getLastToken(rightOwner)
val noSpace = shouldBreak(formatToken)
def exclude = insideBracesBlock(formatToken, expire)
val noSyntaxNL = formatToken.right.is[T.KwYield]
Expand Down
28 changes: 28 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_classic.stat
Original file line number Diff line number Diff line change
Expand Up @@ -7414,3 +7414,31 @@ class a {
result = result * i * i
})) // result = 2 * 2 * 2 = 8
}
<<< SM 7.4.0: 53
maxColumn = 76
===
object a {
def toJsCmd = """var x=document.getElementById(""" + select.encJs + """);
if (x) {
while (x.length > 0) {x.remove(0);}
var y = null;
""" + opts.map { case (value, text) =>
"y=document.createElement('option'); " + "y.text = " + text.encJs + "; " +
"y.value = " + value.encJs + "; " +
(if (Full(value) == dflt) "y.selected = true; " else "") +
" try {x.add(y, null);} catch(e) {if (typeof(e) == 'object' && typeof(e.number) == 'number' && (e.number & 0xFFFF) == 5){ x.add(y,x.options.length); } } "
}
}
>>>
object a {
def toJsCmd = """var x=document.getElementById(""" + select.encJs + """);
if (x) {
while (x.length > 0) {x.remove(0);}
var y = null;
""" + opts.map { case (value, text) =>
"y=document.createElement('option'); " + "y.text = " + text.encJs + "; " +
"y.value = " + value.encJs + "; " +
(if (Full(value) == dflt) "y.selected = true; " else "") +
" try {x.add(y, null);} catch(e) {if (typeof(e) == 'object' && typeof(e.number) == 'number' && (e.number & 0xFFFF) == 5){ x.add(y,x.options.length); } } "
}
}
31 changes: 29 additions & 2 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -7034,8 +7034,7 @@ class a {
)(in: Input[E]): Iteratee[E, Iteratee[E, A]] = in match {
case Input.El(e) => Iteratee.flatten(
Future(p(e))(pec).map(b =>
if (b) Done(inner, in)
else stepNoBreak(inner)(in)
if (b) Done(inner, in) else stepNoBreak(inner)(in)
)(dec)
)
case _ => stepNoBreak(inner)(in)
Expand Down Expand Up @@ -7072,3 +7071,31 @@ class a {
.register(Witness({ i => result = result * i * i }
)) // result = 2 * 2 * 2 = 8
}
<<< SM 7.4.0: 53
maxColumn = 76
===
object a {
def toJsCmd = """var x=document.getElementById(""" + select.encJs + """);
if (x) {
while (x.length > 0) {x.remove(0);}
var y = null;
""" + opts.map { case (value, text) =>
"y=document.createElement('option'); " + "y.text = " + text.encJs + "; " +
"y.value = " + value.encJs + "; " +
(if (Full(value) == dflt) "y.selected = true; " else "") +
" try {x.add(y, null);} catch(e) {if (typeof(e) == 'object' && typeof(e.number) == 'number' && (e.number & 0xFFFF) == 5){ x.add(y,x.options.length); } } "
}
}
>>>
object a {
def toJsCmd = """var x=document.getElementById(""" + select.encJs + """);
if (x) {
while (x.length > 0) {x.remove(0);}
var y = null;
""" + opts.map { case (value, text) =>
"y=document.createElement('option'); " + "y.text = " + text.encJs +
"; " + "y.value = " + value.encJs + "; " +
(if (Full(value) == dflt) "y.selected = true; " else "") +
" try {x.add(y, null);} catch(e) {if (typeof(e) == 'object' && typeof(e.number) == 'number' && (e.number & 0xFFFF) == 5){ x.add(y,x.options.length); } } "
}
}
28 changes: 28 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -7396,3 +7396,31 @@ class a {
result = result * i * i
})) // result = 2 * 2 * 2 = 8
}
<<< SM 7.4.0: 53
maxColumn = 76
===
object a {
def toJsCmd = """var x=document.getElementById(""" + select.encJs + """);
if (x) {
while (x.length > 0) {x.remove(0);}
var y = null;
""" + opts.map { case (value, text) =>
"y=document.createElement('option'); " + "y.text = " + text.encJs + "; " +
"y.value = " + value.encJs + "; " +
(if (Full(value) == dflt) "y.selected = true; " else "") +
" try {x.add(y, null);} catch(e) {if (typeof(e) == 'object' && typeof(e.number) == 'number' && (e.number & 0xFFFF) == 5){ x.add(y,x.options.length); } } "
}
}
>>>
object a {
def toJsCmd = """var x=document.getElementById(""" + select.encJs + """);
if (x) {
while (x.length > 0) {x.remove(0);}
var y = null;
""" + opts.map { case (value, text) =>
"y=document.createElement('option'); " + "y.text = " + text.encJs + "; " +
"y.value = " + value.encJs + "; " +
(if (Full(value) == dflt) "y.selected = true; " else "") +
" try {x.add(y, null);} catch(e) {if (typeof(e) == 'object' && typeof(e.number) == 'number' && (e.number & 0xFFFF) == 5){ x.add(y,x.options.length); } } "
}
}
33 changes: 33 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -7634,3 +7634,36 @@ class a {
})
) // result = 2 * 2 * 2 = 8
}
<<< SM 7.4.0: 53
maxColumn = 76
===
object a {
def toJsCmd = """var x=document.getElementById(""" + select.encJs + """);
if (x) {
while (x.length > 0) {x.remove(0);}
var y = null;
""" + opts.map { case (value, text) =>
"y=document.createElement('option'); " + "y.text = " + text.encJs + "; " +
"y.value = " + value.encJs + "; " +
(if (Full(value) == dflt) "y.selected = true; " else "") +
" try {x.add(y, null);} catch(e) {if (typeof(e) == 'object' && typeof(e.number) == 'number' && (e.number & 0xFFFF) == 5){ x.add(y,x.options.length); } } "
}
}
>>>
object a {
def toJsCmd =
"""var x=document.getElementById(""" + select.encJs + """);
if (x) {
while (x.length > 0) {x.remove(0);}
var y = null;
""" +
opts.map { case (value, text) =>
"y=document.createElement('option'); " + "y.text = " + text.encJs +
"; " + "y.value = " + value.encJs + "; " +
(if (Full(value) == dflt)
"y.selected = true; "
else
"") +
" try {x.add(y, null);} catch(e) {if (typeof(e) == 'object' && typeof(e.number) == 'number' && (e.number & 0xFFFF) == 5){ x.add(y,x.options.length); } } "
}
}