Skip to content

Commit

Permalink
Use .allowQuietSyntax for if-then/while-do
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Apr 28, 2024
1 parent 070e592 commit ce4ba0d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) {
}

private def condExprInParens[T <: Token: ClassTag]: Term =
if (dialect.allowSignificantIndentation) {
if (dialect.allowQuietSyntax) {
val startPos = tokenPos
val simpleExpr = condExpr()
tryParse {
Expand Down Expand Up @@ -1496,7 +1496,7 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) {
if (acceptOpt[LeftBrace]) inBracesAfterOpen(enumerators())
else if (token.is[LeftParen]) {
def parseInParens() = inParensOnOpen(enumerators())
if (dialect.allowSignificantIndentation)
if (dialect.allowQuietSyntax)
// Dotty retry in case of `for (a,b) <- list1.zip(list2) yield (a, b)`
tryParse(Try(parseInParens()).toOption).getOrElse(enumerators())
else parseInParens()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,8 @@ final class ScannerTokens(val tokens: Tokens)(implicit dialect: Dialect) {
case _ => sepRegions
})
case _: KwFor => currRef(RegionFor(next) :: sepRegions)
case _: KwWhile if dialect.allowSignificantIndentation =>
currRef(RegionWhile(next) :: sepRegions)
case _: KwIf if dialect.allowSignificantIndentation =>
case _: KwWhile if dialect.allowQuietSyntax => currRef(RegionWhile(next) :: sepRegions)
case _: KwIf if dialect.allowQuietSyntax =>
currRef(dropRegionLine(sepRegions) match {
case rs @ (_: RegionCaseExpr | _: RegionFor) :: _ => rs
case rs @ (_: RegionDelim) :: (_: RegionFor) :: _ => rs
Expand All @@ -541,7 +540,7 @@ final class ScannerTokens(val tokens: Tokens)(implicit dialect: Dialect) {
case (_: RegionIf) :: rs => currRef(RegionThen :: rs)
case _ => currRef(RegionThen :: sepRegions)
}
case _: KwElse if dialect.allowSignificantIndentation =>
case _: KwElse if dialect.allowQuietSyntax =>
dropRegionLine(sepRegions) match {
case (r: RegionIndent) :: RegionThen :: rs => outdentThenCurrRef(r, rs)
case (_: RegionControl) :: rs => currRef(rs)
Expand Down Expand Up @@ -588,7 +587,7 @@ final class ScannerTokens(val tokens: Tokens)(implicit dialect: Dialect) {
case _: Dot | _: KwMatch => rc.asCond() :: rs
// might continue cond or start body
case _: Ident | _: LeftBrace | _: LeftBracket | _: LeftParen | _: Underscore
if dialect.allowSignificantIndentation => rc.asCondOrBody() :: rs
if dialect.allowQuietSyntax => rc.asCondOrBody() :: rs
case _ => rc.asBody().fold(rs)(_ :: rs)
}
case RegionForMaybeParens if prev.is[RightParen] =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ trait LegacyTokenData {
case ENUM if !dialect.allowEnums =>
case GIVEN if !dialect.allowGivenUsing =>
case EXPORT if !dialect.allowExportClause =>
case THEN if !dialect.allowSignificantIndentation =>
case THEN if !dialect.allowQuietSyntax =>
case TYPELAMBDAARROW if !dialect.allowTypeLambdas =>
case CTXARROW if !dialect.allowGivenUsing =>
case x =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ package object tokenizers {
dialectKeywords += "given"
dialectKeywords += "?=>"
}
if (dialect.allowSignificantIndentation) dialectKeywords += "then"
if (dialect.allowQuietSyntax) dialectKeywords += "then"

baseKeywords ++ dialectKeywords.result()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3692,10 +3692,23 @@ class ControlSyntaxSuite extends BaseDottySuite {
| openDoor()
|}
|""".stripMargin
val error = """|<input>:3: error: `then` expected but `else` found
| else
| ^""".stripMargin
runTestError[Stat](code, error)
val layout = """|def demo() = {
| if (true) greet() else sayGoodbye()
| openDoor()
|}""".stripMargin
val tree = Defn.Def(
Nil,
tname("demo"),
Nil,
List(Nil),
None,
blk(
Term
.If(lit(true), Term.Apply(tname("greet"), Nil), Term.Apply(tname("sayGoodbye"), Nil), Nil),
Term.Apply(tname("openDoor"), Nil)
)
)
runTestAssert[Stat](code, layout)(tree)
}

test("scalafmt #3941 no significant indentation with quiet syntax: if-then 2") {
Expand All @@ -3709,10 +3722,32 @@ class ControlSyntaxSuite extends BaseDottySuite {
| openDoor()
|}
|""".stripMargin
val error = """|<input>:5: error: `;` expected but `else` found
| else
| ^""".stripMargin
runTestError[Stat](code, error)
val layout = """|def demo() = {
| if (a + b == c + d) greet() else sayGoodbye()
| openDoor()
|}""".stripMargin
val tree = Defn.Def(
Nil,
tname("demo"),
Nil,
List(Nil),
None,
blk(
Term.If(
Term.ApplyInfix(
Term.ApplyInfix(tname("a"), tname("+"), Nil, List(tname("b"))),
tname("=="),
Nil,
List(Term.ApplyInfix(tname("c"), tname("+"), Nil, List(tname("d"))))
),
Term.Apply(tname("greet"), Nil),
Term.Apply(tname("sayGoodbye"), Nil),
Nil
),
Term.Apply(tname("openDoor"), Nil)
)
)
runTestAssert[Stat](code, layout)(tree)
}

test("scalafmt #3941 no significant indentation with quiet syntax: while-do 1") {
Expand Down Expand Up @@ -3751,10 +3786,30 @@ class ControlSyntaxSuite extends BaseDottySuite {
| openDoor()
|}
|""".stripMargin
val error = """|<input>:4: error: do {...} while (...) syntax is no longer supported
| do
| ^""".stripMargin
runTestError[Stat](code, error)
val layout = """|def demo() = {
| while (a + b == c + d) sayGoodbye()
| openDoor()
|}""".stripMargin
val tree = Defn.Def(
Nil,
tname("demo"),
Nil,
List(Nil),
None,
blk(
Term.While(
Term.ApplyInfix(
Term.ApplyInfix(tname("a"), tname("+"), Nil, List(tname("b"))),
tname("=="),
Nil,
List(Term.ApplyInfix(tname("c"), tname("+"), Nil, List(tname("d"))))
),
Term.Apply(tname("sayGoodbye"), Nil)
),
Term.Apply(tname("openDoor"), Nil)
)
)
runTestAssert[Stat](code, layout)(tree)
}

}

0 comments on commit ce4ba0d

Please sign in to comment.