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 "searchsymbols.h"
39 #include <cplusplus/LookupContext.h>
42 using namespace CPlusPlus;
43 using namespace CppTools::Internal;
45 SearchSymbols::SymbolTypes SearchSymbols::AllTypes =
46 SearchSymbols::Classes
47 | SearchSymbols::Functions
48 | SearchSymbols::Enums
49 | SearchSymbols::Declarations;
51 SearchSymbols::SearchSymbols():
52 symbolsToSearchFor(Classes | Functions | Enums),
57 void SearchSymbols::setSymbolsToSearchFor(SymbolTypes types)
59 symbolsToSearchFor = types;
62 void SearchSymbols::setSeparateScope(bool separateScope)
64 this->separateScope = separateScope;
67 QList<ModelItemInfo> SearchSymbols::operator()(Document::Ptr doc, const QString &scope)
69 QString previousScope = switchScope(scope);
71 for (unsigned i = 0; i < doc->globalSymbolCount(); ++i) {
72 accept(doc->globalSymbolAt(i));
74 (void) switchScope(previousScope);
75 QList<ModelItemInfo> result = items;
82 QString SearchSymbols::switchScope(const QString &scope)
84 QString previousScope = _scope;
89 bool SearchSymbols::visit(Enum *symbol)
91 if (!(symbolsToSearchFor & Enums))
94 QString name = symbolName(symbol);
95 QString scopedName = scopedSymbolName(name);
96 QString previousScope = switchScope(scopedName);
97 appendItem(separateScope ? name : scopedName,
98 separateScope ? previousScope : QString(),
99 ModelItemInfo::Enum, symbol);
100 for (unsigned i = 0; i < symbol->memberCount(); ++i) {
101 accept(symbol->memberAt(i));
103 (void) switchScope(previousScope);
107 bool SearchSymbols::visit(Function *symbol)
109 if (!(symbolsToSearchFor & Functions))
113 if (const Name *name = symbol->name()) {
114 if (const QualifiedNameId *q = name->asQualifiedNameId()) {
116 extraScope = overview.prettyName(q->base());
119 QString fullScope = _scope;
120 if (!_scope.isEmpty() && !extraScope.isEmpty())
121 fullScope += QLatin1String("::");
122 fullScope += extraScope;
123 QString name = symbolName(symbol);
124 QString scopedName = scopedSymbolName(name);
125 QString type = overview.prettyType(symbol->type(),
126 separateScope ? symbol->unqualifiedName() : 0);
127 appendItem(separateScope ? type : scopedName,
128 separateScope ? fullScope : type,
129 ModelItemInfo::Method, symbol);
133 bool SearchSymbols::visit(Namespace *symbol)
135 QString name = scopedSymbolName(symbol);
136 QString previousScope = switchScope(name);
137 for (unsigned i = 0; i < symbol->memberCount(); ++i) {
138 accept(symbol->memberAt(i));
140 (void) switchScope(previousScope);
144 bool SearchSymbols::visit(Declaration *symbol)
146 if (!(symbolsToSearchFor & Declarations))
149 QString name = symbolName(symbol);
150 QString scopedName = scopedSymbolName(name);
151 QString type = overview.prettyType(symbol->type(),
152 separateScope ? symbol->unqualifiedName() : 0);
153 appendItem(separateScope ? type : scopedName,
154 separateScope ? _scope : type,
155 ModelItemInfo::Declaration, symbol);
159 bool SearchSymbols::visit(Class *symbol)
161 QString name = symbolName(symbol);
162 QString scopedName = scopedSymbolName(name);
163 QString previousScope = switchScope(scopedName);
164 if (symbolsToSearchFor & Classes) {
165 appendItem(separateScope ? name : scopedName,
166 separateScope ? previousScope : QString(),
167 ModelItemInfo::Class, symbol);
169 for (unsigned i = 0; i < symbol->memberCount(); ++i) {
170 accept(symbol->memberAt(i));
172 (void) switchScope(previousScope);
176 bool SearchSymbols::visit(CPlusPlus::UsingNamespaceDirective *)
181 bool SearchSymbols::visit(CPlusPlus::UsingDeclaration *)
186 bool SearchSymbols::visit(CPlusPlus::NamespaceAlias *)
191 bool SearchSymbols::visit(CPlusPlus::Argument *)
196 bool SearchSymbols::visit(CPlusPlus::TypenameArgument *)
201 bool SearchSymbols::visit(CPlusPlus::BaseClass *)
206 bool SearchSymbols::visit(CPlusPlus::Template *)
211 bool SearchSymbols::visit(CPlusPlus::Block *)
216 bool SearchSymbols::visit(CPlusPlus::ForwardClassDeclaration *)
221 bool SearchSymbols::visit(CPlusPlus::ObjCBaseClass *)
226 bool SearchSymbols::visit(CPlusPlus::ObjCBaseProtocol *)
231 bool SearchSymbols::visit(CPlusPlus::ObjCClass *)
236 bool SearchSymbols::visit(CPlusPlus::ObjCForwardClassDeclaration *)
241 bool SearchSymbols::visit(CPlusPlus::ObjCProtocol *)
246 bool SearchSymbols::visit(CPlusPlus::ObjCForwardProtocolDeclaration *)
251 bool SearchSymbols::visit(CPlusPlus::ObjCMethod *)
256 bool SearchSymbols::visit(CPlusPlus::ObjCPropertyDeclaration *)
261 QString SearchSymbols::scopedSymbolName(const QString &symbolName) const
263 QString name = _scope;
265 name += QLatin1String("::");
270 QString SearchSymbols::scopedSymbolName(const Symbol *symbol) const
272 return scopedSymbolName(symbolName(symbol));
275 QString SearchSymbols::symbolName(const Symbol *symbol) const
277 QString symbolName = overview.prettyName(symbol->name());
278 if (symbolName.isEmpty()) {
280 if (symbol->isNamespace()) {
281 type = QLatin1String("namespace");
282 } else if (symbol->isEnum()) {
283 type = QLatin1String("enum");
284 } else if (const Class *c = symbol->asClass()) {
286 type = QLatin1String("union");
287 } else if (c->isStruct()) {
288 type = QLatin1String("struct");
290 type = QLatin1String("class");
293 type = QLatin1String("symbol");
295 symbolName = QLatin1String("<anonymous ");
297 symbolName += QLatin1Char('>');
302 void SearchSymbols::appendItem(const QString &name,
304 ModelItemInfo::ItemType type,
310 QStringList fullyQualifiedName;
311 foreach (const Name *name, LookupContext::fullyQualifiedName(symbol))
312 fullyQualifiedName.append(findOrInsert(overview.prettyName(name)));
314 QString path = m_paths.value(symbol->fileId(), QString());
315 if (path.isEmpty()) {
316 path = QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
317 m_paths.insert(symbol->fileId(), path);
320 const QIcon icon = icons.iconForSymbol(symbol);
321 items.append(ModelItemInfo(findOrInsert(name), findOrInsert(info), type,
325 symbol->column() - 1, // 1-based vs 0-based column