-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe9a85a
commit 067d1da
Showing
5 changed files
with
279 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
mtags/src/main/scala-3/scala/meta/internal/pc/completions/InferExpectedType.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package scala.meta.internal.pc | ||
|
||
import scala.meta.internal.metals.ReportContext | ||
import scala.meta.internal.mtags.MtagsEnrichments.* | ||
import scala.meta.internal.pc.completions.InterCompletionType | ||
import scala.meta.internal.pc.printer.MetalsPrinter | ||
import scala.meta.pc.OffsetParams | ||
import scala.meta.pc.SymbolSearch | ||
|
||
import dotty.tools.dotc.interactive.Interactive | ||
import dotty.tools.dotc.interactive.InteractiveDriver | ||
|
||
class InferExpectedType( | ||
search: SymbolSearch, | ||
driver: InteractiveDriver, | ||
params: OffsetParams | ||
)(implicit rc: ReportContext): | ||
val uri = params.uri | ||
|
||
val sourceFile = CompilerInterfaces.toSource(params.uri, params.text()) | ||
driver.run(uri, sourceFile) | ||
|
||
val ctx = driver.currentCtx | ||
val pos = driver.sourcePosition(params) | ||
|
||
def infer() = | ||
driver.compilationUnits.get(uri) match | ||
case Some(unit) => | ||
val path = | ||
Interactive.pathTo(driver.openedTrees(uri), pos)(using ctx) | ||
val newctx = ctx.fresh.setCompilationUnit(unit) | ||
val tpdPath = | ||
Interactive.pathTo(newctx.compilationUnit.tpdTree, pos.span)(using | ||
newctx | ||
) | ||
val locatedCtx = | ||
Interactive.contextOfPath(tpdPath)(using newctx) | ||
val indexedCtx = IndexedContext(locatedCtx) | ||
val printer = MetalsPrinter.standard( | ||
indexedCtx, | ||
search, | ||
includeDefaultParam = MetalsPrinter.IncludeDefaultParam.ResolveLater, | ||
) | ||
InterCompletionType.inferType(path)(using newctx).map{ | ||
tpe => printer.tpe(tpe) | ||
} | ||
case None => None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
209 changes: 209 additions & 0 deletions
209
tests/cross/src/test/scala/tests/InferExpectedTypeSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
package tests | ||
|
||
import java.nio.file.Paths | ||
|
||
import scala.meta.internal.metals.CompilerOffsetParams | ||
import scala.meta.internal.metals.EmptyCancelToken | ||
import scala.meta.internal.mtags.MtagsEnrichments._ | ||
|
||
import munit.TestOptions | ||
|
||
class InferExpectedTypeSuite extends BasePCSuite { | ||
override protected def ignoreScalaVersion: Option[IgnoreScalaVersion] = Some( | ||
IgnoreScala2 | ||
) | ||
|
||
def check( | ||
name: TestOptions, | ||
original: String, | ||
expectedType: String, | ||
fileName: String = "A.scala" | ||
): Unit = test(name) { | ||
presentationCompiler.restart() | ||
val (code, offset) = params(original.replace("@@", "CURSOR@@"), fileName) | ||
val offsetParams = CompilerOffsetParams( | ||
Paths.get(fileName).toUri(), | ||
code, | ||
offset, | ||
EmptyCancelToken | ||
) | ||
presentationCompiler.inferExpectedType(offsetParams).get().asScala match { | ||
case Some(value) => assertNoDiff(value, expectedType) | ||
case None => fail("Empty result.") | ||
} | ||
} | ||
|
||
check( | ||
"type-ascription", | ||
"""|def doo = (@@ : Double) | ||
|""".stripMargin, | ||
"""|Double | ||
|""".stripMargin | ||
) | ||
// some structures | ||
|
||
check( | ||
"try", | ||
"""|val _: Int = | ||
| try { | ||
| @@ | ||
| } catch { | ||
| case _ => | ||
| } | ||
|""".stripMargin, | ||
"""|Int | ||
|""".stripMargin | ||
) | ||
|
||
check( | ||
"try-catch", | ||
"""|val _: Int = | ||
| try { | ||
| } catch { | ||
| case _ => @@ | ||
| } | ||
|""".stripMargin, | ||
"""|Int | ||
|""".stripMargin | ||
) | ||
|
||
check( | ||
"if-condition", | ||
"""|val _ = if @@ then 1 else 2 | ||
|""".stripMargin, | ||
"""|Boolean | ||
|""".stripMargin | ||
) | ||
|
||
check( | ||
"inline-if", | ||
"""|inline def o: Int = inline if ??? then @@ else ??? | ||
|""".stripMargin, | ||
"""|Int | ||
|""".stripMargin | ||
) | ||
|
||
// pattern matching | ||
|
||
check( | ||
"pattern-match", | ||
"""|val _ = | ||
| List(1) match | ||
| case @@ | ||
|""".stripMargin, | ||
"""|List[Int] | ||
|""".stripMargin | ||
) | ||
|
||
check( | ||
"bind", | ||
"""|val _ = | ||
| List(1) match | ||
| case name @ @@ | ||
|""".stripMargin, | ||
"""|List[Int] | ||
|""".stripMargin | ||
) | ||
|
||
check( | ||
"alternative", | ||
"""|val _ = | ||
| List(1) match | ||
| case Nil | @@ | ||
|""".stripMargin, | ||
"""|List[Int] | ||
|""".stripMargin | ||
) | ||
|
||
check( | ||
"unapply".ignore, | ||
"""|val _ = | ||
| List(1) match | ||
| case @@ :: _ => | ||
|""".stripMargin, | ||
"""|Int | ||
|""".stripMargin | ||
) | ||
|
||
// generic functions | ||
|
||
check( | ||
"any-generic", | ||
"""|val _ : List[Int] = identity(@@) | ||
|""".stripMargin, | ||
"""|List[Int] | ||
|""".stripMargin | ||
) | ||
|
||
check( | ||
"eq-generic", | ||
"""|def eq[T](a: T, b: T): Boolean = ??? | ||
|val _ = eq(1, @@) | ||
|""".stripMargin, | ||
"""|Int | ||
|""".stripMargin | ||
) | ||
|
||
check( | ||
"flatmap".ignore, | ||
"""|val _ : List[Int] = List().flatMap(_ => @@) | ||
|""".stripMargin, | ||
"""|IterableOnce[Int] | ||
|""".stripMargin | ||
) | ||
|
||
check( | ||
"for-comprehension".ignore, | ||
"""|val _ : List[Int] = | ||
| for { | ||
| _ <- List("a", "b") | ||
| } yield @@ | ||
|""".stripMargin, | ||
"""|Int | ||
|""".stripMargin | ||
) | ||
|
||
// bounds | ||
check( | ||
"any".ignore, | ||
"""|trait Foo | ||
|def foo[T](a: T): Boolean = ??? | ||
|val _ = foo(@@) | ||
|""".stripMargin, | ||
"""|<: Any | ||
|""".stripMargin | ||
) | ||
|
||
check( | ||
"bounds-1".ignore, | ||
"""|trait Foo | ||
|def foo[T <: Foo](a: Foo): Boolean = ??? | ||
|val _ = foo(@@) | ||
|""".stripMargin, | ||
"""|<: Foo | ||
|""".stripMargin | ||
) | ||
|
||
check( | ||
"bounds-2".ignore, | ||
"""|trait Foo | ||
|def foo[T :> Foo](a: Foo): Boolean = ??? | ||
|val _ = foo(@@) | ||
|""".stripMargin, | ||
"""|:> Foo | ||
|""".stripMargin | ||
) | ||
|
||
check( | ||
"bounds-3".ignore, | ||
"""|trait A | ||
|class B extends A | ||
|class C extends B | ||
|def roo[F >: C <: A](f: F) = ??? | ||
|val kjk = roo(@@) | ||
|""".stripMargin, | ||
"""|>: C <: A | ||
|""".stripMargin | ||
) | ||
|
||
} |