From ba4a604d687dc45f3cc97f2a32e59516a9343871 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 16 Mar 2020 10:39:30 +0100 Subject: [PATCH] Editors: Fix top/bottom split with cursor visible at end of document It did not center on the text cursor. - Fix lastVisibleBlockNumber() to return the last block if the editor is scrolled to the bottom (and there potentially is no block located at the very bottom edge of the editor widget). - Fix comparison of block numbers (0-based) with line numbers (1-based) Change-Id: I21405443bea3533e393a7cf320ded6d47f647949 Reviewed-by: David Schulz --- src/plugins/texteditor/texteditor.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index a38fab49b03..b2f2bacb5f8 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -3006,15 +3006,19 @@ bool TextEditorWidget::restoreState(const QByteArray &state) horizontalScrollBar()->setValue(hval); if (version >= 2) { - int firstBlock, lastBlock; - stream >> firstBlock; - stream >> lastBlock; + int originalFirstBlock, originalLastBlock; + stream >> originalFirstBlock; + stream >> originalLastBlock; // If current line was visible in the old state, make sure it is visible in the new state. // This can happen if the height of the editor changed in the meantime - if (firstBlock <= lineVal && lineVal <= lastBlock - && (lineVal < firstVisibleBlockNumber() || lastVisibleBlockNumber() <= lineVal)) { + const int lineBlock = lineVal - 1; // line is 1-based, blocks are 0-based + const bool originalCursorVisible = (originalFirstBlock <= lineBlock + && lineBlock <= originalLastBlock); + const int firstBlock = firstVisibleBlockNumber(); + const int lastBlock = lastVisibleBlockNumber(); + const bool cursorVisible = (firstBlock <= lineBlock && lineBlock <= lastBlock); + if (originalCursorVisible && !cursorVisible) centerCursor(); - } } d->saveCurrentCursorPositionForNavigation(); @@ -8456,8 +8460,11 @@ int TextEditorWidget::firstVisibleBlockNumber() const int TextEditorWidget::lastVisibleBlockNumber() const { QTextBlock block = blockForVerticalOffset(viewport()->height() - 1); - if (!block.isValid()) - block.previous(); + if (!block.isValid()) { + block = document()->lastBlock(); + while (block.isValid() && !block.isVisible()) + block = block.previous(); + } return block.isValid() ? block.blockNumber() : -1; }