scala-ide / scala-worksheet

A Scala IDE plugin for a multi-line REPL (called worksheet)
96 stars 24 forks source link

Scala Worksheet is adding a new line because, apparently, the stream I'm reading from has a '\n' char in it #111

Open wnleao opened 11 years ago

wnleao commented 11 years ago

This is my first ticket, sorry if there's anything wrong.

The problem is that I'm testing a Stream of chars, which I made off of a buffered file. My code is the following:

val buff = Source.fromFile("C:/test.txt")
val stream = buff.toStream  

def readLine(s: Stream[Char]): List[Char] =
    if (s.head == '\n') Nil
    else s.head :: readLine(s.tail)               

readLine(stream)

So far so good. The problem happens when Scala Worksheet tries to evaluate the code and add the appropriate comments. And, when it reaches the last line it adds the following:


readLine(stream)                                //> res0: List[Char] = List(1, 2, 3, 1, 2, 3, 
)

The ')' in the second line is not part of the comment, therefore it raises the compile error "';' expected but ')' found"

I've tested it and it seems that it's behaving that way because the next char in the list (the stream's head) is '\n' (new line in the given file).

My impression was taken by the fact that if you do the following:


def readLine(s: Stream[Char]): List[Char] =
    if (s.head == '3') Nil
    else s.head :: readLine(s.tail)               //> readLine: (s: Stream[Char])List[Char]

readLine(stream)                                //> res0: List[Char] = List(1, 2)

I mean, changing the condition to s.head == '3', the next char will not be a '\n' and the Scala Worksheet returns gracefully.

You could also try to do the following:

 def readLine(s: Stream[Char]): List[Char] =
    if (s.head == '\n') List('\n')
    else s.head :: readLine(s.tail)               //> readLine: (s: Stream[Char])List[Char]

  readLine(stream)                                //> res0: List[Char] = List(1, 2, 3, 1, 2, 3, 
, 
                                                  //| )

That time, it breaks the line and adds the comma and then the new line char.

I hope I've done everything right here raising this ticket, cheers! Keep up the good work.

skyluc commented 11 years ago

Thanks for the report @kiniggit.

I tried to reproduce the problem on version 1.1 and 1.2 of worksheet, but without success. Can you give us the version numbers of the Scala plug-ins you are using? They are available in Help>About Eclipse ...>Installation Details.

wnleao commented 11 years ago

Hello! I'm using version 0.1.2.v-2_09-2012 and Scala IDE for eclipse version 2.1.0.m2-2_09-2012.