Commit 76a106c4 authored by Erik Verbruggen's avatar Erik Verbruggen

Changed rewriting error propagation slightly to suppress the TextToModelMerger from running.

parent d8188399
......@@ -140,6 +140,9 @@ public:
void setErrors(const QList<Error> &errors);
void addError(const Error &error);
void enterErrorState(const QString &errorMessage);
bool inErrorState() const { return !m_rewritingErrorMessage.isEmpty(); }
void leaveErrorState() { m_rewritingErrorMessage = QString::null; }
void resetToLastCorrectQml();
QMap<ModelNode, QString> extractText(const QList<ModelNode> &nodes) const;
......@@ -173,7 +176,7 @@ private: //variables
QList<Error> m_errors;
int transactionLevel;
RewriterTransaction m_removeDefaultPropertyTransaction;
bool errorState;
QString m_rewritingErrorMessage;
QString lastCorrectQmlSource;
};
......
......@@ -191,10 +191,10 @@ RewriterView *ModelToTextMerger::view()
return m_rewriterView;
}
bool ModelToTextMerger::applyChanges()
void ModelToTextMerger::applyChanges()
{
if (m_rewriteActions.isEmpty())
return true;
return;
dumpRewriteActions(QLatin1String("Before compression"));
RewriteActionCompressor compress(getPropertyOrder());
......@@ -202,20 +202,24 @@ bool ModelToTextMerger::applyChanges()
dumpRewriteActions(QLatin1String("After compression"));
if (m_rewriteActions.isEmpty())
return true;
return;
Document::Ptr tmpDocument(Document::create(QLatin1String("<ModelToTextMerger>")));
tmpDocument->setSource(m_rewriterView->textModifier()->text());
if (!tmpDocument->parseQml()) {
qDebug() << "*** Possible problem: QML file wasn't parsed correctly.";
qDebug() << "*** QML text:" << m_rewriterView->textModifier()->text();
return false;
QString errorMessage = QLatin1String("Error while rewriting");
if (!tmpDocument->diagnosticMessages().isEmpty())
errorMessage = tmpDocument->diagnosticMessages().first().message;
m_rewriterView->enterErrorState(errorMessage);
return;
}
TextModifier *textModifier = m_rewriterView->textModifier();
bool success = true;
try {
ModelNodePositionRecalculator positionRecalculator(m_rewriterView->positionStorage(), m_rewriterView->positionStorage()->modelNodes());
positionRecalculator.connectTo(textModifier);
......@@ -226,28 +230,33 @@ bool ModelToTextMerger::applyChanges()
textModifier->startGroup();
for (int i = 0; i < m_rewriteActions.size(); ++i) {
if (i != 0) {
textModifier->flushGroup();
success = refactoring.reparseDocument();
}
RewriteAction* action = m_rewriteActions.at(i);
if (DebugRewriteActions) {
qDebug() << "Next rewrite action:" << qPrintable(action->info());
}
ModelNodePositionStorage *positionStore = m_rewriterView->positionStorage();
if (success)
success = action->execute(refactoring, *positionStore);
bool success = action->execute(refactoring, *positionStore);
if (success) {
textModifier->flushGroup();
success = refactoring.reparseDocument();
}
// don't merge these two if statements, because the previous then-part changes the value
// of "success" !
if (!success) {
m_rewriterView->enterErrorState(QLatin1String("Error rewriting document"));
if (!success /*&& DebugRewriteActions*/) {
qDebug() << "*** QML source code: ***";
qDebug() << qPrintable(textModifier->text());
qDebug() << "*** End of QML source code. ***";
if (true || DebugRewriteActions) {
qDebug() << "*** QML source code: ***";
qDebug() << qPrintable(textModifier->text());
qDebug() << "*** End of QML source code. ***";
}
break;
}
}
qDeleteAll(m_rewriteActions);
m_rewriteActions.clear();
......@@ -256,16 +265,14 @@ bool ModelToTextMerger::applyChanges()
reindent(positionRecalculator.dirtyAreas());
textModifier->reactivateChangeSignals();
} catch (...) {
} catch (Exception &e) {
m_rewriterView->enterErrorState(e.description());
qDeleteAll(m_rewriteActions);
m_rewriteActions.clear();
textModifier->commitGroup();
textModifier->reactivateChangeSignals();
throw;
}
return success;
}
void ModelToTextMerger::reindent(const QMap<int, int> &/*dirtyAreas*/) const
......
......@@ -57,7 +57,7 @@ class ModelToTextMerger
public:
ModelToTextMerger(RewriterView *reWriterView);
bool applyChanges(); // Note: his method might throw exceptions, as the model works this way. So to handle rewriting failures, you will also need to catch any exception coming out.
void applyChanges(); // Note: his method might throw exceptions, as the model works this way. So to handle rewriting failures, you will also need to catch any exception coming out.
void nodeCreated(const ModelNode &createdNode);
void nodeRemoved(const ModelNode &removedNode, const NodeAbstractProperty &parentProperty, PropertyChangeFlags propertyChange);
......
......@@ -115,8 +115,7 @@ RewriterView::RewriterView(DifferenceHandling differenceHandling, QObject *paren
m_modelToTextMerger(new Internal::ModelToTextMerger(this)),
m_textToModelMerger(new Internal::TextToModelMerger(this)),
m_textModifier(0),
transactionLevel(0),
errorState(false)
transactionLevel(0)
{
}
......@@ -393,23 +392,19 @@ void RewriterView::setupComponent(const ModelNode &node)
void RewriterView::applyChanges()
{
if (errorState) {
if (inErrorState()) {
qDebug() << "RewriterView::applyChanges() got called while in error state. Will do a quick-exit now.";
throw RewritingException(__LINE__, __FUNCTION__, __FILE__, "RewriterView::applyChanges() already in error state");
}
bool success = false;
try {
success = modelToTextMerger()->applyChanges();
if (!success) {
errorState = true;
throw RewritingException(__LINE__, __FUNCTION__, __FILE__, errors().first().description());
}
modelToTextMerger()->applyChanges();
} catch (Exception &e) {
errorState = true;
throw RewritingException(__LINE__, __FUNCTION__, __FILE__, e.description());
enterErrorState(e.description());
}
if (inErrorState()) {
throw RewritingException(__LINE__, __FUNCTION__, __FILE__, m_rewritingErrorMessage);
}
}
......@@ -436,11 +431,17 @@ void RewriterView::addError(const RewriterView::Error &error)
emit errorsChanged(m_errors);
}
void RewriterView::enterErrorState(const QString &errorMessage)
{
m_rewritingErrorMessage = errorMessage;
}
void RewriterView::resetToLastCorrectQml()
{
ModelAmender differenceHandler(m_textToModelMerger.data());
m_textToModelMerger->load(lastCorrectQmlSource.toUtf8(), differenceHandler);
errorState = false;
leaveErrorState();
}
QMap<ModelNode, QString> RewriterView::extractText(const QList<ModelNode> &nodes) const
......@@ -487,6 +488,9 @@ int RewriterView::firstDefinitionInsideLength(const ModelNode &node) const
void RewriterView::qmlTextChanged()
{
if (inErrorState())
return;
if (m_textToModelMerger && m_textModifier) {
const QString newQmlText = m_textModifier->text();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment