OSDN Git Service

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