Skip to content

Instantly share code, notes, and snippets.

@retronym
Created March 10, 2016 23:51
Show Gist options
  • Save retronym/8941b2d9f1f09e7516dd to your computer and use it in GitHub Desktop.
Save retronym/8941b2d9f1f09e7516dd 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)
}
}
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