aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@digia.com>2014-03-07 11:29:35 +0100
committerEike Ziller <eike.ziller@digia.com>2014-06-06 13:37:35 +0200
commitade2710725bf717ab9ef830dd9a38d90e6d47a1c (patch)
tree534ad795f0202b746f3f34ad9643de2743b7778b
parent73360f8acb5757326ea5755216bab798592de5dc (diff)
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 <david.schulz@digia.com>
-rw-r--r--src/plugins/texteditor/basetexteditor.cpp13
-rw-r--r--src/plugins/texteditor/basetexteditor_p.h1
2 files changed, 12 insertions, 2 deletions
diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index 998db4545a..9c7cd60364 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<BaseTextDocumentLayout*>(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 7f2d65ebc7..0748e0141d 100644
--- a/src/plugins/texteditor/basetexteditor_p.h
+++ b/src/plugins/texteditor/basetexteditor_p.h
@@ -205,6 +205,7 @@ public:
QPointer<BaseTextEditorAnimator> m_animator;
int m_cursorBlockNumber;
+ int m_blockCount;
QPoint m_markDragStart;
bool m_markDragging;