2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-10-19 13:48:10 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-10-19 13:48:10 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-10-19 13:48:10 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2011-10-19 13:48:10 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-10-19 13:48:10 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-10-19 13:48:10 +02:00
|
|
|
|
|
|
|
|
#include "qmljswrapinloader.h"
|
|
|
|
|
#include "qmljsquickfixassist.h"
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
#include <coreplugin/idocument.h>
|
2011-10-19 13:48:10 +02:00
|
|
|
|
|
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
|
|
|
|
#include <qmljs/qmljsdocument.h>
|
2015-03-04 16:46:23 +01:00
|
|
|
#include <qmljs/qmljsscopechain.h>
|
2011-10-19 13:48:10 +02:00
|
|
|
#include <qmljs/qmljsutils.h>
|
|
|
|
|
#include <qmljs/qmljsbind.h>
|
|
|
|
|
#include <qmljstools/qmljsrefactoringchanges.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QCoreApplication>
|
2011-10-19 13:48:10 +02:00
|
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
|
using namespace QmlJS::AST;
|
|
|
|
|
using namespace QmlJSTools;
|
|
|
|
|
|
2013-06-03 19:31:32 +02:00
|
|
|
namespace QmlJSEditor {
|
|
|
|
|
|
|
|
|
|
using namespace Internal;
|
|
|
|
|
|
2011-10-19 13:48:10 +02:00
|
|
|
namespace {
|
|
|
|
|
|
2011-11-04 13:59:23 +01:00
|
|
|
class FindIds : protected Visitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-11-24 02:45:30 +01:00
|
|
|
using Result = QHash<QString, SourceLocation>;
|
2011-11-04 13:59:23 +01:00
|
|
|
|
|
|
|
|
Result operator()(Node *node)
|
|
|
|
|
{
|
|
|
|
|
result.clear();
|
|
|
|
|
Node::accept(node, this);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(UiObjectInitializer *ast) override
|
2011-11-04 13:59:23 +01:00
|
|
|
{
|
|
|
|
|
UiScriptBinding *idBinding;
|
|
|
|
|
QString id = idOfObject(ast, &idBinding);
|
|
|
|
|
if (!id.isEmpty())
|
|
|
|
|
result[id] = locationFromRange(idBinding->statement);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result result;
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-10 11:46:07 +02:00
|
|
|
template <typename T>
|
2011-10-19 13:48:10 +02:00
|
|
|
class Operation: public QmlJSQuickFixOperation
|
|
|
|
|
{
|
2011-12-13 16:17:43 +01:00
|
|
|
Q_DECLARE_TR_FUNCTIONS(QmlJSEditor::Internal::Operation)
|
|
|
|
|
|
2014-09-10 11:46:07 +02:00
|
|
|
T *m_objDef;
|
2011-10-19 13:48:10 +02:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Operation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface,
|
2014-09-10 11:46:07 +02:00
|
|
|
T *objDef)
|
2011-10-19 13:48:10 +02:00
|
|
|
: QmlJSQuickFixOperation(interface, 0)
|
|
|
|
|
, m_objDef(objDef)
|
|
|
|
|
{
|
2018-11-24 02:45:30 +01:00
|
|
|
Q_ASSERT(m_objDef);
|
2011-10-19 13:48:10 +02:00
|
|
|
|
2011-12-13 16:17:43 +01:00
|
|
|
setDescription(tr("Wrap Component in Loader"));
|
2011-10-19 13:48:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString findFreeName(const QString &base)
|
|
|
|
|
{
|
|
|
|
|
QString tryName = base;
|
|
|
|
|
int extraNumber = 1;
|
2018-11-24 02:45:30 +01:00
|
|
|
const ObjectValue *found = nullptr;
|
2011-10-19 13:48:10 +02:00
|
|
|
const ScopeChain &scope = assistInterface()->semanticInfo().scopeChain();
|
|
|
|
|
forever {
|
|
|
|
|
scope.lookup(tryName, &found);
|
|
|
|
|
if (!found || extraNumber > 1000)
|
|
|
|
|
break;
|
|
|
|
|
tryName = base + QString::number(extraNumber++);
|
|
|
|
|
}
|
|
|
|
|
return tryName;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void performChanges(QmlJSRefactoringFilePtr currentFile,
|
|
|
|
|
const QmlJSRefactoringChanges &) override
|
2011-10-19 13:48:10 +02:00
|
|
|
{
|
|
|
|
|
UiScriptBinding *idBinding;
|
|
|
|
|
const QString id = idOfObject(m_objDef, &idBinding);
|
|
|
|
|
QString baseName = id;
|
|
|
|
|
if (baseName.isEmpty()) {
|
|
|
|
|
for (UiQualifiedId *it = m_objDef->qualifiedTypeNameId; it; it = it->next) {
|
|
|
|
|
if (!it->next)
|
|
|
|
|
baseName = it->name.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find ids
|
|
|
|
|
const QString componentId = findFreeName(QLatin1String("component_") + baseName);
|
|
|
|
|
const QString loaderId = findFreeName(QLatin1String("loader_") + baseName);
|
|
|
|
|
|
|
|
|
|
Utils::ChangeSet changes;
|
2011-11-04 13:59:23 +01:00
|
|
|
|
|
|
|
|
FindIds::Result innerIds = FindIds()(m_objDef);
|
|
|
|
|
innerIds.remove(id);
|
|
|
|
|
|
2011-12-13 16:17:43 +01:00
|
|
|
QString comment = tr("// TODO: Move position bindings from the component to the Loader.\n"
|
2013-10-17 13:48:04 +02:00
|
|
|
"// Check all uses of 'parent' inside the root element of the component.")
|
|
|
|
|
+ QLatin1Char('\n');
|
2011-10-19 13:48:10 +02:00
|
|
|
if (idBinding) {
|
2014-04-17 14:09:47 +02:00
|
|
|
comment += tr("// Rename all outer uses of the id \"%1\" to \"%2.item\".").arg(
|
2013-10-17 13:48:04 +02:00
|
|
|
id, loaderId) + QLatin1Char('\n');
|
2011-10-19 13:48:10 +02:00
|
|
|
}
|
2011-11-04 13:59:23 +01:00
|
|
|
|
|
|
|
|
// handle inner ids
|
|
|
|
|
QString innerIdForwarders;
|
|
|
|
|
QHashIterator<QString, SourceLocation> it(innerIds);
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
it.next();
|
|
|
|
|
const QString innerId = it.key();
|
2014-04-17 14:09:47 +02:00
|
|
|
comment += tr("// Rename all outer uses of the id \"%1\" to \"%2.item.%1\".\n").arg(
|
2011-11-04 13:59:23 +01:00
|
|
|
innerId, loaderId);
|
2012-11-26 21:17:34 +02:00
|
|
|
changes.replace(it.value().begin(), it.value().end(), QString::fromLatin1("inner_%1").arg(innerId));
|
|
|
|
|
innerIdForwarders += QString::fromLatin1("\nproperty alias %1: inner_%1").arg(innerId);
|
2011-11-04 13:59:23 +01:00
|
|
|
}
|
|
|
|
|
if (!innerIdForwarders.isEmpty()) {
|
|
|
|
|
innerIdForwarders.append(QLatin1Char('\n'));
|
|
|
|
|
const int afterOpenBrace = m_objDef->initializer->lbraceToken.end();
|
|
|
|
|
changes.insert(afterOpenBrace, innerIdForwarders);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-10 11:46:07 +02:00
|
|
|
const int objDefStart = m_objDef->qualifiedTypeNameId->firstSourceLocation().begin();
|
2011-11-04 13:59:23 +01:00
|
|
|
const int objDefEnd = m_objDef->lastSourceLocation().end();
|
2011-10-19 13:48:10 +02:00
|
|
|
changes.insert(objDefStart, comment +
|
2012-11-26 21:17:34 +02:00
|
|
|
QString::fromLatin1("Component {\n"
|
|
|
|
|
" id: %1\n").arg(componentId));
|
|
|
|
|
changes.insert(objDefEnd, QString::fromLatin1("\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"Loader {\n"
|
|
|
|
|
" id: %2\n"
|
|
|
|
|
" sourceComponent: %1\n"
|
|
|
|
|
"}\n").arg(componentId, loaderId));
|
2011-10-19 13:48:10 +02:00
|
|
|
currentFile->setChangeSet(changes);
|
|
|
|
|
currentFile->appendIndentRange(Range(objDefStart, objDefEnd));
|
|
|
|
|
currentFile->apply();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
2017-12-18 12:23:28 +01:00
|
|
|
void matchWrapInLoaderQuickFix(const QmlJSQuickFixInterface &interface, QuickFixOperations &result)
|
2011-10-19 13:48:10 +02:00
|
|
|
{
|
|
|
|
|
const int pos = interface->currentFile()->cursor().position();
|
|
|
|
|
|
|
|
|
|
QList<Node *> path = interface->semanticInfo().rangePath(pos);
|
|
|
|
|
for (int i = path.size() - 1; i >= 0; --i) {
|
|
|
|
|
Node *node = path.at(i);
|
2018-11-24 02:45:30 +01:00
|
|
|
if (auto objDef = cast<UiObjectDefinition *>(node)) {
|
2011-10-19 13:48:10 +02:00
|
|
|
if (!interface->currentFile()->isCursorOn(objDef->qualifiedTypeNameId))
|
2012-10-10 23:27:16 +02:00
|
|
|
return;
|
2011-10-19 13:48:10 +02:00
|
|
|
// check that the node is not the root node
|
|
|
|
|
if (i > 0 && !cast<UiProgram*>(path.at(i - 1))) {
|
2016-11-18 11:53:38 +01:00
|
|
|
result << new Operation<UiObjectDefinition>(interface, objDef);
|
2012-10-10 23:27:16 +02:00
|
|
|
return;
|
2011-10-19 13:48:10 +02:00
|
|
|
}
|
2018-11-24 02:45:30 +01:00
|
|
|
} else if (auto objBinding = cast<UiObjectBinding *>(node)) {
|
2014-09-10 11:46:07 +02:00
|
|
|
if (!interface->currentFile()->isCursorOn(objBinding->qualifiedTypeNameId))
|
|
|
|
|
return;
|
2016-11-18 11:53:38 +01:00
|
|
|
result << new Operation<UiObjectBinding>(interface, objBinding);
|
2014-09-10 11:46:07 +02:00
|
|
|
return;
|
2011-10-19 13:48:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-03 19:31:32 +02:00
|
|
|
|
|
|
|
|
} // namespace QmlJSEditor
|