Skip to content
This repository
Browse code

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...
commit f606d8176e57fbb61495b693bf3cd4e77373fcfb 1 parent 2c8a8ff
som-snytt som-snytt authored
34 src/reflect/scala/reflect/internal/util/Position.scala
@@ -206,19 +206,27 @@ private[util] trait InternalPositionImpl {
206 206 @deprecated("use `lineCaret`", since="2.11.0")
207 207 def lineCarat: String = lineCaret
208 208
209   - def showError(msg: String): String = finalPosition match {
210   - case FakePos(fmsg) => s"$fmsg $msg"
211   - case NoPosition => msg
212   - case pos => f"${pos.line}: $msg%n${u(pos.lineContent)}%n${pos.lineCaret}"
213   - }
214   - private def u(s: String) = {
215   - def uu(c: Int) = f"\\u$c%04x"
216   - def uable(c: Int) = (c < 0x20 && c != '\t') || c == 0x7F
217   - if (s exists (c => uable(c))) {
218   - val sb = new StringBuilder
219   - s foreach (c => sb append (if (uable(c)) uu(c) else c))
220   - sb.toString
221   - } else s
  209 + def showError(msg: String): String = {
  210 + def escaped(s: String) = {
  211 + def u(c: Int) = f"\\u$c%04x"
  212 + def uable(c: Int) = (c < 0x20 && c != '\t') || c == 0x7F
  213 + if (s exists (c => uable(c))) {
  214 + val sb = new StringBuilder
  215 + s foreach (c => sb append (if (uable(c)) u(c) else c))
  216 + sb.toString
  217 + } else s
  218 + }
  219 + def errorAt(p: Pos) = {
  220 + def where = p.line
  221 + def content = escaped(p.lineContent)
  222 + def indicator = p.lineCaret
  223 + f"$where: $msg%n$content%n$indicator"
  224 + }
  225 + finalPosition match {
  226 + case FakePos(fmsg) => s"$fmsg $msg"
  227 + case NoPosition => msg
  228 + case pos => errorAt(pos)
  229 + }
222 230 }
223 231 def showDebug: String = toString
224 232 def show = (
27 src/reflect/scala/reflect/internal/util/SourceFile.scala
@@ -134,20 +134,19 @@ class BatchSourceFile(val file : AbstractFile, val content0: Array[Char]) extend
134 134 super.identifier(pos)
135 135 }
136 136
137   - def isLineBreak(idx: Int) =
138   - (idx < length) && (content(idx) match {
139   - // don't identify the CR in CR LF as a line break, since LF will do.
140   - case CR => (idx + 1 == length) || (content(idx + 1) != LF)
141   - case ch => isLineBreakChar(ch)
142   - })
143   -
144   - def isEndOfLine(idx: Int) =
145   - (idx < length) && (content(idx) match {
146   - // don't identify the CR in CR LF as a line break, since LF will do.
147   - case CR => (idx + 1 == length) || (content(idx + 1) != LF)
148   - case LF => true
149   - case _ => false
150   - })
  137 + private def charAtIsEOL(idx: Int)(p: Char => Boolean) = {
  138 + // don't identify the CR in CR LF as a line break, since LF will do.
  139 + def notCRLF0 = content(idx) != CR || idx + 1 >= length || content(idx + 1) != LF
  140 +
  141 + idx < length && notCRLF0 && p(content(idx))
  142 + }
  143 +
  144 + def isLineBreak(idx: Int) = charAtIsEOL(idx)(isLineBreakChar)
  145 +
  146 + def isEndOfLine(idx: Int) = charAtIsEOL(idx) {
  147 + case CR | LF => true
  148 + case _ => false
  149 + }
151 150
152 151 def calculateLineIndices(cs: Array[Char]) = {
153 152 val buf = new ArrayBuffer[Int]

0 comments on commit f606d81

Please sign in to comment.
Something went wrong with that request. Please try again.