qmljscomponentfromobjectdef.cpp 6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "qmljscomponentfromobjectdef.h"
31
#include "qmljscomponentnamedialog.h"
32
33
34
35
36
37
38
39
40
41
42
43
#include "qmljsrefactoringchanges.h"

#include <coreplugin/ifile.h>

#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/qmljsdocument.h>

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

using namespace QmlJS::AST;
44
using namespace QmlJSEditor;
45
46
using namespace QmlJSEditor::Internal;

47
namespace {
48

49
50
51
52
53
54
55
56
57
58
59
60
61
static QString toString(Statement *statement)
{
    ExpressionStatement *expStmt = cast<ExpressionStatement *>(statement);
    if (!expStmt)
        return QString::null;
    if (IdentifierExpression *idExp = cast<IdentifierExpression *>(expStmt->expression)) {
        return idExp->name->asString();
    } else if (StringLiteral *strExp = cast<StringLiteral *>(expStmt->expression)) {
        return strExp->value->asString();
    }
    return QString::null;
}

62
static QString getIdProperty(UiObjectDefinition *def)
63
{
64
65
    QString objectName;

66
67
68
69
70
71
72
73
    if (def && def->initializer) {
        for (UiObjectMemberList *iter = def->initializer->members; iter; iter = iter->next) {
            if (UiScriptBinding *script = cast<UiScriptBinding*>(iter->member)) {
                if (!script->qualifiedId)
                    continue;
                if (script->qualifiedId->next)
                    continue;
                if (script->qualifiedId->name) {
74
75
76
77
                    if (script->qualifiedId->name->asString() == QLatin1String("id"))
                        return toString(script->statement);
                    if (script->qualifiedId->name->asString() == QLatin1String("objectName"))
                        objectName = toString(script->statement);
78
79
80
81
82
                }
            }
        }
    }

83
    return objectName;
84
85
}

86
class Operation: public QmlJSQuickFixOperation
87
{
88
89
    UiObjectDefinition *m_objDef;
    QString m_componentName;
90
91
92
93

public:
    Operation(const QmlJSQuickFixState &state, UiObjectDefinition *objDef)
        : QmlJSQuickFixOperation(state, 0)
94
        , m_objDef(objDef)
95
    {
96
97
98
99
        Q_ASSERT(m_objDef != 0);

        m_componentName = getIdProperty(m_objDef);

100
101
        if (m_componentName.isEmpty()) {
            setDescription(QCoreApplication::translate("QmlJSEditor::ComponentFromObjectDef",
102
                                                       "Move Component into separate file"));
103
104
105
106
107
        } else {
            m_componentName[0] = m_componentName.at(0).toUpper();
            setDescription(QCoreApplication::translate("QmlJSEditor::ComponentFromObjectDef",
                                                       "Move Component into '%1.qml'").arg(m_componentName));
        }
108
    }
109

110
    virtual void performChanges(QmlJSRefactoringFile *currentFile, QmlJSRefactoringChanges *refactoring)
111
    {
112
113
114
115
116
117
118
119
120
121
122
        QString componentName = m_componentName;
        QString path = QFileInfo(fileName()).path();
        if (componentName.isEmpty()) {
            ComponentNameDialog::go(&componentName, &path, state().editor());
        }

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

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

124
        QString imports;
125
        UiProgram *prog = currentFile->qmljsDocument()->qmlProgram();
126
        if (prog && prog->imports) {
127
128
129
            const int start = currentFile->startOf(prog->imports->firstSourceLocation());
            const int end = currentFile->startOf(prog->members->member->firstSourceLocation());
            imports = currentFile->textOf(start, end);
130
        }
131

132
133
134
        const int start = currentFile->startOf(m_objDef->firstSourceLocation());
        const int end = currentFile->startOf(m_objDef->lastSourceLocation());
        const QString txt = imports + currentFile->textOf(start, end)
135
                + QLatin1String("}\n");
136

137
138
139
140
        // stop if we can't create the new file
        if (!refactoring->createFile(newFileName, txt))
            return;

141
        Utils::ChangeSet changes;
142
        changes.replace(start, end, componentName + QLatin1String(" {\n"));
143
        currentFile->change(changes);
144
        currentFile->indent(Range(start, end + 1));
145
146
147
148
    }
};

} // end of anonymous namespace
149

150
QList<QmlJSQuickFixOperation::Ptr> ComponentFromObjectDef::match(const QmlJSQuickFixState &state)
151
{
152
    QList<QmlJSQuickFixOperation::Ptr> result;
153
    const int pos = state.currentFile().cursor().position();
154

155
    QList<Node *> path = state.semanticInfo().astPath(pos);
156
157
158
    for (int i = path.size() - 1; i >= 0; --i) {
        Node *node = path.at(i);
        if (UiObjectDefinition *objDef = cast<UiObjectDefinition *>(node)) {
159
160
             // check that the node is not the root node
            if (i > 0 && !cast<UiProgram*>(path.at(i - 1))) {
161
162
                result.append(QmlJSQuickFixOperation::Ptr(new Operation(state, objDef)));
                return result;
163
164
165
166
            }
        }
    }

167
    return result;
168
}