Skip to content

Commit

Permalink
Fix some minor issues before 0.3.5 (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Nov 12, 2021
1 parent af1bc74 commit c1ffc4b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
6 changes: 3 additions & 3 deletions core/shared/src/main/scala/cats/parse/Caret.scala
Expand Up @@ -23,17 +23,17 @@ package cats.parse

import cats.Order

/** This is a pointer to a zero based row, column, and total offset.
/** This is a pointer to a zero based line, column, and total offset.
*/
case class Caret(row: Int, col: Int, offset: Int)
case class Caret(line: Int, col: Int, offset: Int)

object Caret {
val Start: Caret = Caret(0, 0, 0)

implicit val caretOrder: Order[Caret] =
new Order[Caret] {
def compare(left: Caret, right: Caret): Int = {
val c0 = Integer.compare(left.row, right.row)
val c0 = Integer.compare(left.line, right.line)
if (c0 != 0) c0
else {
val c1 = Integer.compare(left.col, right.col)
Expand Down
20 changes: 9 additions & 11 deletions core/shared/src/main/scala/cats/parse/LocationMap.scala
Expand Up @@ -69,8 +69,8 @@ class LocationMap(val input: String) {
*/
def toLineCol(offset: Int): Option[(Int, Int)] =
if (isValidOffset(offset)) {
val Caret(_, row, col) = toCaretUnsafeImpl(offset)
Some((row, col))
val Caret(_, line, col) = toCaretUnsafeImpl(offset)
Some((line, col))
} else None

// This does not do bounds checking because we
Expand All @@ -94,20 +94,18 @@ class LocationMap(val input: String) {
// the key, or a.length if all elements in the array are less than the specified key.
//
// so insertion pos = ~(idx + 1)
val row = ~(idx + 1)
// so we are pointing into a row
val rowStart = firstPos(row)
val col = offset - rowStart
Caret(offset, row, col)
val line = ~(idx + 1)
// so we are pointing into a line
val lineStart = firstPos(line)
val col = offset - lineStart
Caret(offset, line, col)
} else {
// idx is exactly the right value because offset is beginning of a line
Caret(offset, idx, 0)
}
}

/** Convert an offset to a Caret.
* @throws IllegalArgumentException
* if offset is longer than input
/** Convert an offset to a Caret. throws IllegalArgumentException if offset is longer than input
*/
def toCaretUnsafe(offset: Int): Caret =
if (isValidOffset(offset)) toCaretUnsafeImpl(offset)
Expand All @@ -123,7 +121,7 @@ class LocationMap(val input: String) {
if (i >= 0 && i < lines.length) Some(lines(i))
else None

/** Return the offset for a given row/col. if we return Some(input.length) this means EOF if we
/** Return the offset for a given line/col. if we return Some(input.length) this means EOF if we
* return Some(i) for 0 <= i < input.length it is a valid item else offset < 0 or offset >
* input.length we return None
*/
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/test/scala/cats/parse/ParserTest.scala
Expand Up @@ -72,7 +72,7 @@ object ParserGen {

implicit val cogenCaret: Cogen[Caret] =
Cogen { caret: Caret =>
(caret.offset.toLong << 32) | (caret.col.toLong << 16) | (caret.row.toLong)
(caret.offset.toLong << 32) | (caret.col.toLong << 16) | (caret.line.toLong)
}

def arbGen[A: Arbitrary: Cogen]: GenT[Gen] =
Expand Down

0 comments on commit c1ffc4b

Please sign in to comment.