Skip to content

Commit

Permalink
fix exceptions and incorrect layout when inserting a line break at th…
Browse files Browse the repository at this point in the history
…e end of a BigText
  • Loading branch information
sunny-chung committed Oct 2, 2024
1 parent e75ffe5 commit 1832637
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ open class BigTextImpl(
val startPos = findRenderPositionStart(node)
return startPos + if (index - 1 - rowStart == node.value.rowBreakOffsets.size && node.value.isEndWithForceRowBreak) {
node.value.bufferLength
} else if (index > 0) {
} else if (index - 1 - rowStart > 0) {
node.value.rowBreakOffsets[index - 1 - rowStart] - node.value.renderBufferStart
} else {
0
Expand All @@ -176,7 +176,7 @@ open class BigTextImpl(
val (node, rowIndexStart) = tree.findNodeByRowBreaks(rowIndex - 1)!!
val rowOffset = if (rowIndex - 1 - rowIndexStart == node.value.rowBreakOffsets.size && node.value.isEndWithForceRowBreak) {
node.value.renderBufferEndExclusive
} else if (rowIndex > 0) {
} else if (rowIndex - 1 - rowIndexStart > 0) {
val i = rowIndex - 1 - rowIndexStart
if (i > node.value.rowBreakOffsets.lastIndex) {
throw IndexOutOfBoundsException("findLineIndexByRowIndex($rowIndex) rowBreakOffsets[$i] length ${node.value.rowBreakOffsets.size}")
Expand All @@ -187,7 +187,7 @@ open class BigTextImpl(
}
val positionStart = findRenderPositionStart(node)
val rowPositionStart = positionStart + rowOffset - node.value.renderBufferStart
val lineBreakPosition = rowPositionStart - 1
val lineBreakPosition = maxOf(0, rowPositionStart - 1)

val lineBreakAtNode = tree.findNodeByRenderCharIndex(lineBreakPosition)!!
val lineStart = findLineStart(lineBreakAtNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class BigTextTransformerImpl(internal val delegate: BigTextImpl) : BigTextImpl(
) {
require(pos in 0..originalLength) { "Out of bound. pos = $pos, originalLength = $originalLength" }

val renderPos = findTransformedPositionByOriginalPosition(pos)

insertChunkAtPosition(
position = pos,
chunkedStringLength = bufferOffsetEndExclusive - bufferOffsetStart,
Expand All @@ -121,6 +123,9 @@ class BigTextTransformerImpl(internal val delegate: BigTextImpl) : BigTextImpl(

leftStringLength = 0
}

val insertLength = bufferOffsetEndExclusive - bufferOffsetStart
layout(maxOf(0, renderPos - 1), minOf(length, renderPos + insertLength + 1))
}

private fun transformInsertChunkAtPosition(position: Int, chunkedString: CharSequence, offsetMapping: BigTextTransformOffsetMapping, incrementalTransformOffsetMappingLength: Int, isReplaceOriginal: Boolean): Int {
Expand Down

0 comments on commit 1832637

Please sign in to comment.