1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

TextEditor: Fix crash when the caret is beyond a newline in the last position

This commit is contained in:
attila 2025-03-18 13:07:07 +01:00 committed by Attila Szarvas
parent 53b96db0d5
commit ae4bca24f4

View file

@ -2156,6 +2156,14 @@ std::pair<Point<float>, float> TextEditor::getCursorEdge (const CaretState& temp
if (textStorage->isEmpty()) if (textStorage->isEmpty())
return { { getJustificationOffsetX(), 0.0f }, currentFont.getHeight() }; return { { getJustificationOffsetX(), 0.0f }, currentFont.getHeight() };
if (visualIndex == getTotalNumChars())
{
const auto& lastParagraph = textStorage->back().value;
return { { getJustificationOffsetX(), lastParagraph->getTop() + lastParagraph->getHeight() },
currentFont.getHeight() };
}
return getTextSelectionEdge (visualIndex, tempCaret.getEdge()); return getTextSelectionEdge (visualIndex, tempCaret.getEdge());
} }
@ -2280,17 +2288,19 @@ TextEditor::CaretState TextEditor::CaretState::withPreferredEdge (Edge newEdge)
void TextEditor::CaretState::updateEdge() void TextEditor::CaretState::updateEdge()
{ {
jassert (0 <= position && position <= owner.getTotalNumChars()); // The position can be temporarily outside the current text's bounds. It's the TextEditor's
// responsibility to update the caret position after editing operations.
const auto clampedPosition = std::clamp (position, 0, owner.getTotalNumChars());
if (position == 0) if (clampedPosition == 0)
{ {
edge = Edge::leading; edge = Edge::leading;
} }
else if (owner.getText()[position - 1] == '\n') else if (owner.getText()[clampedPosition - 1] == '\n')
{ {
edge = Edge::leading; edge = Edge::leading;
} }
else if (position == owner.getTotalNumChars()) else if (clampedPosition == owner.getTotalNumChars())
{ {
edge = Edge::trailing; edge = Edge::trailing;
} }