Skip to content

Commit 530f138

Browse files
committed
add completions for specific MatchType cases
1 parent 3c5dbc3 commit 530f138

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

compiler/src/dotty/tools/dotc/interactive/Completion.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import dotty.tools.dotc.util.SourcePosition
2323

2424
import scala.collection.mutable
2525
import scala.util.control.NonFatal
26+
import dotty.tools.dotc.core.Types.MatchType
27+
import dotty.tools.dotc.core.Types.AppliedType
28+
import dotty.tools.dotc.core.Types.TypeRef
29+
import dotty.tools.dotc.core.Types.MatchAlias
2630

2731
/**
2832
* One of the results of a completion query.
@@ -302,6 +306,7 @@ object Completion {
302306
def selectionCompletions(qual: Tree)(using Context): CompletionMap =
303307
implicitConversionMemberCompletions(qual) ++
304308
extensionCompletions(qual) ++
309+
matchTypeCompletions(qual) ++
305310
directMemberCompletions(qual)
306311

307312
/** Completions for members of `qual`'s type.
@@ -362,6 +367,28 @@ object Completion {
362367
implicitConversionTargets(qual)(using ctx.fresh.setExploreTyperState()).flatMap(accessibleMembers)
363368
membersFromConversion.toSeq.groupByName
364369

370+
/** Completions for derived members of `MatchType`'s type. */
371+
def matchTypeCompletions(qual: Tree)(using Context): CompletionMap =
372+
/** Extractor for match types hidden behind an AppliedType/MatchAlias */
373+
object MatchTypeInDisguise {
374+
def unapply(tp: AppliedType): Option[MatchType] = tp match {
375+
case AppliedType(tycon: TypeRef, args) =>
376+
tycon.info match {
377+
case MatchAlias(alias) =>
378+
alias.applyIfParameterized(args) match {
379+
case mt: MatchType => Some(mt)
380+
case _ => None
381+
}
382+
case _ => None
383+
}
384+
case _ => None
385+
}
386+
}
387+
388+
qual.tpe.widenDealias match
389+
case MatchTypeInDisguise(mt) => accessibleMembers(mt.reduced).groupByName
390+
case _ => Map.empty
391+
365392
/** Completions from extension methods */
366393
private def extensionCompletions(qual: Tree)(using Context): CompletionMap =
367394
def asDefLikeType(tpe: Type): Type = tpe match

language-server/test/dotty/tools/languageserver/CompletionTest.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,4 +1121,20 @@ class CompletionTest {
11211121
| val x = Bar.`fo${m1}"""
11221122
.withSource.completion(m1, expected)
11231123
}
1124+
1125+
@Test def matchTypeCompletion: Unit = {
1126+
val expected = Set(
1127+
("map", Method, "[B](f: Int => B): Foo[B]"),
1128+
)
1129+
code"""trait Foo[A] {
1130+
| def map[B](f: A => B): Foo[B]
1131+
|}
1132+
|case class Bar[F[_]](bar: F[Int])
1133+
|type M[T] = T match {
1134+
| case Int => Foo[Int]
1135+
|}
1136+
|def foo(x: Bar[M]) = x.bar.m${m1}"""
1137+
.withSource.completion(m1, expected)
1138+
1139+
}
11241140
}

0 commit comments

Comments
 (0)