QmlDesigner: Delay and compress text changes from editor

This is required to avoid blocking the ui while typing, because
the model is updated.
It will take 400ms without changes to the text until the model
gets updated.

Change-Id: Id428a52c15aeb8f0b802ee0448be8e6777a7402a
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Thomas Hartmann
2017-01-03 17:28:23 +01:00
parent 2322f70fb6
commit 3d48d7565c
2 changed files with 29 additions and 17 deletions

View File

@@ -31,6 +31,7 @@
#include "rewritererror.h"
#include <QScopedPointer>
#include <QTimer>
#include <QUrl>
namespace QmlJS {
@@ -170,6 +171,7 @@ protected: // functions
void setModificationGroupActive(bool active);
void applyModificationGroupChanges();
void applyChanges();
void amendQmlText();
private: //variables
TextModifier *m_textModifier = nullptr;
@@ -185,7 +187,8 @@ private: //variables
QList<RewriterError> m_warnings;
RewriterTransaction m_removeDefaultPropertyTransaction;
QString m_rewritingErrorMessage;
QString lastCorrectQmlSource;
QString m_lastCorrectQmlSource;
QTimer m_amendTimer;
};
} //QmlDesigner

View File

@@ -53,6 +53,8 @@ RewriterView::RewriterView(DifferenceHandling differenceHandling, QObject *paren
m_modelToTextMerger(new Internal::ModelToTextMerger(this)),
m_textToModelMerger(new Internal::TextToModelMerger(this))
{
m_amendTimer.setSingleShot(true);
connect(&m_amendTimer, &QTimer::timeout, this, &RewriterView::amendQmlText);
}
RewriterView::~RewriterView()
@@ -79,7 +81,7 @@ void RewriterView::modelAttached(Model *model)
ModelAmender differenceHandler(m_textToModelMerger.data());
const QString qmlSource = m_textModifier->text();
if (m_textToModelMerger->load(qmlSource, differenceHandler))
lastCorrectQmlSource = qmlSource;
m_lastCorrectQmlSource = qmlSource;
}
void RewriterView::modelAboutToBeDetached(Model * /*model*/)
@@ -411,6 +413,18 @@ void RewriterView::applyChanges()
}
}
void RewriterView::amendQmlText()
{
emitCustomNotification(StartRewriterAmend);
const QString newQmlText = m_textModifier->text();
ModelAmender differenceHandler(m_textToModelMerger.data());
if (m_textToModelMerger->load(newQmlText, differenceHandler))
m_lastCorrectQmlSource = newQmlText;
emitCustomNotification(EndRewriterAmend);
}
Internal::ModelNodePositionStorage *RewriterView::positionStorage() const
{
return m_positionStorage.data();
@@ -717,17 +731,12 @@ void RewriterView::qmlTextChanged()
case Validate: {
ModelValidator differenceHandler(m_textToModelMerger.data());
if (m_textToModelMerger->load(newQmlText, differenceHandler))
lastCorrectQmlSource = newQmlText;
m_lastCorrectQmlSource = newQmlText;
break;
}
case Amend:
default: {
emitCustomNotification(StartRewriterAmend);
ModelAmender differenceHandler(m_textToModelMerger.data());
if (m_textToModelMerger->load(newQmlText, differenceHandler))
lastCorrectQmlSource = newQmlText;
emitCustomNotification(EndRewriterAmend);
case Amend: {
m_amendTimer.start(400);
break;
}
}