WestCoastProjects
WestCoastProjects

Reputation: 63201

Paste code containing tabs into scala repl

I am using

:paste

before copying in a scala code snippet that apparently contains tabs. But even so the following occurs:

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) }

So then is there any way to be able to put code with tabs into the repl?

Upvotes: 3

Views: 543

Answers (2)

som-snytt
som-snytt

Reputation: 39577

You'll need this rather trivial fix to the REPL.

Alternatively, you can implement the fix and supply -Dscala.repl.reader=my.Reader.

The other answer suggests finding ways to hijack the 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

Or foregoing editing:

$ 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

Or foregoing completion:

$ 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

Upvotes: 2

meucaa
meucaa

Reputation: 1515

You can paste your code in a file and then do the following command in your REPL:

 scala> :load YourFile.scala

NB: the file should not have package declarations or else the loading will fail.

Upvotes: 1

Related Questions