QmlDesigner: Sanitze QML file on save

Before writing the annotations we also sanitize the QML file.
This removes dangling PropertyChanges and dangling KeyFrameGroups.

Those are simply noise and have no relevance. We only sanitize
ui.qml files.

Change-Id: If4bb993b808f9d96ab89296117e776e8e9eec2c3
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
This commit is contained in:
Thomas Hartmann
2021-10-06 13:14:16 +02:00
committed by Thomas Hartmann
parent e3d9ba8f59
commit b071603175
3 changed files with 39 additions and 1 deletions

View File

@@ -634,8 +634,11 @@ void DesignDocument::setEditor(Core::IEditor *editor)
connect(Core::EditorManager::instance(), &Core::EditorManager::aboutToSave,
this, [this](Core::IDocument *document) {
if (m_textEditor && m_textEditor->document() == document) {
if (m_documentModel && m_documentModel->rewriterView())
if (m_documentModel && m_documentModel->rewriterView()) {
if (fileName().completeSuffix() == "ui.qml")
m_documentModel->rewriterView()->sanitizeModel();
m_documentModel->rewriterView()->writeAuxiliaryData();
}
}
});

View File

@@ -177,6 +177,8 @@ public:
ModelNode getNodeForCanonicalIndex(int index);
void sanitizeModel();
signals:
void modelInterfaceProjectUpdated();

View File

@@ -39,6 +39,8 @@
#include <modelnodepositionstorage.h>
#include <modelnode.h>
#include <nodeproperty.h>
#include <qmlobjectnode.h>
#include <qmltimelinekeyframegroup.h>
#ifndef QMLDESIGNER_TEST
#include <qmldesignerplugin.h>
@@ -57,6 +59,7 @@
#include <utility>
#include <vector>
#include <algorithm>
using namespace QmlDesigner::Internal;
@@ -658,6 +661,36 @@ ModelNode RewriterView::getNodeForCanonicalIndex(int index)
return m_canonicalIntModelNode.value(index);
}
void RewriterView::sanitizeModel()
{
if (inErrorState())
return;
QmlObjectNode root = rootModelNode();
QTC_ASSERT(root.isValid(), return);
QList<ModelNode> danglingNodes;
const auto danglingStates = root.allInvalidStateOperations();
const auto danglingKeyframeGroups = QmlTimelineKeyframeGroup::allInvalidTimelineKeyframeGroups(this);
std::transform(danglingStates.begin(),
danglingStates.end(),
std::back_inserter(danglingNodes),
[](const auto &node) { return node.modelNode(); });
std::transform(danglingKeyframeGroups.begin(),
danglingKeyframeGroups.end(),
std::back_inserter(danglingNodes),
[](const auto &node) { return node.modelNode(); });
executeInTransaction("RewriterView::sanitizeModel", [&]() {
for (auto node : std::as_const(danglingNodes))
node.destroy();
});
}
Internal::ModelNodePositionStorage *RewriterView::positionStorage() const
{
return m_positionStorage.data();