Skip to content

Commit

Permalink
ConvertToNewScala3Syntax: use the new parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Dec 24, 2023
1 parent 8db641d commit ab73a5c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,66 +32,70 @@ private class ConvertToNewScala3Syntax(ftoks: FormatTokens)
ft: FormatToken,
style: ScalafmtConfig
): Option[Replacement] = Option {
val flag = style.rewrite.scala3.newSyntax
def left = ftoks.prevNonComment(ft).left
ft.right match {

case _: Token.LeftParen if dialect.allowSignificantIndentation =>
ft.meta.rightOwner match {
case _: Term.If if ftoks.prevNonComment(ft).left.is[Token.KwIf] =>
case _: Term.If if flag.ifCond && left.is[Token.KwIf] =>
removeToken
case _: Term.While
if ftoks.prevNonComment(ft).left.is[Token.KwWhile] =>
case _: Term.While if flag.whileCond && left.is[Token.KwWhile] =>
removeToken
case _: Term.For if ftoks.prevNonComment(ft).left.is[Token.KwFor] =>
case _: Term.For if flag.forEnum && left.is[Token.KwFor] =>
removeToken
case _: Term.ForYield
if ftoks.prevNonComment(ft).left.is[Token.KwFor] =>
case _: Term.ForYield if flag.forEnum && left.is[Token.KwFor] =>
removeToken
case _ => null
}

case _: Token.Colon if dialect.allowPostfixStarVarargSplices =>
ft.meta.rightOwner match {
case t: Term.Repeated if isSimpleRepeated(t) =>
case t: Term.Repeated if flag.varargStar && isSimpleRepeated(t) =>
removeToken // trick: to get "*", just remove ":" and "_"
case _ => null
}

case _: Token.At if dialect.allowPostfixStarVarargSplices =>
ft.meta.rightOwner match {
case Pat.Bind(_, _: Pat.SeqWildcard) =>
case Pat.Bind(_, _: Pat.SeqWildcard) if flag.varargStar =>
removeToken // trick: to get "*", just remove "@" and "_"
case _ => null
}

case _: Token.Underscore =>
ft.meta.rightOwner match {
case _: Importee.Wildcard if dialect.allowStarWildcardImport =>
case _: Importee.Wildcard
if flag.importStar && dialect.allowStarWildcardImport =>
replaceTokenIdent("*", ft.right)
case t: Type.Wildcard
if dialect.allowQuestionMarkAsTypeWildcard &&
if flag.typeWildcardQuestionMark &&
dialect.allowQuestionMarkAsTypeWildcard &&
t.parent.exists(_.is[Type.ArgClause]) =>
replaceTokenIdent("?", ft.right)
case t: Term.Repeated
if dialect.allowPostfixStarVarargSplices && isSimpleRepeated(t) =>
if flag.varargStar && dialect.allowPostfixStarVarargSplices &&
isSimpleRepeated(t) =>
removeToken // see above, under Colon
case t: Pat.SeqWildcard
if dialect.allowPostfixStarVarargSplices &&
if flag.varargStar && dialect.allowPostfixStarVarargSplices &&
t.parent.exists(_.is[Pat.Bind]) =>
removeToken // see above, under At
case _ => null
}

case _: Token.RightArrow if dialect.allowAsForImportRename =>
ft.meta.rightOwner match {
case _: Importee.Rename | _: Importee.Unimport =>
case _: Importee.Rename | _: Importee.Unimport if flag.importAs =>
replaceTokenIdent("as", ft.right)
case _ => null
}

case Token.Ident("*") =>
ft.meta.rightOwner match {
case _: Type.AnonymousParam
if dialect.allowUnderscoreAsTypePlaceholder =>
if flag.typePlaceholderUnderscore &&
dialect.allowUnderscoreAsTypePlaceholder =>
replaceTokenBy("_")(t =>
new Token.Underscore(t.input, t.dialect, t.start)
)
Expand All @@ -106,18 +110,19 @@ private class ConvertToNewScala3Syntax(ftoks: FormatTokens)
ft: FormatToken,
style: ScalafmtConfig
): Option[(Replacement, Replacement)] = Option {
def nextRight = ftoks.nextNonComment(ftoks.next(ft)).right
ft.right match {

case x: Token.RightParen if left.how eq ReplacementType.Remove =>
ft.meta.rightOwner match {
case _: Term.If =>
if (!ftoks.nextNonComment(ftoks.next(ft)).right.is[Token.KwThen])
if (!nextRight.is[Token.KwThen])
replaceToken("then")(
new Token.KwThen(x.input, x.dialect, x.start)
)
else removeToken
case _: Term.While | _: Term.For =>
if (!ftoks.nextNonComment(ftoks.next(ft)).right.is[Token.KwDo])
if (!nextRight.is[Token.KwDo])
replaceToken("do")(new Token.KwDo(x.input, x.dialect, x.start))
else removeToken
case _ => null
Expand Down
13 changes: 8 additions & 5 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1235,8 +1235,8 @@ object a {
object a:
val a =
if a then // scalafmt: { rewrite.scala3.newSyntax.ifCond = false }
if aa then // scalafmt: { rewrite.scala3.newSyntax.ifCond = true }
aaa
if (aa) // scalafmt: { rewrite.scala3.newSyntax.ifCond = true }
aaa
// c1
else b
val a =
Expand Down Expand Up @@ -1300,11 +1300,14 @@ object a:
for (a <- b) yield for a <- b yield foo
while a > 0 do while a > 0 do foo
// scalafmt: { rewrite.scala3.newSyntax.whileCond = false }
while a > 0 do while a > 0 do foo
while (a > 0)
while a > 0 do foo
// scalafmt: { rewrite.scala3.newSyntax.whileCond = true }
for a <- b do for a <- b do foo
// scalafmt: { rewrite.scala3.newSyntax.forEnum = false }
for a <- b do for a <- b do foo
for (a <- b) do
for (a <- b)
foo
// scalafmt: { rewrite.scala3.newSyntax.forEnum = true }
for a <- b do for a <- b do foo
end a
Expand Down Expand Up @@ -1412,7 +1415,7 @@ object a:
import java as j
import Predef.{augmentString as _}
// scalafmt: { rewrite.scala3.newSyntax.importAs = false }
import Predef.{augmentString as _}
import Predef.{augmentString => _}
<<< rewrite to new syntax, imports, scala2-source3
rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.removeOptionalBraces = yes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1162,8 +1162,8 @@ object a {
object a:
val a =
if a then // scalafmt: { rewrite.scala3.newSyntax.ifCond = false }
if aa then // scalafmt: { rewrite.scala3.newSyntax.ifCond = true }
aaa
if (aa) // scalafmt: { rewrite.scala3.newSyntax.ifCond = true }
aaa
// c1
else b
val a =
Expand Down Expand Up @@ -1227,11 +1227,11 @@ object a:
for (a <- b) yield for a <- b yield foo
while a > 0 do while a > 0 do foo
// scalafmt: { rewrite.scala3.newSyntax.whileCond = false }
while a > 0 do while a > 0 do foo
while (a > 0) while a > 0 do foo
// scalafmt: { rewrite.scala3.newSyntax.whileCond = true }
for a <- b do for a <- b do foo
// scalafmt: { rewrite.scala3.newSyntax.forEnum = false }
for a <- b do for a <- b do foo
for (a <- b) do for (a <- b) foo
// scalafmt: { rewrite.scala3.newSyntax.forEnum = true }
for a <- b do for a <- b do foo
end a
Expand Down Expand Up @@ -1339,7 +1339,7 @@ object a:
import java as j
import Predef.{augmentString as _}
// scalafmt: { rewrite.scala3.newSyntax.importAs = false }
import Predef.{augmentString as _}
import Predef.{augmentString => _}
<<< rewrite to new syntax, imports, scala2-source3
rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.removeOptionalBraces = yes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1242,8 +1242,8 @@ object a {
object a:
val a =
if a then // scalafmt: { rewrite.scala3.newSyntax.ifCond = false }
if aa then // scalafmt: { rewrite.scala3.newSyntax.ifCond = true }
aaa
if (aa) // scalafmt: { rewrite.scala3.newSyntax.ifCond = true }
aaa
// c1
else
b
Expand Down Expand Up @@ -1314,17 +1314,17 @@ object a:
while a > 0 do
foo
// scalafmt: { rewrite.scala3.newSyntax.whileCond = false }
while a > 0 do
while a > 0 do
foo
while (a > 0)
while a > 0 do
foo
// scalafmt: { rewrite.scala3.newSyntax.whileCond = true }
for a <- b do
for a <- b do
foo
// scalafmt: { rewrite.scala3.newSyntax.forEnum = false }
for a <- b do
for a <- b do
foo
for (a <- b) do
for (a <- b)
foo
// scalafmt: { rewrite.scala3.newSyntax.forEnum = true }
for a <- b do
for a <- b do
Expand Down Expand Up @@ -1434,7 +1434,7 @@ object a:
import java as j
import Predef.{augmentString as _}
// scalafmt: { rewrite.scala3.newSyntax.importAs = false }
import Predef.{augmentString as _}
import Predef.{augmentString => _}
<<< rewrite to new syntax, imports, scala2-source3
rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.removeOptionalBraces = yes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,8 @@ object a {
object a:
val a =
if a then // scalafmt: { rewrite.scala3.newSyntax.ifCond = false }
if aa then // scalafmt: { rewrite.scala3.newSyntax.ifCond = true }
aaa
if (aa) // scalafmt: { rewrite.scala3.newSyntax.ifCond = true }
aaa
// c1
else
b
Expand Down Expand Up @@ -1396,19 +1396,19 @@ object a:
foo
end while
// scalafmt: { rewrite.scala3.newSyntax.whileCond = false }
while a > 0 do
while a > 0 do
foo
while (a > 0)
while a > 0 do
foo
end while
// scalafmt: { rewrite.scala3.newSyntax.whileCond = true }
for a <- b do
for a <- b do
foo
end for
// scalafmt: { rewrite.scala3.newSyntax.forEnum = false }
for a <- b do
for a <- b do
foo
for (a <- b) do
for (a <- b)
foo
end for
// scalafmt: { rewrite.scala3.newSyntax.forEnum = true }
for a <- b do
Expand Down Expand Up @@ -1554,7 +1554,7 @@ object a:
import java as j
import Predef.{augmentString as _}
// scalafmt: { rewrite.scala3.newSyntax.importAs = false }
import Predef.{augmentString as _}
import Predef.{augmentString => _}
<<< rewrite to new syntax, imports, scala2-source3
rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.removeOptionalBraces = yes
Expand Down

0 comments on commit ab73a5c

Please # to comment.