2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-07-27 09:29:22 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2010-07-27 09:29:22 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-07-27 09:29:22 +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
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2010-07-27 09:29:22 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-07-27 09:29:22 +02:00
|
|
|
|
|
|
|
|
#include "qmljsquickfix.h"
|
|
|
|
|
#include "qmljscomponentfromobjectdef.h"
|
2011-10-19 13:48:10 +02:00
|
|
|
#include "qmljswrapinloader.h"
|
2010-07-27 09:29:22 +02:00
|
|
|
#include "qmljseditor.h"
|
2011-04-15 16:19:23 +02:00
|
|
|
#include "qmljsquickfixassist.h"
|
2010-07-27 09:29:22 +02:00
|
|
|
|
|
|
|
|
#include <extensionsystem/iplugin.h>
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
2010-11-11 10:05:05 +01:00
|
|
|
#include <qmljstools/qmljsrefactoringchanges.h>
|
2010-07-27 09:29:22 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QApplication>
|
2010-07-27 09:29:22 +02:00
|
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
|
using namespace QmlJS::AST;
|
2010-11-11 10:05:05 +01:00
|
|
|
using namespace QmlJSTools;
|
2010-07-27 09:29:22 +02:00
|
|
|
using TextEditor::RefactoringChanges;
|
|
|
|
|
|
2013-06-03 19:31:32 +02:00
|
|
|
namespace QmlJSEditor {
|
|
|
|
|
|
|
|
|
|
using namespace Internal;
|
|
|
|
|
|
2010-07-27 09:29:22 +02:00
|
|
|
namespace {
|
|
|
|
|
|
2011-01-07 14:49:00 +01:00
|
|
|
/*
|
|
|
|
|
Reformats a one-line object into a multi-line one, i.e.
|
|
|
|
|
Item { x: 10; y: 20; width: 10 }
|
|
|
|
|
into
|
|
|
|
|
Item {
|
|
|
|
|
x: 10;
|
|
|
|
|
y: 20;
|
|
|
|
|
width: 10
|
|
|
|
|
}
|
|
|
|
|
*/
|
2010-07-27 09:29:22 +02:00
|
|
|
class SplitInitializerOp: public QmlJSQuickFixFactory
|
|
|
|
|
{
|
2012-10-10 23:27:16 +02:00
|
|
|
void match(const QmlJSQuickFixInterface &interface, QuickFixOperations &result)
|
2010-07-27 09:29:22 +02:00
|
|
|
{
|
|
|
|
|
UiObjectInitializer *objectInitializer = 0;
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
const int pos = interface->currentFile()->cursor().position();
|
2010-07-27 09:29:22 +02:00
|
|
|
|
2011-07-04 09:46:10 +02:00
|
|
|
if (QmlJS::AST::Node *member = interface->semanticInfo().rangeAt(pos)) {
|
2010-07-27 09:29:22 +02:00
|
|
|
if (QmlJS::AST::UiObjectBinding *b = QmlJS::AST::cast<QmlJS::AST::UiObjectBinding *>(member)) {
|
|
|
|
|
if (b->initializer->lbraceToken.startLine == b->initializer->rbraceToken.startLine)
|
|
|
|
|
objectInitializer = b->initializer;
|
|
|
|
|
|
|
|
|
|
} else if (QmlJS::AST::UiObjectDefinition *b = QmlJS::AST::cast<QmlJS::AST::UiObjectDefinition *>(member)) {
|
|
|
|
|
if (b->initializer->lbraceToken.startLine == b->initializer->rbraceToken.startLine)
|
|
|
|
|
objectInitializer = b->initializer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (objectInitializer)
|
2014-09-23 19:02:30 +02:00
|
|
|
result.append(new Operation(interface, objectInitializer));
|
2010-07-27 09:29:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Operation: public QmlJSQuickFixOperation
|
|
|
|
|
{
|
|
|
|
|
UiObjectInitializer *_objectInitializer;
|
|
|
|
|
|
|
|
|
|
public:
|
2011-04-15 16:19:23 +02:00
|
|
|
Operation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface,
|
|
|
|
|
UiObjectInitializer *objectInitializer)
|
|
|
|
|
: QmlJSQuickFixOperation(interface, 0)
|
2010-07-27 09:29:22 +02:00
|
|
|
, _objectInitializer(objectInitializer)
|
|
|
|
|
{
|
|
|
|
|
setDescription(QApplication::translate("QmlJSEditor::QuickFix",
|
2012-05-08 09:09:01 +02:00
|
|
|
"Split Initializer"));
|
2010-07-27 09:29:22 +02:00
|
|
|
}
|
|
|
|
|
|
2012-10-10 23:27:16 +02:00
|
|
|
void performChanges(QmlJSRefactoringFilePtr currentFile,
|
2011-08-17 11:35:57 +02:00
|
|
|
const QmlJSRefactoringChanges &)
|
2010-07-27 09:29:22 +02:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(_objectInitializer != 0);
|
|
|
|
|
|
|
|
|
|
Utils::ChangeSet changes;
|
|
|
|
|
|
|
|
|
|
for (QmlJS::AST::UiObjectMemberList *it = _objectInitializer->members; it; it = it->next) {
|
|
|
|
|
if (QmlJS::AST::UiObjectMember *member = it->member) {
|
|
|
|
|
const QmlJS::AST::SourceLocation loc = member->firstSourceLocation();
|
|
|
|
|
|
|
|
|
|
// insert a newline at the beginning of this binding
|
2010-08-13 12:49:11 +02:00
|
|
|
changes.insert(currentFile->startOf(loc), QLatin1String("\n"));
|
2010-07-27 09:29:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// insert a newline before the closing brace
|
2010-08-13 12:49:11 +02:00
|
|
|
changes.insert(currentFile->startOf(_objectInitializer->rbraceToken),
|
2010-07-27 09:29:22 +02:00
|
|
|
QLatin1String("\n"));
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
currentFile->setChangeSet(changes);
|
|
|
|
|
currentFile->appendIndentRange(Range(currentFile->startOf(_objectInitializer->lbraceToken),
|
2010-08-13 12:49:11 +02:00
|
|
|
currentFile->startOf(_objectInitializer->rbraceToken)));
|
2011-08-17 11:35:57 +02:00
|
|
|
currentFile->apply();
|
2010-07-27 09:29:22 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2011-10-19 14:27:40 +02:00
|
|
|
/*
|
|
|
|
|
Adds a comment to suppress a static analysis message
|
|
|
|
|
*/
|
|
|
|
|
class AddAnalysisMessageSuppressionComment: public QmlJSQuickFixFactory
|
|
|
|
|
{
|
2011-12-13 16:17:43 +01:00
|
|
|
Q_DECLARE_TR_FUNCTIONS(QmlJSEditor::AddAnalysisMessageSuppressionComment)
|
2011-10-19 14:27:40 +02:00
|
|
|
public:
|
2012-10-10 23:27:16 +02:00
|
|
|
void match(const QmlJSQuickFixInterface &interface, QuickFixOperations &result)
|
2011-10-19 14:27:40 +02:00
|
|
|
{
|
|
|
|
|
const QList<StaticAnalysis::Message> &messages = interface->semanticInfo().staticAnalysisMessages;
|
|
|
|
|
|
|
|
|
|
foreach (const StaticAnalysis::Message &message, messages) {
|
|
|
|
|
if (interface->currentFile()->isCursorOn(message.location)) {
|
2014-09-23 19:02:30 +02:00
|
|
|
result.append(new Operation(interface, message));
|
2012-10-10 23:27:16 +02:00
|
|
|
return;
|
2011-10-19 14:27:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
class Operation: public QmlJSQuickFixOperation
|
|
|
|
|
{
|
|
|
|
|
StaticAnalysis::Message _message;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Operation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface,
|
|
|
|
|
const StaticAnalysis::Message &message)
|
|
|
|
|
: QmlJSQuickFixOperation(interface, 0)
|
|
|
|
|
, _message(message)
|
|
|
|
|
{
|
2012-05-08 09:09:01 +02:00
|
|
|
setDescription(AddAnalysisMessageSuppressionComment::tr("Add a Comment to Suppress This Message"));
|
2011-10-19 14:27:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void performChanges(QmlJSRefactoringFilePtr currentFile,
|
|
|
|
|
const QmlJSRefactoringChanges &)
|
|
|
|
|
{
|
|
|
|
|
Utils::ChangeSet changes;
|
|
|
|
|
const int insertLoc = _message.location.begin() - _message.location.startColumn + 1;
|
2012-11-26 21:17:34 +02:00
|
|
|
changes.insert(insertLoc, QString::fromLatin1("// %1\n").arg(_message.suppressionString()));
|
2011-10-19 14:27:40 +02:00
|
|
|
currentFile->setChangeSet(changes);
|
|
|
|
|
currentFile->appendIndentRange(Range(insertLoc, insertLoc + 1));
|
|
|
|
|
currentFile->apply();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2010-07-27 09:29:22 +02:00
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
void registerQuickFixes(ExtensionSystem::IPlugin *plugIn)
|
2010-07-27 09:29:22 +02:00
|
|
|
{
|
|
|
|
|
plugIn->addAutoReleasedObject(new SplitInitializerOp);
|
|
|
|
|
plugIn->addAutoReleasedObject(new ComponentFromObjectDef);
|
2011-10-19 13:48:10 +02:00
|
|
|
plugIn->addAutoReleasedObject(new WrapInLoader);
|
2011-10-19 14:27:40 +02:00
|
|
|
plugIn->addAutoReleasedObject(new AddAnalysisMessageSuppressionComment);
|
2010-07-27 09:29:22 +02:00
|
|
|
}
|
2013-06-03 19:31:32 +02:00
|
|
|
|
|
|
|
|
} // namespace QmlJSEditor
|