Permalink
Browse files

SI-8015 Refactor per code review

Make an obscure private def local and rename it usefully.

If you really must have similar functions in the API
(to detect line breaks and EOL), at least make them DRY.
  • Loading branch information...
1 parent 2c8a8ff commit f606d8176e57fbb61495b693bf3cd4e77373fcfb @som-snytt som-snytt committed Jan 14, 2014
View
34 src/reflect/scala/reflect/internal/util/Position.scala
@@ -206,19 +206,27 @@ private[util] trait InternalPositionImpl {
@deprecated("use `lineCaret`", since="2.11.0")
def lineCarat: String = lineCaret
- def showError(msg: String): String = finalPosition match {
- case FakePos(fmsg) => s"$fmsg $msg"
- case NoPosition => msg
- case pos => f"${pos.line}: $msg%n${u(pos.lineContent)}%n${pos.lineCaret}"
- }
- private def u(s: String) = {
- def uu(c: Int) = f"\\u$c%04x"
- def uable(c: Int) = (c < 0x20 && c != '\t') || c == 0x7F
- if (s exists (c => uable(c))) {
- val sb = new StringBuilder
- s foreach (c => sb append (if (uable(c)) uu(c) else c))
- sb.toString
- } else s
+ def showError(msg: String): String = {
+ def escaped(s: String) = {
+ def u(c: Int) = f"\\u$c%04x"
+ def uable(c: Int) = (c < 0x20 && c != '\t') || c == 0x7F
+ if (s exists (c => uable(c))) {
+ val sb = new StringBuilder
+ s foreach (c => sb append (if (uable(c)) u(c) else c))
+ sb.toString
+ } else s
+ }
+ def errorAt(p: Pos) = {
+ def where = p.line
+ def content = escaped(p.lineContent)
+ def indicator = p.lineCaret
+ f"$where: $msg%n$content%n$indicator"
+ }
+ finalPosition match {
+ case FakePos(fmsg) => s"$fmsg $msg"
+ case NoPosition => msg
+ case pos => errorAt(pos)
+ }
}
def showDebug: String = toString
def show = (
View
27 src/reflect/scala/reflect/internal/util/SourceFile.scala
@@ -134,20 +134,19 @@ class BatchSourceFile(val file : AbstractFile, val content0: Array[Char]) extend
super.identifier(pos)
}
- def isLineBreak(idx: Int) =
- (idx < length) && (content(idx) match {
- // don't identify the CR in CR LF as a line break, since LF will do.
- case CR => (idx + 1 == length) || (content(idx + 1) != LF)
- case ch => isLineBreakChar(ch)
- })
-
- def isEndOfLine(idx: Int) =
- (idx < length) && (content(idx) match {
- // don't identify the CR in CR LF as a line break, since LF will do.
- case CR => (idx + 1 == length) || (content(idx + 1) != LF)
- case LF => true
- case _ => false
- })
+ private def charAtIsEOL(idx: Int)(p: Char => Boolean) = {
+ // don't identify the CR in CR LF as a line break, since LF will do.
+ def notCRLF0 = content(idx) != CR || idx + 1 >= length || content(idx + 1) != LF
+
+ idx < length && notCRLF0 && p(content(idx))
+ }
+
+ def isLineBreak(idx: Int) = charAtIsEOL(idx)(isLineBreakChar)
+
+ def isEndOfLine(idx: Int) = charAtIsEOL(idx) {
+ case CR | LF => true
+ case _ => false
+ }
def calculateLineIndices(cs: Array[Char]) = {
val buf = new ArrayBuffer[Int]

0 comments on commit f606d81

Please sign in to comment.