将包含选项卡的代码粘贴到 scala repl 中

Paste code containing tabs into scala repl

我正在使用

:paste

在复制显然包含制表符的 Scala 代码片段之前。但即便如此,还是会发生以下情况:

scala> :paste
// Entering paste mode (ctrl-D to finish)

type Moves = Seq[Board]
val EmptyMoves = Seq[Board]()

def allMoves() = {
Display all 413 possibilities? (y or n)
e = (‘x’,’o’).map{ case xo =>
Display all 413 possibilities? (y or n)
dex
Display all 413 possibilities? (y or n)
t, ix % 3) }

那么有没有任何方法可以将带有制表符的代码放入repl?

您可以将代码粘贴到文件中,然后在 REPL 中执行以下命令:

 scala> :load YourFile.scala

注意: 文件不应该有包声明,否则加载将失败。

REPL 需要这个 rather trivial fix

或者,您可以实施修复并提供 -Dscala.repl.reader=my.Reader

另一个答案建议想方设法劫持 reader:

scala> :power
Power mode enabled. :phase is at typer.
import scala.tools.nsc._, intp.global._, definitions._
Try :help or completions for vals._ and power._

scala> import scala.tools.nsc.interpreter._, java.io._
import scala.tools.nsc.interpreter._
import java.io._

scala> def f(code: String) = repl.savingReader {
     | repl.in = new SimpleReader(new BufferedReader(new StringReader(code)), new PrintWriter(scala.Console.out), false)
     | repl.loop() }
f: (code: String)$r.repl.LineResults.LineResult

scala> f("val x = 42. toInt")    // embedded tab
x: Int = 42
res5: $r.repl.LineResults.LineResult = EOF

或前述编辑:

$ scala -Xnojline
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions for evaluation. Or try :help.

scala> 

scala> val x = 42.  toInt
x: Int = 42

或前述完成:

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions for evaluation. Or try :help.

scala> val x = 42.             // 42 tab toInt
!=   /    >=          ceil          getClass        isPosInfinity   isWhole     shortValue       toDegrees     toOctalString   underlying   
%    <    >>          compare       intValue        isValidByte     longValue   signum           toDouble      toRadians       until        
&    <<   >>>         compareTo     isInfinite      isValidChar     max         to               toFloat       toShort         |            
*    <=   ^           doubleValue   isInfinity      isValidInt      min         toBinaryString   toHexString   unary_+                      
+    ==   abs         floatValue    isNaN           isValidLong     round       toByte           toInt         unary_-                      
-    >    byteValue   floor         isNegInfinity   isValidShort    self        toChar           toLong        unary_~                      

scala> val x = 42.toInt
x: Int = 42

scala> :power
Power mode enabled. :phase is at typer.
import scala.tools.nsc._, intp.global._, definitions._
Try :help or completions for vals._ and power._

scala> import scala.tools.nsc.interpreter._
import scala.tools.nsc.interpreter._

scala> repl.in = new jline.InteractiveReader(() => NoCompletion)
repl.in: scala.tools.nsc.interpreter.InteractiveReader = scala.tools.nsc.interpreter.jline.InteractiveReader@10660795

scala> val x = 42.toInt
x: Int = 42