forked from qt-creator/qt-creator
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:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user