1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include <qmljs/parser/qmljsast_p.h>
35 #include <qmljs/parser/qmljsengine_p.h>
37 #include "changeimportsvisitor.h"
39 using namespace QmlJS;
40 using namespace QmlJS::AST;
42 using namespace QmlDesigner;
43 using namespace QmlDesigner::Internal;
45 ChangeImportsVisitor::ChangeImportsVisitor(TextModifier &textModifier,
46 const QString &source):
47 QMLRewriter(textModifier), m_source(source)
50 bool ChangeImportsVisitor::add(QmlJS::AST::UiProgram *ast, const Import &import)
52 setDidRewriting(false);
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();
61 insertionPoint = m_source.length();
63 while (insertionPoint > 0) {
65 const QChar c = m_source.at(insertionPoint);
66 if (!c.isSpace() && c != QLatin1Char(';'))
69 replace(insertionPoint+1, 0, QLatin1String("\n") + import.toString(false));
71 replace(0, 0, import.toString(false) + QLatin1String("\n\n"));
74 setDidRewriting(true);
79 bool ChangeImportsVisitor::remove(QmlJS::AST::UiProgram *ast, const Import &import)
81 setDidRewriting(false);
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);
95 return didRewriting();
98 bool ChangeImportsVisitor::equals(QmlJS::AST::UiImport *ast, const Import &import)
100 if (import.isLibraryImport()) {
101 return flatten(ast->importUri) == import.url();
102 } else if (import.isFileImport()) {
103 return ast->fileName->asString() == import.file();