From 77960a6f4fb691a0009af9e7c7e7dfde30ea322d Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Thu, 28 Mar 2024 09:00:20 -0700 Subject: [PATCH] FormatTokensRewrite: check rewritten tokens sorted --- .../main/scala/org/scalafmt/rewrite/FormatTokensRewrite.scala | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/FormatTokensRewrite.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/FormatTokensRewrite.scala index e98efea6d7..0226c39787 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/FormatTokensRewrite.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/FormatTokensRewrite.scala @@ -48,6 +48,7 @@ class FormatTokensRewrite( def copySlice(end: Int): Unit = { val append = end - nextidxToCopy + require(append >= 0) // make sure rewritten tokens are sorted if (append > 0) { appended += append result ++= arr.view.slice(nextidxToCopy, end) @@ -308,6 +309,7 @@ object FormatTokensRewrite { private val claimed = new mutable.HashMap[Int, Int]() private[FormatTokensRewrite] val tokens = new mutable.ArrayBuffer[Replacement]() + private[FormatTokensRewrite] var maxClaimed = -1 private[rewrite] def getClaimed(ftIdx: Int): Option[(Int, Replacement)] = claimed.get(ftIdx) match { @@ -366,6 +368,8 @@ object FormatTokensRewrite { tokens(claimedIdx) = repl claimedIdx } else { + require(ftIdx > maxClaimed, s"claiming token at $ftIdx <= $maxClaimed") + maxClaimed = ftIdx if (preClaimed) claimed.update(ftIdx, idx) tokens.append(repl)