diff --git a/src/utils/vvim.cpp b/src/utils/vvim.cpp index 2a39b1a4..348ff54b 100644 --- a/src/utils/vvim.cpp +++ b/src/utils/vvim.cpp @@ -4410,6 +4410,11 @@ void VVim::processJumpLocationAction(QList &p_tokens, bool p_next) pib = block.length() - 1; } + if (!m_editor->isBlockVisible(block)) { + // Scroll the block to the center of screen. + VEditUtils::scrollBlockInPage(m_editor, block.blockNumber(), 1); + } + cursor.setPosition(block.position() + pib); m_editor->setTextCursor(cursor); } diff --git a/src/vedit.cpp b/src/vedit.cpp index f66c9588..06b64e8e 100644 --- a/src/vedit.cpp +++ b/src/vedit.cpp @@ -1230,3 +1230,29 @@ void VEdit::makeBlockVisible(const QTextBlock &p_block) qDebug() << "scroll page up to make block visible"; } } + +bool VEdit::isBlockVisible(const QTextBlock &p_block) +{ + if (!p_block.isValid() || !p_block.isVisible()) { + return false; + } + + QScrollBar *vbar = verticalScrollBar(); + if (!vbar || !vbar->isVisible()) { + // No vertical scrollbar. + return true; + } + + QAbstractTextDocumentLayout *layout = document()->documentLayout(); + int height = rect().height(); + QScrollBar *hbar = horizontalScrollBar(); + if (hbar && hbar->isVisible()) { + height -= hbar->height(); + } + + QRectF rect = layout->blockBoundingRect(p_block); + int y = contentOffsetY() + (int)rect.y(); + int rectHeight = (int)rect.height(); + + return (y >= 0 && y < height) || (y < 0 && y + rectHeight > 0); +} diff --git a/src/vedit.h b/src/vedit.h index d10f10e7..83c6692d 100644 --- a/src/vedit.h +++ b/src/vedit.h @@ -129,6 +129,8 @@ public: // Will not change current cursor. void makeBlockVisible(const QTextBlock &p_block); + bool isBlockVisible(const QTextBlock &p_block); + signals: // Request VEditTab to save and exit edit mode. void saveAndRead();