summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2015-09-02 13:23:44 +1000
committerJason Zaugg <jzaugg@gmail.com>2015-09-02 20:59:35 +1000
commitf279894a3efe84b85e9becfd0ce96aaa0c21bfbd (patch)
treea6848021a9b1e733d02cffdb45b08da68a4d4a8a /test
parent9a3089bd4ac68db798ac006731ebd1b99e9aaaff (diff)
downloadscala-f279894a3efe84b85e9becfd0ce96aaa0c21bfbd.tar.gz
scala-f279894a3efe84b85e9becfd0ce96aaa0c21bfbd.tar.bz2
scala-f279894a3efe84b85e9becfd0ce96aaa0c21bfbd.zip
Use the presentation compiler to drive REPL tab completion
The old implementation is still avaiable under a flag, but we'll remove it in due course. Design goal: - Push as much code in src/interactive as possible to enable reuse outside of the REPL - Don't entangle the REPL completion with JLine. The enclosed test case drives the REPL and autocompletion programatically. - Don't hard code UI choices, like how to render symbols or how to filter candidates. When completion is requested, we wrap the entered code into the same "interpreter wrapper" synthetic code as is done for regular execution. We then start a throwaway instance of the presentation compiler, which takes this as its one and only source file, and has a classpath formed from the REPL's classpath and the REPL's output directory (by default, this is in memory). We can then typecheck the tree, and find the position in the synthetic source corresponding to the cursor location. This is enough to use the new completion APIs in the presentation compiler to prepare a list of candidates. We go to extra lengths to allow completion of partially typed identifiers that appear to be keywords, e.g `global.def` should offer `definitions`. Two secret handshakes are included; move the the end of the line, type `// print<TAB>` and you'll see the post-typer tree. `// typeAt 4 6<TAB>` shows the type of the range position within the buffer. The enclosed unit test exercises most of the new functionality.
Diffstat (limited to 'test')
-rw-r--r--test/junit/scala/tools/nsc/interpreter/CompletionTest.scala109
1 files changed, 109 insertions, 0 deletions
diff --git a/test/junit/scala/tools/nsc/interpreter/CompletionTest.scala b/test/junit/scala/tools/nsc/interpreter/CompletionTest.scala
new file mode 100644
index 0000000000..70cb2882ba
--- /dev/null
+++ b/test/junit/scala/tools/nsc/interpreter/CompletionTest.scala
@@ -0,0 +1,109 @@
+package scala.tools.nsc.interpreter
+
+import java.io.{StringWriter, PrintWriter}
+
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+import scala.tools.nsc.Settings
+
+class CompletionTest {
+ val EmptyString = "" // def string results include the empty string so that JLine won't insert "def ..." at the cursor
+
+ def newIMain(): IMain = {
+ val settings = new Settings()
+ settings.Xnojline.value = true
+ settings.usejavacp.value = true
+
+ val writer = new StringWriter
+ val out = new PrintWriter(writer)
+ new IMain(settings, out)
+ }
+ @Test
+ def t4438_arrayCompletion(): Unit = {
+ val intp = newIMain()
+ val completer = new PresentationCompilerCompleter(intp)
+ assert(completer.complete("Array(1, 2, 3) rev").candidates.contains("reverseMap"))
+ }
+
+ @Test
+ def completions(): Unit = {
+ val intp = newIMain()
+ val completer = new PresentationCompilerCompleter(intp)
+ checkExact(completer, "object O { def x_y_z = 1 }; import O._; x_y")("x_y_z")
+ checkExact(completer, "object O { private def x_y_z = 1 }; import O._; x_y")()
+ checkExact(completer, "object O { private def x_y_z = 1; x_y", "}")("x_y_z")
+ checkExact(completer, "object x_y_z; import x_y")("x_y_z")
+
+ checkExact(completer, "object x_y_z { def a_b_c }; import x_y_z.a_b")("a_b_c")
+
+ checkExact(completer, "object X { private[this] def definition = 0; def")("definition")
+
+ // stable terms are offered in type completion as they might be used as a prefix
+ checkExact(completer, """object O { def x_y_z = 0; val x_z_y = ""; type T = x_""")("x_z_y")
+ checkExact(completer, """def method { def x_y_z = 0; val x_z_y = ""; type T = x_""")("x_z_y")
+ }
+
+ @Test
+ def previousLineCompletions(): Unit = {
+ val intp = newIMain()
+ intp.interpret("class C { val x_y_z = 42 }")
+ intp.interpret("object O { type T = Int }")
+
+ val completer = new PresentationCompilerCompleter(intp)
+
+ checkExact(completer, "new C().x_y")("x_y_z")
+ checkExact(completer, "(1 : O.T).toCha")("toChar")
+ }
+
+ @Test
+ def previousResultInvocation(): Unit = {
+ val intp = newIMain()
+ intp.interpret("1 + 1")
+
+ val completer = new PresentationCompilerCompleter(intp)
+
+ checkExact(completer, ".toCha")("toChar")
+ }
+
+ @Test
+ def defString(): Unit = {
+ val intp = newIMain()
+ val completer = new PresentationCompilerCompleter(intp)
+
+ // Double Tab on a fully typed selection shows the def string
+ checkExact(completer, "(p: {def a_b_c: Int}) => p.a_b_c")()
+ checkExact(completer, "(p: {def a_b_c: Int}) => p.a_b_c")(EmptyString, "def a_b_c: Int")
+
+ // likewise for an ident
+ checkExact(completer, "(p: {def x_y_z: Int}) => {import p._; x_y_z")()
+ checkExact(completer, "(p: {def x_y_z: Int}) => {import p._; x_y_z")(EmptyString, "def x_y_z: Int")
+
+ // If the first completion only gives one alternative
+ checkExact(completer, "(p: {def x_y_z: Int; def x_y_z(a: String): Int }) => p.x_y")("x_y_z")
+ // ... it is automatically inserted into the buffer. Hitting <TAB> again is triggers the help
+ checkExact(completer, "(p: {def x_y_z: Int; def x_y_z(a: String): Int }) => p.x_y_z")(EmptyString, "def x_y_z(a: String): Int", "def x_y_z: Int")
+
+ checkExact(completer, "(p: {def x_y_z: Int; def x_z_y(a: String): Int }) => p.x_")("x_y_z", "x_z_y")
+ // By contrast, in this case the user had to type "y_z" manually, so no def string printing just yet
+ checkExact(completer, "(p: {def x_y_z: Int; def x_z_y(a: String): Int }) => p.x_y_z")()
+ // Another <TAB>, Okay, time to print.
+ checkExact(completer, "(p: {def x_y_z: Int; def x_z_y(a: String): Int }) => p.x_y_z")(EmptyString, "def x_y_z: Int")
+
+ // The def string reconstructs the source-level modifiers (rather than showing the desugarings of vals),
+ // and performs as-seen-from with respect to the prefix
+ checkExact(completer, "trait T[A]{ lazy val x_y_z: A }; class C extends T[Int] { x_y_z")()
+ checkExact(completer, "trait T[A]{ lazy val x_y_z: A }; class C extends T[Int] { x_y_z")(EmptyString, "lazy val x_y_z: Int")
+ }
+
+ @Test
+ def treePrint(): Unit = {
+ val intp = newIMain()
+ val completer = new PresentationCompilerCompleter(intp)
+ checkExact(completer, " 1.toHexString //print")(EmptyString, "scala.Predef.intWrapper(1).toHexString // : String")
+ }
+
+ def checkExact(completer: PresentationCompilerCompleter, before: String, after: String = "")(expected: String*): Unit = {
+ assertEquals(expected.toSet, completer.complete(before, after).candidates.toSet)
+ }
+}