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 Caret arguments ordering #313

Merged
merged 5 commits into from
Nov 20, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions core/shared/src/main/scala/cats/parse/LocationMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class LocationMap(val input: String) {
*/
def toLineCol(offset: Int): Option[(Int, Int)] =
if (isValidOffset(offset)) {
val Caret(_, line, col) = toCaretUnsafeImpl(offset)
val Caret(line, col, _) = toCaretUnsafeImpl(offset)
Some((line, col))
} else None

Expand All @@ -81,9 +81,9 @@ class LocationMap(val input: String) {
// this is end of line
if (offset == 0) Caret.Start
else {
val Caret(_, line, col) = toCaretUnsafeImpl(offset - 1)
if (endsWithNewLine) Caret(offset, line + 1, 0)
else Caret(offset, line, col + 1)
val Caret(line, col, _) = toCaretUnsafeImpl(offset - 1)
if (endsWithNewLine) Caret(line = line + 1, col = 0, offset = offset)
else Caret(line = line, col = col + 1, offset = offset)
}
} else {
val idx = Arrays.binarySearch(firstPos, offset)
Expand All @@ -98,10 +98,10 @@ class LocationMap(val input: String) {
// so we are pointing into a line
val lineStart = firstPos(line)
val col = offset - lineStart
Caret(offset, line, col)
Caret(line = line, col = col, offset = offset)
} else {
// idx is exactly the right value because offset is beginning of a line
Caret(offset, idx, 0)
Caret(line = idx, col = 0, offset = offset)
}
}

Expand Down
3 changes: 2 additions & 1 deletion core/shared/src/test/scala/cats/parse/LocationMapTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ class LocationMapTest extends munit.ScalaCheckSuite {
val lc = lm.toLineCol(offset)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually while we are at it can we add: assertEquals(c.offset, offset) to make sure we pass through the offset?

assertEquals(oc, Some(c))
assertEquals(lc, oc.map { case Caret(_, r, c) => (r, c) })
assertEquals(lc, oc.map { c => (c.line, c.col) })
assertEquals(c.offset, offset)
}

if (other < 0 || s.length < other) {
Expand Down