OSDN Git Service

99ff8f578396f345738f24703de8b13befea9322
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / filemanager / removepropertyvisitor.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 "removepropertyvisitor.h"
35
36 #include <qmljs/parser/qmljsast_p.h>
37 #include <qmljs/parser/qmljsengine_p.h>
38
39 using namespace QmlDesigner::Internal;
40 using namespace QmlJS;
41 using namespace QmlJS::AST;
42
43 RemovePropertyVisitor::RemovePropertyVisitor(QmlDesigner::TextModifier &modifier,
44                                              quint32 parentLocation,
45                                              const QString &propertyName):
46     QMLRewriter(modifier),
47     parentLocation(parentLocation),
48     propertyName(propertyName)
49 {
50 }
51
52 bool RemovePropertyVisitor::visit(QmlJS::AST::UiObjectBinding *ast)
53 {
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);
59     }
60
61     if (ast->qualifiedTypeNameId && ast->qualifiedTypeNameId->identifierToken.offset == parentLocation) {
62         // FIXME: change this to use the QmlJS::Rewriter class
63         removeFrom(ast->initializer);
64     }
65
66     return !didRewriting();
67 }
68
69 bool RemovePropertyVisitor::visit(QmlJS::AST::UiObjectDefinition *ast)
70 {
71     if (ast->firstSourceLocation().offset == parentLocation) {
72         // FIXME: change this to use the QmlJS::Rewriter class
73         removeFrom(ast->initializer);
74     }
75
76     return !didRewriting();
77 }
78
79 // FIXME: duplicate code in the QmlJS::Rewriter class, remove this
80 void RemovePropertyVisitor::removeFrom(QmlJS::AST::UiObjectInitializer *ast)
81 {
82     QString prefix;
83     int dotIdx = propertyName.indexOf(QLatin1Char('.'));
84     if (dotIdx != -1)
85         prefix = propertyName.left(dotIdx);
86
87     for (UiObjectMemberList *it = ast->members; it; it = it->next) {
88         UiObjectMember *member = it->member;
89
90         // run full name match (for ungrouped properties):
91         if (memberNameMatchesPropertyName(propertyName, member)) {
92             removeMember(member);
93         }
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);
99                 }
100             }
101         }
102     }
103 }
104
105 // FIXME: duplicate code in the QmlJS::Rewriter class, remove this
106 void RemovePropertyVisitor::removeGroupedProperty(UiObjectDefinition *ast)
107 {
108     int dotIdx = propertyName.indexOf(QLatin1Char('.'));
109     if (dotIdx == -1)
110         return;
111
112     const QString propName = propertyName.mid(dotIdx + 1);
113
114     UiObjectMember *wanted = 0;
115     unsigned memberCount = 0;
116     for (UiObjectMemberList *it = ast->initializer->members; it; it = it->next) {
117         ++memberCount;
118         UiObjectMember *member = it->member;
119
120         if (!wanted && memberNameMatchesPropertyName(propName, member)) {
121             wanted = member;
122         }
123     }
124
125     if (!wanted)
126         return;
127     if (memberCount == 1)
128         removeMember(ast);
129     else
130         removeMember(wanted);
131 }
132
133 // FIXME: duplicate code in the QmlJS::Rewriter class, remove this
134 void RemovePropertyVisitor::removeMember(UiObjectMember *member)
135 {
136     int start = member->firstSourceLocation().offset;
137     int end = member->lastSourceLocation().end();
138
139     includeSurroundingWhitespace(start, end);
140
141     replace(start, end - start, QLatin1String(""));
142     setDidRewriting(true);
143 }
144
145 // FIXME: duplicate code in the QmlJS::Rewriter class, remove this
146 bool RemovePropertyVisitor::memberNameMatchesPropertyName(const QString &propertyName, UiObjectMember *ast)
147 {
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;
156     else
157         return false;
158 }