qmljscomponentfromobjectdef.cpp 5.02 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
8
9
10
11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** 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.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
30
31
32
33
**
**************************************************************************/

#include "qmljscomponentfromobjectdef.h"
34
#include "qmljscomponentnamedialog.h"
Leandro Melo's avatar
Leandro Melo committed
35
#include "qmljsquickfixassist.h"
36
37
38
39
40

#include <coreplugin/ifile.h>

#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/qmljsdocument.h>
41
#include <qmljs/qmljsutils.h>
42
#include <qmljstools/qmljsrefactoringchanges.h>
43
44
45
46
47
48

#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>

using namespace QmlJS::AST;
49
using namespace QmlJSEditor;
50
using namespace QmlJSEditor::Internal;
51
using namespace QmlJSTools;
52

53
namespace {
54

55
class Operation: public QmlJSQuickFixOperation
56
{
57
    UiObjectDefinition *m_objDef;
58
    QString m_idName, m_componentName;
59
60

public:
Leandro Melo's avatar
Leandro Melo committed
61
62
63
    Operation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface,
              UiObjectDefinition *objDef)
        : QmlJSQuickFixOperation(interface, 0)
64
        , m_objDef(objDef)
65
    {
66
67
        Q_ASSERT(m_objDef != 0);

68
        m_idName = idOfObject(m_objDef);
69
        if (!m_idName.isEmpty()) {
70
            m_componentName = m_idName;
71
72
            m_componentName[0] = m_componentName.at(0).toUpper();
        }
73
74
75

        setDescription(QCoreApplication::translate("QmlJSEditor::ComponentFromObjectDef",
                                                   "Move Component into separate file"));
76
    }
77

78
79
    virtual void performChanges(QmlJSRefactoringFilePtr currentFile,
                                const QmlJSRefactoringChanges &refactoring)
80
    {
81
82
        QString componentName = m_componentName;
        QString path = QFileInfo(fileName()).path();
83
        ComponentNameDialog::go(&componentName, &path, assistInterface()->widget());
84
85
86
87
88
89

        if (componentName.isEmpty() || path.isEmpty())
            return;

        const QString newFileName = path + QDir::separator() + componentName
                + QLatin1String(".qml");
90

91
        QString imports;
92
        UiProgram *prog = currentFile->qmljsDocument()->qmlProgram();
93
        if (prog && prog->imports) {
94
95
96
            const int start = currentFile->startOf(prog->imports->firstSourceLocation());
            const int end = currentFile->startOf(prog->members->member->firstSourceLocation());
            imports = currentFile->textOf(start, end);
97
        }
98

99
100
101
        const int start = currentFile->startOf(m_objDef->firstSourceLocation());
        const int end = currentFile->startOf(m_objDef->lastSourceLocation());
        const QString txt = imports + currentFile->textOf(start, end)
102
                + QLatin1String("}\n");
103

104
        // stop if we can't create the new file
105
        if (!refactoring.createFile(newFileName, txt))
106
107
            return;

108
109
110
111
112
        QString replacement = componentName + QLatin1String(" {\n");
        if (!m_idName.isEmpty())
                replacement += QLatin1String("id: ") + m_idName
                        + QLatin1Char('\n');

113
        Utils::ChangeSet changes;
114
        changes.replace(start, end, replacement);
115
116
117
        currentFile->setChangeSet(changes);
        currentFile->appendIndentRange(Range(start, end + 1));
        currentFile->apply();
118
119
120
121
    }
};

} // end of anonymous namespace
122

Leandro Melo's avatar
Leandro Melo committed
123
124
125

QList<QmlJSQuickFixOperation::Ptr> ComponentFromObjectDef::match(
    const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface)
126
{
127
    const int pos = interface->currentFile()->cursor().position();
128

129
    QList<Node *> path = interface->semanticInfo().rangePath(pos);
130
131
132
    for (int i = path.size() - 1; i >= 0; --i) {
        Node *node = path.at(i);
        if (UiObjectDefinition *objDef = cast<UiObjectDefinition *>(node)) {
133
            if (!interface->currentFile()->isCursorOn(objDef->qualifiedTypeNameId))
134
                return noResult();
135
136
             // check that the node is not the root node
            if (i > 0 && !cast<UiProgram*>(path.at(i - 1))) {
Leandro Melo's avatar
Leandro Melo committed
137
                return singleResult(new Operation(interface, objDef));
138
139
140
141
            }
        }
    }

142
    return noResult();
143
}