diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index 2008e7839f..cf869218af 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -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)) @@ -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] diff --git a/scalafmt-tests/src/test/resources/newlines/source_classic.stat b/scalafmt-tests/src/test/resources/newlines/source_classic.stat index 9f241dad56..2b21f9cdc7 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_classic.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_classic.stat @@ -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); } } " + } +} diff --git a/scalafmt-tests/src/test/resources/newlines/source_fold.stat b/scalafmt-tests/src/test/resources/newlines/source_fold.stat index bbe4eec2ba..b318dc3b36 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_fold.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_fold.stat @@ -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) @@ -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); } } " + } +} diff --git a/scalafmt-tests/src/test/resources/newlines/source_keep.stat b/scalafmt-tests/src/test/resources/newlines/source_keep.stat index b836703a1b..67c8deaa59 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_keep.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_keep.stat @@ -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); } } " + } +} diff --git a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat index 6973f54c5e..beff7f351c 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat @@ -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); } } " + } +}