diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2016-11-22 21:06:40 +1000 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2016-11-22 21:12:20 +1000 |
commit | 76155fa4e9ef103de4b8283097f6cde18c6f1e08 (patch) | |
tree | de2e99a568bab8d9e15210d3945562f3c83feb25 | |
parent | 35f8908c214b5458ffe32c782dc81055773f5fb4 (diff) | |
download | scala-76155fa4e9ef103de4b8283097f6cde18c6f1e08.tar.gz scala-76155fa4e9ef103de4b8283097f6cde18c6f1e08.tar.bz2 scala-76155fa4e9ef103de4b8283097f6cde18c6f1e08.zip |
Improve performance of REPL autocompletion
The code used to fuzzily match, e.g, `declasses` with `getDeclaredClasses`
was exploring fruitless parts of the search space. The enclosed test
case was hanging the REPL.
This commit improves this by performing a prefix match of the unconsumed input
against the current chunk of the candidate before exploring the `inits`.
Fixes scala/scala-dev#271
-rw-r--r-- | src/interactive/scala/tools/nsc/interactive/Global.scala | 3 | ||||
-rw-r--r-- | test/junit/scala/tools/nsc/interpreter/CompletionTest.scala | 8 |
2 files changed, 10 insertions, 1 deletions
diff --git a/src/interactive/scala/tools/nsc/interactive/Global.scala b/src/interactive/scala/tools/nsc/interactive/Global.scala index 27a02c46a2..5c00d67888 100644 --- a/src/interactive/scala/tools/nsc/interactive/Global.scala +++ b/src/interactive/scala/tools/nsc/interactive/Global.scala @@ -1202,7 +1202,8 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "") case Nil => entered.isEmpty && matchCount > 0 case head :: tail => val enteredAlternatives = Set(entered, entered.capitalize) - head.inits.filter(_.length <= entered.length).exists(init => + val n = (head, entered).zipped.count {case (c, e) => c == e || (c.isUpper && c == e.toUpper)} + head.take(n).inits.exists(init => enteredAlternatives.exists(entered => lenientMatch(entered.stripPrefix(init), tail, matchCount + (if (init.isEmpty) 0 else 1)) ) diff --git a/test/junit/scala/tools/nsc/interpreter/CompletionTest.scala b/test/junit/scala/tools/nsc/interpreter/CompletionTest.scala index 78ebb7cf9c..7c37be126d 100644 --- a/test/junit/scala/tools/nsc/interpreter/CompletionTest.scala +++ b/test/junit/scala/tools/nsc/interpreter/CompletionTest.scala @@ -174,6 +174,14 @@ class CompletionTest { checkExact(completer, "case class D(a: Int, b: Int) { this.a")("a", "asInstanceOf") } + @Test + def performanceOfLenientMatch(): Unit = { + val intp = newIMain() + val completer = new PresentationCompilerCompleter(intp) + val ident: String = "thisIsAReallyLongMethodNameWithManyManyManyManyChunks" + checkExact(completer, s"($ident: Int) => tia")(ident) + } + def checkExact(completer: PresentationCompilerCompleter, before: String, after: String = "")(expected: String*): Unit = { assertEquals(expected.toSet, completer.complete(before, after).candidates.toSet) } |