From ade2710725bf717ab9ef830dd9a38d90e6d47a1c Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Fri, 7 Mar 2014 11:29:35 +0100 Subject: [PATCH] TextEditors: Keep visible position in editor for 'external' changes If contents of a text editor change from outside the editor, the visible portion of the text in the viewport should stay the same. That is especially apparent when opening a document in a split view, and adding/removing lines in one of them, above the first visible line in the other. Task-number: QTCREATORBUG-11486 Change-Id: I28cde17ecf98cb98c1d6f1259dc66d3671585ee3 Reviewed-by: David Schulz --- src/plugins/texteditor/basetexteditor.cpp | 13 +++++++++++-- src/plugins/texteditor/basetexteditor_p.h | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 998db4545a6..9c7cd603646 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -614,13 +614,13 @@ void BaseTextEditorWidget::editorContentsChange(int position, int charsRemoved, d->m_contentsChanged = true; QTextDocument *doc = document(); BaseTextDocumentLayout *documentLayout = static_cast(doc->documentLayout()); + const QTextBlock posBlock = doc->findBlock(position); // Keep the line numbers and the block information for the text marks updated if (charsRemoved != 0) { documentLayout->updateMarksLineNumber(); - documentLayout->updateMarksBlock(document()->findBlock(position)); + documentLayout->updateMarksBlock(posBlock); } else { - const QTextBlock posBlock = doc->findBlock(position); const QTextBlock nextBlock = doc->findBlock(position + charsAdded); if (posBlock != nextBlock) { documentLayout->updateMarksLineNumber(); @@ -639,6 +639,14 @@ void BaseTextEditorWidget::editorContentsChange(int position, int charsRemoved, if (charsAdded != 0 && document()->characterAt(position + charsAdded - 1).isPrint()) d->m_assistRelevantContentAdded = true; + + int newBlockCount = doc->blockCount(); + if (!hasFocus() && newBlockCount != d->m_blockCount) { + // lines were inserted or removed from outside, keep viewport on same part of text + if (firstVisibleBlock().blockNumber() > posBlock.blockNumber()) + verticalScrollBar()->setValue(verticalScrollBar()->value() + newBlockCount - d->m_blockCount); + } + d->m_blockCount = newBlockCount; } void BaseTextEditorWidget::slotSelectionChanged() @@ -2390,6 +2398,7 @@ BaseTextEditorWidgetPrivate::BaseTextEditorWidgetPrivate() m_codeAssistant(new CodeAssistant), m_assistRelevantContentAdded(false), m_cursorBlockNumber(-1), + m_blockCount(0), m_markDragging(false), m_autoCompleter(new AutoCompleter), m_clipboardAssistProvider(new Internal::ClipboardAssistProvider) diff --git a/src/plugins/texteditor/basetexteditor_p.h b/src/plugins/texteditor/basetexteditor_p.h index 7f2d65ebc72..0748e0141d9 100644 --- a/src/plugins/texteditor/basetexteditor_p.h +++ b/src/plugins/texteditor/basetexteditor_p.h @@ -205,6 +205,7 @@ public: QPointer m_animator; int m_cursorBlockNumber; + int m_blockCount; QPoint m_markDragStart; bool m_markDragging;