OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / filemanager / changeimportsvisitor.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include <qmljs/parser/qmljsast_p.h>
34 #include <qmljs/parser/qmljsengine_p.h>
35
36 #include "changeimportsvisitor.h"
37
38 using namespace QmlJS;
39 using namespace QmlJS::AST;
40
41 using namespace QmlDesigner;
42 using namespace QmlDesigner::Internal;
43
44 ChangeImportsVisitor::ChangeImportsVisitor(TextModifier &textModifier,
45                                            const QString &source):
46         QMLRewriter(textModifier), m_source(source)
47 {}
48
49 bool ChangeImportsVisitor::add(QmlJS::AST::UiProgram *ast, const Import &import)
50 {
51     setDidRewriting(false);
52     if (!ast)
53         return false;
54
55     if (ast->imports && ast->imports->import) {
56         int insertionPoint = 0;
57         if (ast->members && ast->members->member) {
58             insertionPoint = ast->members->member->firstSourceLocation().begin();
59         } else {
60             insertionPoint = m_source.length();
61         }
62         while (insertionPoint > 0) {
63             --insertionPoint;
64             const QChar c = m_source.at(insertionPoint);
65             if (!c.isSpace() && c != QLatin1Char(';'))
66                 break;
67         }
68         replace(insertionPoint+1, 0, QLatin1String("\n") + import.toString(false));
69     } else {
70         replace(0, 0, import.toString(false) + QLatin1String("\n\n"));
71     }
72
73     setDidRewriting(true);
74
75     return true;
76 }
77
78 bool ChangeImportsVisitor::remove(QmlJS::AST::UiProgram *ast, const Import &import)
79 {
80     setDidRewriting(false);
81     if (!ast)
82         return false;
83
84     for (UiImportList *iter = ast->imports; iter; iter = iter->next) {
85         if (equals(iter->import, import)) {
86             int start = iter->firstSourceLocation().begin();
87             int end = iter->lastSourceLocation().end();
88             includeSurroundingWhitespace(start, end);
89             replace(start, end - start, QString());
90             setDidRewriting(true);
91         }
92     }
93
94     return didRewriting();
95 }
96
97 bool ChangeImportsVisitor::equals(QmlJS::AST::UiImport *ast, const Import &import)
98 {
99     if (import.isLibraryImport()) {
100         return flatten(ast->importUri) == import.url();
101     } else if (import.isFileImport()) {
102         return ast->fileName->asString() == import.file();
103     } else {
104         return false;
105     }
106 }