Repository: scalameta/scalameta (scala)

Issue:
Anonymous class without braces in if block with braces fails to parse
Related https://github.com/scalameta/metals/pull/4877

Scalameta >= 4.7.2 fails to parse the following program with Scala3 dialect

```scala
if(x) {
  new TreeMap:
    override def foo = ???
}
```

while the following works 

```scala
if(x)
  new TreeMap:
    override def foo = ???
```


```
scala> import scala.meta._
import scala.meta._

scala> val program = """
     | if(x) {
     |   new TreeMap:
     |     def foo = ???
     | }
     | """
val program: String =
"
if(x) {
  new TreeMap:
    def foo = ???
}
"

scala> dialects.Scala3(program).parse[Stat]
val res0: scala.meta.parsers.Parsed[scala.meta.Stat] =
<input>:3: error: expected template body
  new TreeMap:
              ^

```

Pull request description:
fix https://github.com/scalameta/scalameta/issues/3093

It seems like we should accept the optional braces around template body when we accept `:`, even inside the block with braces.

I'm trying to confirm this is valid change from syntax specification https://docs.scala-lang.org/scala3/reference/other-new-features/indentation.html (that's why it's still draft)...

Patch:
diff --git a/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala b/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
index 840a1682bd..d66aead52a 100644
--- a/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
+++ b/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
@@ -4175,14 +4175,13 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>
     } else if (isColonEol()) {
       accept[Colon]
 
-      val nextIndented =
-        if (enumCaseAllowed)
-          in.observeIndentedEnum()
-        else
-          in.observeIndented()
+      if (enumCaseAllowed)
+        in.observeIndentedEnum()
+      else
+        in.observeIndented()
 
-      if (nextIndented)
-        indentedOnOpen(templateStatSeq(enumCaseAllowed, secondaryConstructorAllowed))
+      if (acceptOpt[Indentation.Indent])
+        indentedAfterOpen(templateStatSeq(enumCaseAllowed, secondaryConstructorAllowed))
       else if (!enumCaseAllowed && isEndMarkerIntro(tokenPos))
         (selfEmpty(), Nil)
       else


Test patch:
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/SignificantIndentationSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/SignificantIndentationSuite.scala
index 84891a4cd3..e529dc90a1 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/SignificantIndentationSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/SignificantIndentationSuite.scala
@@ -104,6 +104,35 @@ class SignificantIndentationSuite extends BaseDottySuite {
     )
   }
 
+  // https://github.com/scalameta/scalameta/issues/3093
+  test("anonymous-class-in-block") {
+    val code = """|if(x) {
+                  |  new A:
+                  |    def f: Int
+                  |}
+                  |""".stripMargin
+    runTestAssert[Stat](code, assertLayout = None)(
+      Term.If(
+        Term.Name("x"),
+        Term.Block(
+          List(
+            Term.NewAnonymous(
+              Template(
+                Nil,
+                List(Init(Type.Name("A"), Name(""), Nil)),
+                Self(Name(""), None),
+                List(Decl.Def(Nil, Term.Name("f"), Nil, Type.Name("Int"))),
+                Nil
+              )
+            )
+          )
+        ),
+        Lit.Unit(),
+        Nil
+      )
+    )
+  }
+
   test("empty-anonymous-class") {
     val code = """|new:
                   |  def f: Int