diff options
author | Som Snytt <som.snytt@gmail.com> | 2014-01-13 17:03:20 -0800 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2014-01-13 17:03:20 -0800 |
commit | f606d8176e57fbb61495b693bf3cd4e77373fcfb (patch) | |
tree | dfcff3e28d99b13a9335f9bcef592ac6b296127a /src/reflect/scala/reflect/internal/util | |
parent | 2c8a8ff6ba29be8d21845c272157ded07acc67d8 (diff) | |
download | scala-f606d8176e57fbb61495b693bf3cd4e77373fcfb.tar.gz scala-f606d8176e57fbb61495b693bf3cd4e77373fcfb.tar.bz2 scala-f606d8176e57fbb61495b693bf3cd4e77373fcfb.zip |
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.
Diffstat (limited to 'src/reflect/scala/reflect/internal/util')
-rw-r--r-- | src/reflect/scala/reflect/internal/util/Position.scala | 34 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/util/SourceFile.scala | 27 |
2 files changed, 34 insertions, 27 deletions
diff --git a/src/reflect/scala/reflect/internal/util/Position.scala b/src/reflect/scala/reflect/internal/util/Position.scala index bcf637b558..4032ec1157 100644 --- a/src/reflect/scala/reflect/internal/util/Position.scala +++ b/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 = ( diff --git a/src/reflect/scala/reflect/internal/util/SourceFile.scala b/src/reflect/scala/reflect/internal/util/SourceFile.scala index 39c4ac8a95..9866b043bb 100644 --- a/src/reflect/scala/reflect/internal/util/SourceFile.scala +++ b/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] |