Skip to content

Commit 42b6e52

Browse files
committed
fix: convert bytes offsets correctly to chars
1 parent 69ac6e6 commit 42b6e52

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/main/kotlin/com/github/xepozz/mago/qualityTool/MagoMessageProcessor.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,16 @@ class MagoMessageProcessor(private val info: QualityToolAnnotatorInfo<*>) : Qual
4848
// it.file.endsWith(currentFilePath)
4949
// }
5050
.map { problem ->
51+
/**
52+
* temporary convert to bytes-offset to chars offset
53+
*/
54+
val range = byteRangeToCharRange(file.text, problem.startChar, problem.endChar)
55+
val textRange = TextRange(range.first, range.last + 1)
56+
// val textRange = TextRange(problem.startChar, problem.endChar)
57+
5158
QualityToolMessage(
5259
this,
53-
TextRange(problem.startChar, problem.endChar),
60+
textRange,
5461
problem.severity,
5562
problem.message,
5663
MagoReformatFileAction(info.project),
@@ -64,4 +71,11 @@ class MagoMessageProcessor(private val info: QualityToolAnnotatorInfo<*>) : Qual
6471
}
6572
.forEach { addMessage(it) }
6673
}
74+
75+
private fun byteRangeToCharRange(text: String, byteStart: Int, byteEnd: Int): IntRange {
76+
val bytes = text.toByteArray(Charsets.UTF_8)
77+
val charStart = String(bytes.copyOf(byteStart), Charsets.UTF_8).length
78+
val charEnd = String(bytes.copyOf(byteEnd), Charsets.UTF_8).length
79+
return charStart until charEnd
80+
}
6781
}

0 commit comments

Comments
 (0)