2010-07-27 09:29:22 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2010-07-27 09:29:22 +02:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2010-07-27 09:29:22 +02:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2010-07-27 09:29:22 +02:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
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
|
|
|
|
|
|
|
|
#include <QtGui/QApplication>
|
|
|
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
|
using namespace QmlJS::AST;
|
|
|
|
|
using namespace QmlJSEditor;
|
|
|
|
|
using namespace QmlJSEditor::Internal;
|
2010-11-11 10:05:05 +01:00
|
|
|
using namespace QmlJSTools;
|
2010-07-27 09:29:22 +02:00
|
|
|
using namespace TextEditor;
|
|
|
|
|
using TextEditor::RefactoringChanges;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
public:
|
2011-04-15 16:19:23 +02:00
|
|
|
virtual QList<QmlJSQuickFixOperation::Ptr> match(
|
|
|
|
|
const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface)
|
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)
|
2011-04-15 16:19:23 +02:00
|
|
|
return singleResult(new Operation(interface, objectInitializer));
|
2010-09-30 16:12:24 +02:00
|
|
|
else
|
|
|
|
|
return noResult();
|
2010-07-27 09:29:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
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",
|
|
|
|
|
"Split initializer"));
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
virtual void performChanges(QmlJSRefactoringFilePtr currentFile,
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual QList<QmlJSQuickFixOperation::Ptr> match(
|
|
|
|
|
const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface)
|
|
|
|
|
{
|
|
|
|
|
const QList<StaticAnalysis::Message> &messages = interface->semanticInfo().staticAnalysisMessages;
|
|
|
|
|
|
|
|
|
|
foreach (const StaticAnalysis::Message &message, messages) {
|
|
|
|
|
if (interface->currentFile()->isCursorOn(message.location)) {
|
|
|
|
|
return singleResult(new Operation(interface, message));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return noResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
class Operation: public QmlJSQuickFixOperation
|
|
|
|
|
{
|
|
|
|
|
StaticAnalysis::Message _message;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Operation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface,
|
|
|
|
|
const StaticAnalysis::Message &message)
|
|
|
|
|
: QmlJSQuickFixOperation(interface, 0)
|
|
|
|
|
, _message(message)
|
|
|
|
|
{
|
|
|
|
|
setDescription(AddAnalysisMessageSuppressionComment::tr("Add a comment to suppress this message"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void performChanges(QmlJSRefactoringFilePtr currentFile,
|
|
|
|
|
const QmlJSRefactoringChanges &)
|
|
|
|
|
{
|
|
|
|
|
Utils::ChangeSet changes;
|
|
|
|
|
const int insertLoc = _message.location.begin() - _message.location.startColumn + 1;
|
|
|
|
|
changes.insert(insertLoc, QString("// %1\n").arg(_message.suppressionString()));
|
|
|
|
|
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
|
|
|
}
|