Skip to content

Commit 00362b4

Browse files
fix Caret arguments ordering (#313)
* fix Caret arguments ordering `line` was filled with `offset`, `col` was filled with `line` and `offset` with `col`. I'm guessing at one point Caret was `Caret(offset, line, column)`, but was changed to `Caret(line, column, offset)` without re-examing argument order. This caused this behavior: ```scala val p = (Parser.caret.with1 <* Parser.anyChar).rep val pattern = """aaa""" val parsed = p.parseAll(pattern).right.get // parsed: cats.data.NonEmptyList[Caret] = NonEmptyList(Caret(0, 0, 0), List(Caret(1, 0, 1), Caret(2, 0, 2))) ``` Expected: ```scala // parsed: cats.data.NonEmptyList[Caret] = NonEmptyList(Caret(0, 0, 0), List(Caret(0, 1, 1), Caret(0, 2, 2))) ``` * formatting * Deconstruct Caret properly * remove unneeded case * add assert for offset
1 parent 95ce42a commit 00362b4

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

core/shared/src/main/scala/cats/parse/LocationMap.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class LocationMap(val input: String) {
6969
*/
7070
def toLineCol(offset: Int): Option[(Int, Int)] =
7171
if (isValidOffset(offset)) {
72-
val Caret(_, line, col) = toCaretUnsafeImpl(offset)
72+
val Caret(line, col, _) = toCaretUnsafeImpl(offset)
7373
Some((line, col))
7474
} else None
7575

@@ -81,9 +81,9 @@ class LocationMap(val input: String) {
8181
// this is end of line
8282
if (offset == 0) Caret.Start
8383
else {
84-
val Caret(_, line, col) = toCaretUnsafeImpl(offset - 1)
85-
if (endsWithNewLine) Caret(offset, line + 1, 0)
86-
else Caret(offset, line, col + 1)
84+
val Caret(line, col, _) = toCaretUnsafeImpl(offset - 1)
85+
if (endsWithNewLine) Caret(line = line + 1, col = 0, offset = offset)
86+
else Caret(line = line, col = col + 1, offset = offset)
8787
}
8888
} else {
8989
val idx = Arrays.binarySearch(firstPos, offset)
@@ -98,10 +98,10 @@ class LocationMap(val input: String) {
9898
// so we are pointing into a line
9999
val lineStart = firstPos(line)
100100
val col = offset - lineStart
101-
Caret(offset, line, col)
101+
Caret(line = line, col = col, offset = offset)
102102
} else {
103103
// idx is exactly the right value because offset is beginning of a line
104-
Caret(offset, idx, 0)
104+
Caret(line = idx, col = 0, offset = offset)
105105
}
106106
}
107107

core/shared/src/test/scala/cats/parse/LocationMapTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ class LocationMapTest extends munit.ScalaCheckSuite {
226226
val lc = lm.toLineCol(offset)
227227

228228
assertEquals(oc, Some(c))
229-
assertEquals(lc, oc.map { case Caret(_, r, c) => (r, c) })
229+
assertEquals(lc, oc.map { c => (c.line, c.col) })
230+
assertEquals(c.offset, offset)
230231
}
231232

232233
if (other < 0 || s.length < other) {

0 commit comments

Comments
 (0)