Skip to content
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

Fix some minor issues before 0.3.5 #305

Merged
merged 1 commit into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/shared/src/main/scala/cats/parse/Caret.scala
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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