2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2010-07-27 09:29:22 +02:00
|
|
|
|
|
|
|
|
#include "qmljscomponentfromobjectdef.h"
|
|
|
|
|
#include "qmljseditor.h"
|
2022-08-25 12:46:47 +02:00
|
|
|
#include "qmljseditortr.h"
|
|
|
|
|
#include "qmljsquickfix.h"
|
2011-04-15 16:19:23 +02:00
|
|
|
#include "qmljsquickfixassist.h"
|
2022-08-25 12:46:47 +02:00
|
|
|
#include "qmljswrapinloader.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>
|
2017-12-18 12:23:28 +01:00
|
|
|
#include <texteditor/codeassist/assistinterface.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;
|
2017-12-18 12:23:28 +01:00
|
|
|
using namespace TextEditor;
|
2010-07-27 09:29:22 +02:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
*/
|
2017-12-18 12:23:28 +01:00
|
|
|
class SplitInitializerOperation: public QmlJSQuickFixOperation
|
2010-07-27 09:29:22 +02:00
|
|
|
{
|
2017-12-18 12:23:28 +01:00
|
|
|
UiObjectInitializer *_objectInitializer;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
SplitInitializerOperation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface,
|
|
|
|
|
UiObjectInitializer *objectInitializer)
|
|
|
|
|
: QmlJSQuickFixOperation(interface, 0)
|
|
|
|
|
, _objectInitializer(objectInitializer)
|
2010-07-27 09:29:22 +02:00
|
|
|
{
|
2022-08-25 12:46:47 +02:00
|
|
|
setDescription(Tr::tr("Split Initializer"));
|
2017-12-18 12:23:28 +01:00
|
|
|
}
|
2010-07-27 09:29:22 +02:00
|
|
|
|
2017-12-18 12:23:28 +01:00
|
|
|
void performChanges(QmlJSRefactoringFilePtr currentFile,
|
2018-11-24 02:45:30 +01:00
|
|
|
const QmlJSRefactoringChanges &) override
|
2017-12-18 12:23:28 +01:00
|
|
|
{
|
2018-11-24 02:45:30 +01:00
|
|
|
Q_ASSERT(_objectInitializer);
|
2010-07-27 09:29:22 +02:00
|
|
|
|
2017-12-18 12:23:28 +01:00
|
|
|
Utils::ChangeSet changes;
|
2010-07-27 09:29:22 +02:00
|
|
|
|
2017-12-18 12:23:28 +01:00
|
|
|
for (UiObjectMemberList *it = _objectInitializer->members; it; it = it->next) {
|
|
|
|
|
if (UiObjectMember *member = it->member) {
|
|
|
|
|
const SourceLocation loc = member->firstSourceLocation();
|
2010-07-27 09:29:22 +02:00
|
|
|
|
2017-12-18 12:23:28 +01:00
|
|
|
// insert a newline at the beginning of this binding
|
|
|
|
|
changes.insert(currentFile->startOf(loc), QLatin1String("\n"));
|
|
|
|
|
}
|
2010-07-27 09:29:22 +02:00
|
|
|
}
|
|
|
|
|
|
2017-12-18 12:23:28 +01:00
|
|
|
// insert a newline before the closing brace
|
|
|
|
|
changes.insert(currentFile->startOf(_objectInitializer->rbraceToken),
|
|
|
|
|
QLatin1String("\n"));
|
2010-07-27 09:29:22 +02:00
|
|
|
|
2017-12-18 12:23:28 +01:00
|
|
|
currentFile->setChangeSet(changes);
|
|
|
|
|
currentFile->appendIndentRange(Range(currentFile->startOf(_objectInitializer->lbraceToken),
|
|
|
|
|
currentFile->startOf(_objectInitializer->rbraceToken)));
|
|
|
|
|
currentFile->apply();
|
|
|
|
|
}
|
|
|
|
|
};
|
2010-07-27 09:29:22 +02:00
|
|
|
|
2017-12-18 12:23:28 +01:00
|
|
|
void matchSplitInitializerQuickFix(const QmlJSQuickFixInterface &interface, QuickFixOperations &result)
|
|
|
|
|
{
|
2018-11-24 02:45:30 +01:00
|
|
|
UiObjectInitializer *objectInitializer = nullptr;
|
2010-07-27 09:29:22 +02:00
|
|
|
|
2017-12-18 12:23:28 +01:00
|
|
|
const int pos = interface->currentFile()->cursor().position();
|
2010-07-27 09:29:22 +02:00
|
|
|
|
2017-12-18 12:23:28 +01:00
|
|
|
if (Node *member = interface->semanticInfo().rangeAt(pos)) {
|
2018-11-24 02:45:30 +01:00
|
|
|
if (auto b = AST::cast<const UiObjectBinding *>(member)) {
|
2017-12-18 12:23:28 +01:00
|
|
|
if (b->initializer->lbraceToken.startLine == b->initializer->rbraceToken.startLine)
|
|
|
|
|
objectInitializer = b->initializer;
|
2010-07-27 09:29:22 +02:00
|
|
|
|
2018-11-24 02:45:30 +01:00
|
|
|
} else if (auto b = AST::cast<const UiObjectDefinition *>(member)) {
|
2017-12-18 12:23:28 +01:00
|
|
|
if (b->initializer->lbraceToken.startLine == b->initializer->rbraceToken.startLine)
|
|
|
|
|
objectInitializer = b->initializer;
|
2010-07-27 09:29:22 +02:00
|
|
|
}
|
2017-12-18 12:23:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (objectInitializer)
|
|
|
|
|
result << new SplitInitializerOperation(interface, objectInitializer);
|
|
|
|
|
}
|
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
|
|
|
|
|
*/
|
2017-12-18 12:23:28 +01:00
|
|
|
class AnalysizeMessageSuppressionOperation: public QmlJSQuickFixOperation
|
2011-10-19 14:27:40 +02:00
|
|
|
{
|
2017-12-18 12:23:28 +01:00
|
|
|
StaticAnalysis::Message _message;
|
|
|
|
|
|
|
|
|
|
Q_DECLARE_TR_FUNCTIONS(AddAnalysisMessageSuppressionComment)
|
|
|
|
|
|
2011-10-19 14:27:40 +02:00
|
|
|
public:
|
2017-12-18 12:23:28 +01:00
|
|
|
AnalysizeMessageSuppressionOperation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface,
|
|
|
|
|
const StaticAnalysis::Message &message)
|
|
|
|
|
: QmlJSQuickFixOperation(interface, 0)
|
|
|
|
|
, _message(message)
|
2011-10-19 14:27:40 +02:00
|
|
|
{
|
2022-08-25 12:46:47 +02:00
|
|
|
setDescription(Tr::tr("Add a Comment to Suppress This Message"));
|
2011-10-19 14:27:40 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void performChanges(QmlJSRefactoringFilePtr currentFile,
|
|
|
|
|
const QmlJSRefactoringChanges &) override
|
2011-10-19 14:27:40 +02:00
|
|
|
{
|
2017-12-18 12:23:28 +01:00
|
|
|
Utils::ChangeSet changes;
|
|
|
|
|
const int insertLoc = _message.location.begin() - _message.location.startColumn + 1;
|
|
|
|
|
changes.insert(insertLoc, QString::fromLatin1("// %1\n").arg(_message.suppressionString()));
|
|
|
|
|
currentFile->setChangeSet(changes);
|
|
|
|
|
currentFile->appendIndentRange(Range(insertLoc, insertLoc + 1));
|
|
|
|
|
currentFile->apply();
|
|
|
|
|
}
|
|
|
|
|
};
|
2011-10-19 14:27:40 +02:00
|
|
|
|
2017-12-18 12:23:28 +01:00
|
|
|
void matchAddAnalysisMessageSuppressionCommentQuickFix(const QmlJSQuickFixInterface &interface, QuickFixOperations &result)
|
|
|
|
|
{
|
|
|
|
|
const QList<StaticAnalysis::Message> &messages = interface->semanticInfo().staticAnalysisMessages;
|
|
|
|
|
|
2022-05-17 16:11:03 +02:00
|
|
|
for (const StaticAnalysis::Message &message : messages) {
|
2017-12-18 12:23:28 +01:00
|
|
|
if (interface->currentFile()->isCursorOn(message.location)) {
|
|
|
|
|
result << new AnalysizeMessageSuppressionOperation(interface, message);
|
|
|
|
|
return;
|
2011-10-19 14:27:40 +02:00
|
|
|
}
|
2017-12-18 12:23:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
2011-10-19 14:27:40 +02:00
|
|
|
|
2010-07-27 09:29:22 +02:00
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
2017-12-18 12:23:28 +01:00
|
|
|
QuickFixOperations findQmlJSQuickFixes(const AssistInterface *interface)
|
2010-07-27 09:29:22 +02:00
|
|
|
{
|
2017-12-18 12:23:28 +01:00
|
|
|
QSharedPointer<const AssistInterface> assistInterface(interface);
|
|
|
|
|
auto qmlJSInterface = assistInterface.staticCast<const QmlJSQuickFixAssistInterface>();
|
|
|
|
|
|
|
|
|
|
QuickFixOperations quickFixes;
|
|
|
|
|
|
|
|
|
|
matchSplitInitializerQuickFix(qmlJSInterface, quickFixes);
|
|
|
|
|
matchComponentFromObjectDefQuickFix(qmlJSInterface, quickFixes);
|
|
|
|
|
matchWrapInLoaderQuickFix(qmlJSInterface, quickFixes);
|
|
|
|
|
matchAddAnalysisMessageSuppressionCommentQuickFix(qmlJSInterface, quickFixes);
|
|
|
|
|
|
|
|
|
|
return quickFixes;
|
2010-07-27 09:29:22 +02:00
|
|
|
}
|
2013-06-03 19:31:32 +02:00
|
|
|
|
|
|
|
|
} // namespace QmlJSEditor
|