Skip to content

Instantly share code, notes, and snippets.

@ochrons
Forked from retronym/test.scala
Created March 11, 2016 08:50
Show Gist options
  • Save ochrons/9acc78e21b488bf6861f to your computer and use it in GitHub Desktop.
Save ochrons/9acc78e21b488bf6861f to your computer and use it in GitHub Desktop.
import scala.tools.nsc.interactive, scala.tools.nsc.reporters._, scala.tools.nsc._
object Test {
def main(args: Array[String]): Unit = {
val reporter = new StoreReporter
val s = new Settings()
s.processArgumentString("-Ypresentation-any-thread")
val global = new interactive.Global(s, reporter)
import global._
val Cursor = ""//"_DUMMY_ "
def completions(before: String, after: String): List[String] = {
val run = new global.TyperRun
val unit = newCompilationUnit(before + Cursor + after)
val richUnit = new RichCompilationUnit(unit.source)
unitOfFile(richUnit.source.file) = richUnit
val results = global.completionsAt(richUnit.position(before.length))
results.matchingResults().map(_.symNameDropLocal.decoded).distinct
}
def check(actual: => Any, expected: Any) {
try {
if (actual != expected)
println(s"NOK: $actual != $expected")
} catch {
case t: Throwable =>
println("NOK: " + t.getMessage)
}
}
val complex1 =
"""object Test {
|class spanny(s: String) {
| def apply(d: String*) = {}
|}
|lazy val span = new spanny("spa")
|def orange = span
|println(or""".stripMargin
val complex2 =
"""ange()
|}
""".stripMargin
println(complex1 + Console.RED + "|" + Console.RESET + complex2)
check(completions(complex1, complex2), List("orange"))
check(completions("class C { 42.toHex", ""), List("toHexString"))
check(completions("class C { 42.toHex", "Str"), List("toHexString"))
check(completions("class C { 42.toHex", "Str}"), List("toHexString"))
check(completions("class C { 42.toHex", "Str()}"), List("toHexString"))
// keyword completion requires patching in "_CURSOR_ " at the cursor
check(completions("class C { val definitions = 0; this.def", "}"), List("definitions"))
check(completions("object Page { val orange }; class C { Page.o", "range()"), List("orange"))
check(completions("object Page { val orange }; class C { Page.o", "range"), List("orange"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment