summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-01-20 18:25:47 +0000
committerPaul Phillips <paulp@improving.org>2011-01-20 18:25:47 +0000
commitcb693f9f3a02f95d6cb341269dd15e168c5b929c (patch)
tree668fae6b5c92e3dab5f451aca060257041067b73 /src
parentd8994ad4d1436f5e1e31ea3529c115f119302767 (diff)
downloadscala-cb693f9f3a02f95d6cb341269dd15e168c5b929c.tar.gz
scala-cb693f9f3a02f95d6cb341269dd15e168c5b929c.tar.bz2
scala-cb693f9f3a02f95d6cb341269dd15e168c5b929c.zip
The repl becomes more intense about making sure...
The repl becomes more intense about making sure you wanted file completion before offering it. Closes #4130, no review.
Diffstat (limited to 'src')
-rw-r--r--src/compiler/scala/tools/nsc/Interpreter.scala26
-rw-r--r--src/compiler/scala/tools/nsc/InterpreterLoop.scala9
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/Completion.scala7
3 files changed, 34 insertions, 8 deletions
diff --git a/src/compiler/scala/tools/nsc/Interpreter.scala b/src/compiler/scala/tools/nsc/Interpreter.scala
index 348342c4b4..09c75c6fa3 100644
--- a/src/compiler/scala/tools/nsc/Interpreter.scala
+++ b/src/compiler/scala/tools/nsc/Interpreter.scala
@@ -72,6 +72,12 @@ import Interpreter._
class Interpreter(val settings: Settings, out: PrintWriter) {
repl =>
+ /** whether to print out result lines */
+ private[nsc] var printResults: Boolean = true
+
+ /** whether to print errors */
+ private[nsc] var totalSilence: Boolean = false
+
private val RESULT_OBJECT_PREFIX = "RequestResult$"
def println(x: Any) = {
@@ -89,6 +95,9 @@ class Interpreter(val settings: Settings, out: PrintWriter) {
/** reporter */
object reporter extends ConsoleReporter(settings, null, out) {
override def printMessage(msg: String) {
+ if (totalSilence)
+ return
+
out println (
if (truncationOK) clean(msg)
else cleanNoTruncate(msg)
@@ -170,9 +179,6 @@ class Interpreter(val settings: Settings, out: PrintWriter) {
import compiler.definitions
import definitions.{ EmptyPackage, getMember }
- /** whether to print out result lines */
- private[nsc] var printResults: Boolean = true
-
/** Temporarily be quiet */
def beQuietDuring[T](operation: => T): T = {
val wasPrinting = printResults
@@ -181,6 +187,12 @@ class Interpreter(val settings: Settings, out: PrintWriter) {
operation
}
}
+ def beSilentDuring[T](operation: => T): T = {
+ val saved = totalSilence
+ totalSilence = true
+ try operation
+ finally totalSilence = saved
+ }
/** whether to bind the lastException variable */
private var bindLastException = true
@@ -529,6 +541,14 @@ class Interpreter(val settings: Settings, out: PrintWriter) {
else Some(trees)
}
}
+ def isParseable(line: String): Boolean = {
+ beSilentDuring {
+ parse(line) match {
+ case Some(xs) => xs.nonEmpty
+ case _ => false
+ }
+ }
+ }
/** Compile an nsc SourceFile. Returns true if there are
* no compilation errors, or false otherwise.
diff --git a/src/compiler/scala/tools/nsc/InterpreterLoop.scala b/src/compiler/scala/tools/nsc/InterpreterLoop.scala
index 6d65f74924..7c25b36d7d 100644
--- a/src/compiler/scala/tools/nsc/InterpreterLoop.scala
+++ b/src/compiler/scala/tools/nsc/InterpreterLoop.scala
@@ -549,10 +549,11 @@ class InterpreterLoop(in0: Option[BufferedReader], protected val out: PrintWrite
interpretStartingWith(interpreter.mostRecentVar + code)
}
else {
- val result = for (comp <- in.completion ; res <- comp execute code) yield res
- result match {
- case Some(res) => injectAndName(res) ; None // completion took responsibility, so do not parse
- case _ => reallyInterpret
+ if (interpreter.isParseable(code)) reallyInterpret
+ else {
+ val res = in.completion flatMap (_ execute code) map injectAndName
+ if (res.isDefined) None // completion took responsibility, so do not parse
+ else reallyInterpret // we know it will fail, this is to show the error
}
}
}
diff --git a/src/compiler/scala/tools/nsc/interpreter/Completion.scala b/src/compiler/scala/tools/nsc/interpreter/Completion.scala
index e242c90c0c..6dcdbf4579 100644
--- a/src/compiler/scala/tools/nsc/interpreter/Completion.scala
+++ b/src/compiler/scala/tools/nsc/interpreter/Completion.scala
@@ -20,6 +20,9 @@ object Completion {
&& !(code startsWith "./")
&& !(code startsWith "..")
)
+ def looksLikePath(code: String) = (code != null) && (code.length >= 2) && (
+ Set("/", "\\", "./", "../", "~/") exists (code startsWith _)
+ )
object Forwarder {
def apply(forwardTo: () => Option[CompletionAware]): CompletionAware = new CompletionAware {
@@ -348,7 +351,9 @@ class Completion(val repl: Interpreter) extends CompletionOutput {
else tryCompletion(Parsed.dotted(buf drop 1, cursor), lastResultFor)
def regularCompletion = tryCompletion(mkDotted, topLevelFor)
- def fileCompletion = tryCompletion(mkUndelimited, FileCompletion completionsFor _.buffer)
+ def fileCompletion =
+ if (!looksLikePath(buf)) None
+ else tryCompletion(mkUndelimited, FileCompletion completionsFor _.buffer)
/** This is the kickoff point for all manner of theoretically possible compiler
* unhappiness - fault may be here or elsewhere, but we don't want to crash the