Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit 7968421

Browse files
committed
SI-9206 Local refactor to save eyesight
We talk about bit rot but not about how dust accumulates on code that hasn't been swept since the last time the furniture was moved around.
1 parent 3bfafbc commit 7968421

File tree

2 files changed

+39
-42
lines changed

2 files changed

+39
-42
lines changed

src/repl/scala/tools/nsc/interpreter/ILoop.scala

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,16 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
783783
echo("\n// Detected repl transcript paste: ctrl-D to finish.\n")
784784
apply(Iterator(start) ++ readWhile(!isPromptOnly(_)))
785785
}
786+
787+
def unapply(line: String): Boolean = isPrompted(line)
788+
}
789+
790+
private object invocation {
791+
def unapply(line: String): Boolean = Completion.looksLikeInvocation(line)
786792
}
787793

794+
private val lineComment = """\s*//.*""".r // all comment
795+
788796
/** Interpret expressions starting with the first line.
789797
* Read lines until a complete compilation unit is available
790798
* or until a syntax error has been seen. If a full unit is
@@ -795,53 +803,42 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
795803
// signal completion non-completion input has been received
796804
in.completion.resetVerbosity()
797805

798-
def reallyInterpret = {
799-
val reallyResult = intp.interpret(code)
800-
(reallyResult, reallyResult match {
801-
case IR.Error => None
802-
case IR.Success => Some(code)
803-
case IR.Incomplete =>
804-
if (in.interactive && code.endsWith("\n\n")) {
805-
echo("You typed two blank lines. Starting a new command.")
806+
def reallyInterpret = intp.interpret(code) match {
807+
case IR.Error => None
808+
case IR.Success => Some(code)
809+
case IR.Incomplete if in.interactive && code.endsWith("\n\n") =>
810+
echo("You typed two blank lines. Starting a new command.")
811+
None
812+
case IR.Incomplete =>
813+
in.readLine(paste.ContinueString) match {
814+
case null =>
815+
// we know compilation is going to fail since we're at EOF and the
816+
// parser thinks the input is still incomplete, but since this is
817+
// a file being read non-interactively we want to fail. So we send
818+
// it straight to the compiler for the nice error message.
819+
intp.compileString(code)
806820
None
807-
}
808-
else in.readLine(paste.ContinueString) match {
809-
case null =>
810-
// we know compilation is going to fail since we're at EOF and the
811-
// parser thinks the input is still incomplete, but since this is
812-
// a file being read non-interactively we want to fail. So we send
813-
// it straight to the compiler for the nice error message.
814-
intp.compileString(code)
815-
None
816-
817-
case line => interpretStartingWith(code + "\n" + line)
818-
}
819-
})
821+
822+
case line => interpretStartingWith(code + "\n" + line)
823+
}
820824
}
821825

822-
/** Here we place ourselves between the user and the interpreter and examine
823-
* the input they are ostensibly submitting. We intervene in several cases:
826+
/* Here we place ourselves between the user and the interpreter and examine
827+
* the input they are ostensibly submitting. We intervene in several cases:
824828
*
825-
* 1) If the line starts with "scala> " it is assumed to be an interpreter paste.
826-
* 2) If the line starts with "." (but not ".." or "./") it is treated as an invocation
827-
* on the previous result.
828-
* 3) If the Completion object's execute returns Some(_), we inject that value
829-
* and avoid the interpreter, as it's likely not valid scala code.
829+
* 1) If the line starts with "scala> " it is assumed to be an interpreter paste.
830+
* 2) If the line starts with "." (but not ".." or "./") it is treated as an invocation
831+
* on the previous result.
832+
* 3) If the Completion object's execute returns Some(_), we inject that value
833+
* and avoid the interpreter, as it's likely not valid scala code.
830834
*/
831-
if (code == "") None
832-
else if (!paste.running && paste.isPrompted(code)) {
833-
paste.transcript(code)
834-
None
835-
}
836-
else if (Completion.looksLikeInvocation(code) && intp.mostRecentVar != "") {
837-
interpretStartingWith(intp.mostRecentVar + code)
835+
code match {
836+
case "" => None
837+
case lineComment() => None // line comment, do nothing
838+
case paste() if !paste.running => paste.transcript(code) ; None
839+
case invocation() if intp.mostRecentVar != "" => interpretStartingWith(intp.mostRecentVar + code)
840+
case _ => reallyInterpret
838841
}
839-
else if (code.trim startsWith "//") {
840-
// line comment, do nothing
841-
None
842-
}
843-
else
844-
reallyInterpret._2
845842
}
846843

847844
// runs :load `file` on any files passed via -i

src/repl/scala/tools/nsc/interpreter/Pasted.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ abstract class Pasted {
8585
case _ => code
8686
}
8787

88-
def run() {
88+
def run(): Unit = {
8989
println("// Replaying %d commands from transcript.\n" format cmds.size)
9090
cmds foreach { cmd =>
9191
print(ActualPromptString)

0 commit comments

Comments
 (0)