New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BatchSourceFile throws an exception when there is an empty line at the end of a file #9885
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9885?orig=1 |
@som-snytt said: scala> val offset = file.lineToOffset(4)
offset: Int = 37
scala> file.content.length
res2: Int = 37 |
Eugene Apollonsky (chessman) said:
@ val a = """package com.example
|object Test {
|}
|
|""".stripMargin
a: String = """
package com.example
object Test {
}
"""
@ a.split("\n", -1)
res17: Array[String] = Array("package com.example", "object Test {", "}", "", "") |
Eugene Apollonsky (chessman) said:
|
@som-snytt said (edited on Aug 13, 2016 12:00:09 AM UTC): $ 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 text = """package com.example
| |object Test {
| |}
| |
| |""".stripMargin
text: String =
"package com.example
object Test {
}
"
scala> text.lines.length
res0: Int = 4
scala> text.split("\n")
res1: Array[String] = Array(package com.example, object Test {, })
scala> text.split("\n", -1)
res2: Array[String] = Array(package com.example, object Test {, }, "", "")
scala> "a\nb\nc\n".split("\n", -1)
res3: Array[String] = Array(a, b, c, "")
Position.toString wants its line number, which is based on its point, so probably the error is due to a position past EOF. (here) Position has 1-based line number (here), so that may be confusing. It's not obvious that you couldn't create a Position by converting a valid position's line number to an offset, without subtracting one first. Position.offset(f, f.lineToOffset(p.line - 1)) // start of line |
@som-snytt said: |
@som-snytt said: Is this another of those hardest problems in computer science? BatchSourceFile introduces a NL into an empty file (though possibly it should only do that, for internal reasons, for nonempty files not ending in whitespace). scala> import scala.reflect.internal.util._
import scala.reflect.internal.util._
scala> val f = new BatchSourceFile("test", "")
f: scala.reflect.internal.util.BatchSourceFile = test
scala> f.content
res0: Array[Char] =
Array(
)
scala> f.content.length
res1: Int = 1
|
The linked tickets are unrelated. Fool me once! |
Fails with:
The text was updated successfully, but these errors were encountered: