1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include <qmljs/parser/qmljsast_p.h>
34 #include <qmljs/parser/qmljsengine_p.h>
36 #include "changeimportsvisitor.h"
38 using namespace QmlJS;
39 using namespace QmlJS::AST;
41 using namespace QmlDesigner;
42 using namespace QmlDesigner::Internal;
44 ChangeImportsVisitor::ChangeImportsVisitor(TextModifier &textModifier,
45 const QString &source):
46 QMLRewriter(textModifier), m_source(source)
49 bool ChangeImportsVisitor::add(QmlJS::AST::UiProgram *ast, const Import &import)
51 setDidRewriting(false);
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();
60 insertionPoint = m_source.length();
62 while (insertionPoint > 0) {
64 const QChar c = m_source.at(insertionPoint);
65 if (!c.isSpace() && c != QLatin1Char(';'))
68 replace(insertionPoint+1, 0, QLatin1String("\n") + import.toString(false));
70 replace(0, 0, import.toString(false) + QLatin1String("\n\n"));
73 setDidRewriting(true);
78 bool ChangeImportsVisitor::remove(QmlJS::AST::UiProgram *ast, const Import &import)
80 setDidRewriting(false);
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);
94 return didRewriting();
97 bool ChangeImportsVisitor::equals(QmlJS::AST::UiImport *ast, const Import &import)
99 if (import.isLibraryImport()) {
100 return flatten(ast->importUri) == import.url();
101 } else if (import.isFileImport()) {
102 return ast->fileName->asString() == import.file();