[Bug]: TextEditor::getTotalTextHeight() adds height for a newline twice

(I’ve created an issue and PR over on GitHub, but I’m posting this here because — let’s be honest — it looks like the GitHub issue/PR lists aren’t checked that often.)

  1. Create a juce::TextEditor, enable multi-line support and set the text to "a".
  2. Set a breakpoint in juce::TextEditor::getTotalTextHeight()
  3. Type in a newline after the "a"

The juce::TextEditor::getTotalTextHeight() function loops over all sections and atoms using while (next()) {}.

int getTotalTextHeight()
    while (next()) {}

    auto height = lineY + lineHeight + getYOffset();

    if (atom != nullptr && atom->isNewLine())
        height += lineHeight;

    return roundToInt (height);

The last of the next() calls ends up in moveToEndOfLastAtom():

void moveToEndOfLastAtom()
        if (atom != nullptr)
            atomX = atomRight;

            if (atom->isNewLine())
                atomX = getJustificationOffsetX (0);
                lineY += lineHeight * lineSpacing;

As you can ssee, movetoEndOfLastAtom() already adds a lineHeight if the last atom is a new line. But then when we step out back into getTotalTextHeight(), this function also adds extra height when the last character is a new line!

The total height ends up with an extra line, as can be verified in the debugger. If you look at the UI and the text height is bigger than the component height, you’ll see that the scroll bar has some “extra” space at the bottom for the double newline.