From 18192e36d662f1947d47d135c3361783aed21e13 Mon Sep 17 00:00:00 2001 From: Rikito Taniguchi Date: Tue, 23 Aug 2022 02:39:42 +0900 Subject: [PATCH] fix: fix implement-all members for anonymous class (#4284) fix: https://github.com/scalameta/metals/issues/4232 We had a pc test https://github.com/scalameta/metals/blob/ae5c4650aefb6e6228ad77991c5813c299e860ed/tests/cross/src/test/scala/tests/pc/AutoImplementAbstractMembersSuite.scala#L220-L236 but the error position is different in Scala. `<> Foo` instead of `new <>` This PR fixes the issue, and added a LSP test. --- .../pc/completions/OverrideCompletions.scala | 13 +++++++- .../feature/Scala3CodeActionLspSuite.scala | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/completions/OverrideCompletions.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/completions/OverrideCompletions.scala index bdbb17186ef..59ff85adbb5 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/completions/OverrideCompletions.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/completions/OverrideCompletions.scala @@ -131,15 +131,25 @@ object OverrideCompletions: ): ju.List[l.TextEdit] = object FindTypeDef: def unapply(path: List[Tree])(using Context): Option[TypeDef] = path match + // class <> extends ... {} case (td: TypeDef) :: _ => Some(td) // new Iterable[Int] {} case (_: Ident) :: _ :: (_: Template) :: (td: TypeDef) :: _ => Some(td) - // new Context {} + // given Foo with {} case (_: Ident) :: (_: Template) :: (td: TypeDef) :: _ => Some(td) + // <> Foo {} + case (_: Template) :: (td: TypeDef) :: _ => + Some(td) + // abstract class Mutable { ... } + // new <> { } case (_: Ident) :: (_: New) :: (_: Select) :: (_: Apply) :: (_: Template) :: (td: TypeDef) :: _ => Some(td) + // trait Base[T]: + // extension (x: T) + // ... + // class <>[T] extends Base[Int] case (dd: DefDef) :: (_: Template) :: (td: TypeDef) :: _ if dd.symbol.isConstructor => Some(td) @@ -179,6 +189,7 @@ object OverrideCompletions: config, ) path match + // given <> case (_: Ident) :: (dd: DefDef) :: _ => implementAll(dd).asJava case FindTypeDef(td) => diff --git a/tests/slow/src/test/scala/tests/feature/Scala3CodeActionLspSuite.scala b/tests/slow/src/test/scala/tests/feature/Scala3CodeActionLspSuite.scala index 09ddbf51f2b..95e7298c93b 100644 --- a/tests/slow/src/test/scala/tests/feature/Scala3CodeActionLspSuite.scala +++ b/tests/slow/src/test/scala/tests/feature/Scala3CodeActionLspSuite.scala @@ -405,6 +405,38 @@ class Scala3CodeActionLspSuite expectNoDiagnostics = false, ) + check( + "implement-anonymous-class", + """|package anonymous + | + |trait Foo: + | def foo(x: Int): Int + | def bar(x: String): String + | + |def main = + | <> Foo {} + | + |""".stripMargin, + s"""|${ImplementAbstractMembers.title} + |""".stripMargin, + """|package anonymous + | + |trait Foo: + | def foo(x: Int): Int + | def bar(x: String): String + | + |def main = + | new Foo { + | + | override def foo(x: Int): Int = ??? + | + | override def bar(x: String): String = ??? + | + | } + |""".stripMargin, + expectNoDiagnostics = false, + ) + check( "wrong-type", """|package a