QmlJSEditor: Improving ComponentFromObjectDef

Now the use can choose which properties are defined
inside the component and which properties are kept in the
original file. To make the concept clear the dialog contains
a snippet of the generated code.

Change-Id: I92f9f241c9780345dd03b991232c4a811356c266
Reviewed-by: Tim Jenssen <tim.jenssen@theqtcompany.com>
This commit is contained in:
Thomas Hartmann
2016-04-29 10:31:21 +02:00
parent 1ba0e5cdf9
commit 5c45e9ec34
4 changed files with 208 additions and 54 deletions

View File

@@ -35,6 +35,8 @@
#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljsutils.h>
#include <qmljs/qmljspropertyreader.h>
#include <qmljs/qmljsrewriter.h>
#include <qmljstools/qmljsrefactoringchanges.h>
#include <projectexplorer/session.h>
#include <projectexplorer/projectnodes.h>
@@ -61,6 +63,7 @@ class Operation: public QmlJSQuickFixOperation
QString m_idName, m_componentName;
SourceLocation m_firstSourceLocation;
SourceLocation m_lastSourceLocation;
UiObjectInitializer *m_initializer;
public:
void init()
{
@@ -78,7 +81,8 @@ public:
: QmlJSQuickFixOperation(interface, 0),
m_idName(idOfObject(objDef)),
m_firstSourceLocation(objDef->firstSourceLocation()),
m_lastSourceLocation(objDef->lastSourceLocation())
m_lastSourceLocation(objDef->lastSourceLocation()),
m_initializer(objDef->initializer)
{
init();
}
@@ -88,7 +92,8 @@ public:
: QmlJSQuickFixOperation(interface, 0),
m_idName(idOfObject(objDef)),
m_firstSourceLocation(objDef->qualifiedTypeNameId->firstSourceLocation()),
m_lastSourceLocation(objDef->lastSourceLocation())
m_lastSourceLocation(objDef->lastSourceLocation()),
m_initializer(objDef->initializer)
{
init();
}
@@ -98,8 +103,33 @@ public:
{
QString componentName = m_componentName;
QString path = QFileInfo(fileName()).path();
bool confirm = ComponentNameDialog::go(&componentName, &path, Core::ICore::dialogParent());
QmlJS::PropertyReader propertyReader(currentFile->qmljsDocument(), m_initializer);
QStringList result;
QStringList sourcePreview;
if (!m_idName.isEmpty())
sourcePreview.append(QLatin1String(" id: ") + m_idName);
else
sourcePreview.append(QString());
QStringList sortedPropertiesWithoutId;
foreach (const QString &property, propertyReader.properties())
if (property != QLatin1String("id"))
sortedPropertiesWithoutId.append(property);
sortedPropertiesWithoutId.sort();
foreach (const QString &property, sortedPropertiesWithoutId)
sourcePreview.append(QLatin1String(" ") + property + QLatin1String(": ") + propertyReader.readAstValue(property));
bool confirm = ComponentNameDialog::go(&componentName, &path,
sortedPropertiesWithoutId,
sourcePreview,
QFileInfo(fileName()).fileName(),
&result,
Core::ICore::dialogParent());
if (!confirm)
return;
@@ -112,18 +142,44 @@ public:
QString imports;
UiProgram *prog = currentFile->qmljsDocument()->qmlProgram();
if (prog && prog->headers) {
const int start = currentFile->startOf(prog->headers->firstSourceLocation());
const int end = currentFile->startOf(prog->members->member->firstSourceLocation());
const unsigned int start = currentFile->startOf(prog->headers->firstSourceLocation());
const unsigned int end = currentFile->startOf(prog->members->member->firstSourceLocation());
imports = currentFile->textOf(start, end);
}
const int start = currentFile->startOf(m_firstSourceLocation);
const int end = currentFile->startOf(m_lastSourceLocation);
const QString txt = imports + currentFile->textOf(start, end)
const unsigned int start = currentFile->startOf(m_firstSourceLocation);
const unsigned int end = currentFile->startOf(m_lastSourceLocation);
QString newComponentSource = imports + currentFile->textOf(start, end)
+ QLatin1String("}\n");
//Remove properties from resulting code...
Utils::ChangeSet changeSet;
QmlJS::Rewriter rewriter(newComponentSource, &changeSet, QStringList());
QmlJS::Dialect dialect = QmlJS::Dialect::Qml;
QmlJS::Document::MutablePtr doc = QmlJS::Document::create(newFileName, dialect);
doc->setSource(newComponentSource);
doc->parseQml();
if (doc->isParsedCorrectly()) {
UiObjectMember *astRootNode = 0;
if (UiProgram *program = doc->qmlProgram())
if (program->members)
astRootNode = program->members->member;
foreach (const QString &property, result)
rewriter.removeBindingByName(initializerOfObject(astRootNode), property);
} else {
qWarning() << Q_FUNC_INFO << "parsing failed:" << newComponentSource;
}
changeSet.apply(&newComponentSource);
// stop if we can't create the new file
if (!refactoring.createFile(newFileName, txt))
if (!refactoring.createFile(newFileName, newComponentSource))
return;
if (path == QFileInfo(fileName()).path()) {
@@ -151,10 +207,14 @@ public:
Core::VcsManager::msgToAddToVcsFailed(QStringList(newFileName), versionControl));
}
}
QString replacement = componentName + QLatin1String(" {\n");
if (!m_idName.isEmpty())
replacement += QLatin1String("id: ") + m_idName + QLatin1Char('\n');
foreach (const QString &property, result)
replacement += property + QLatin1String(": ") + propertyReader.readAstValue(property) + QLatin1Char('\n');
Utils::ChangeSet changes;
changes.replace(start, end, replacement);
currentFile->setChangeSet(changes);
@@ -174,6 +234,7 @@ void ComponentFromObjectDef::match(const QmlJSQuickFixInterface &interface, Quic
for (int i = path.size() - 1; i >= 0; --i) {
Node *node = path.at(i);
if (UiObjectDefinition *objDef = cast<UiObjectDefinition *>(node)) {
if (!interface->currentFile()->isCursorOn(objDef->qualifiedTypeNameId))
return;
// check that the node is not the root node