Skip to content

Commit

Permalink
fix: fix implement-all members for anonymous class (#4284)
Browse files Browse the repository at this point in the history
fix: #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.
`<<new>> Foo` instead of `new <<Foo>>`

This PR fixes the issue, and added a LSP test.
  • Loading branch information
tanishiking authored Aug 22, 2022
1 parent 09d54f3 commit 18192e3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,25 @@ object OverrideCompletions:
): ju.List[l.TextEdit] =
object FindTypeDef:
def unapply(path: List[Tree])(using Context): Option[TypeDef] = path match
// class <<Foo>> 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)
// <<new>> Foo {}
case (_: Template) :: (td: TypeDef) :: _ =>
Some(td)
// abstract class Mutable { ... }
// new <<Mutable>> { }
case (_: Ident) :: (_: New) :: (_: Select) :: (_: Apply) :: (_: Template) :: (td: TypeDef) :: _ =>
Some(td)
// trait Base[T]:
// extension (x: T)
// ...
// class <<Concrete>>[T] extends Base[Int]
case (dd: DefDef) :: (_: Template) :: (td: TypeDef) :: _
if dd.symbol.isConstructor =>
Some(td)
Expand Down Expand Up @@ -179,6 +189,7 @@ object OverrideCompletions:
config,
)
path match
// given <<Foo>>
case (_: Ident) :: (dd: DefDef) :: _ =>
implementAll(dd).asJava
case FindTypeDef(td) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
| <<new>> 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
Expand Down

0 comments on commit 18192e3

Please sign in to comment.