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 "removepropertyvisitor.h"
36 #include <qmljs/parser/qmljsast_p.h>
37 #include <qmljs/parser/qmljsengine_p.h>
39 using namespace QmlDesigner::Internal;
40 using namespace QmlJS;
41 using namespace QmlJS::AST;
43 RemovePropertyVisitor::RemovePropertyVisitor(QmlDesigner::TextModifier &modifier,
44 quint32 parentLocation,
45 const QString &propertyName):
46 QMLRewriter(modifier),
47 parentLocation(parentLocation),
48 propertyName(propertyName)
52 bool RemovePropertyVisitor::visit(QmlJS::AST::UiObjectBinding *ast)
54 if (ast->firstSourceLocation().offset == parentLocation) {
55 //this condition is wrong for the UiObjectBinding case, but we keep it
56 //since we are paranoid until the release is done.
57 // FIXME: change this to use the QmlJS::Rewriter class
58 removeFrom(ast->initializer);
61 if (ast->qualifiedTypeNameId && ast->qualifiedTypeNameId->identifierToken.offset == parentLocation) {
62 // FIXME: change this to use the QmlJS::Rewriter class
63 removeFrom(ast->initializer);
66 return !didRewriting();
69 bool RemovePropertyVisitor::visit(QmlJS::AST::UiObjectDefinition *ast)
71 if (ast->firstSourceLocation().offset == parentLocation) {
72 // FIXME: change this to use the QmlJS::Rewriter class
73 removeFrom(ast->initializer);
76 return !didRewriting();
79 // FIXME: duplicate code in the QmlJS::Rewriter class, remove this
80 void RemovePropertyVisitor::removeFrom(QmlJS::AST::UiObjectInitializer *ast)
83 int dotIdx = propertyName.indexOf(QLatin1Char('.'));
85 prefix = propertyName.left(dotIdx);
87 for (UiObjectMemberList *it = ast->members; it; it = it->next) {
88 UiObjectMember *member = it->member;
90 // run full name match (for ungrouped properties):
91 if (memberNameMatchesPropertyName(propertyName, member)) {
94 // check for grouped properties:
95 else if (!prefix.isEmpty()) {
96 if (UiObjectDefinition *def = cast<UiObjectDefinition *>(member)) {
97 if (flatten(def->qualifiedTypeNameId) == prefix) {
98 removeGroupedProperty(def);
105 // FIXME: duplicate code in the QmlJS::Rewriter class, remove this
106 void RemovePropertyVisitor::removeGroupedProperty(UiObjectDefinition *ast)
108 int dotIdx = propertyName.indexOf(QLatin1Char('.'));
112 const QString propName = propertyName.mid(dotIdx + 1);
114 UiObjectMember *wanted = 0;
115 unsigned memberCount = 0;
116 for (UiObjectMemberList *it = ast->initializer->members; it; it = it->next) {
118 UiObjectMember *member = it->member;
120 if (!wanted && memberNameMatchesPropertyName(propName, member)) {
127 if (memberCount == 1)
130 removeMember(wanted);
133 // FIXME: duplicate code in the QmlJS::Rewriter class, remove this
134 void RemovePropertyVisitor::removeMember(UiObjectMember *member)
136 int start = member->firstSourceLocation().offset;
137 int end = member->lastSourceLocation().end();
139 includeSurroundingWhitespace(start, end);
141 replace(start, end - start, QLatin1String(""));
142 setDidRewriting(true);
145 // FIXME: duplicate code in the QmlJS::Rewriter class, remove this
146 bool RemovePropertyVisitor::memberNameMatchesPropertyName(const QString &propertyName, UiObjectMember *ast)
148 if (UiPublicMember *publicMember = cast<UiPublicMember*>(ast))
149 return publicMember->name->asString() == propertyName;
150 else if (UiObjectBinding *objectBinding = cast<UiObjectBinding*>(ast))
151 return flatten(objectBinding->qualifiedId) == propertyName;
152 else if (UiScriptBinding *scriptBinding = cast<UiScriptBinding*>(ast))
153 return flatten(scriptBinding->qualifiedId) == propertyName;
154 else if (UiArrayBinding *arrayBinding = cast<UiArrayBinding*>(ast))
155 return flatten(arrayBinding->qualifiedId) == propertyName;