searchsymbols.cpp 8.68 KB
Newer Older
1
/**************************************************************************
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
6
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
8 9
**
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
28
**
29
**************************************************************************/
30 31 32 33 34

#include "searchsymbols.h"

#include <Literals.h>
#include <Scope.h>
35
#include <Names.h>
36
#include <cplusplus/LookupContext.h>
Roberto Raggi's avatar
Roberto Raggi committed
37
#include <QDebug>
38 39

using namespace CPlusPlus;
40
using namespace CppTools;
41

42 43 44 45 46 47
SearchSymbols::SymbolTypes SearchSymbols::AllTypes =
        SearchSymbols::Classes
        | SearchSymbols::Functions
        | SearchSymbols::Enums
        | SearchSymbols::Declarations;

48
SearchSymbols::SearchSymbols():
49 50
    symbolsToSearchFor(Classes | Functions | Enums),
    separateScope(false)
51 52 53
{
}

54
void SearchSymbols::setSymbolsToSearchFor(SymbolTypes types)
55
{
56
    symbolsToSearchFor = types;
57 58
}

59 60 61 62 63
void SearchSymbols::setSeparateScope(bool separateScope)
{
    this->separateScope = separateScope;
}

64 65 66 67 68 69 70 71
QList<ModelItemInfo> SearchSymbols::operator()(Document::Ptr doc, const QString &scope)
{
    QString previousScope = switchScope(scope);
    items.clear();
    for (unsigned i = 0; i < doc->globalSymbolCount(); ++i) {
        accept(doc->globalSymbolAt(i));
    }
    (void) switchScope(previousScope);
Roberto Raggi's avatar
Roberto Raggi committed
72
    QList<ModelItemInfo> result = items;
73
    strings.clear();
Roberto Raggi's avatar
Roberto Raggi committed
74 75 76
    items.clear();
    m_paths.clear();
    return result;
77 78 79 80 81 82 83 84 85 86 87
}

QString SearchSymbols::switchScope(const QString &scope)
{
    QString previousScope = _scope;
    _scope = scope;
    return previousScope;
}

bool SearchSymbols::visit(Enum *symbol)
{
88
    if (!(symbolsToSearchFor & Enums))
89 90 91
        return false;

    QString name = symbolName(symbol);
92 93 94 95 96
    QString scopedName = scopedSymbolName(name);
    QString previousScope = switchScope(scopedName);
    appendItem(separateScope ? name : scopedName,
               separateScope ? previousScope : QString(),
               ModelItemInfo::Enum, symbol);
97 98
    for (unsigned i = 0; i < symbol->memberCount(); ++i) {
        accept(symbol->memberAt(i));
99 100 101 102 103 104 105
    }
    (void) switchScope(previousScope);
    return false;
}

bool SearchSymbols::visit(Function *symbol)
{
106
    if (!(symbolsToSearchFor & Functions))
107 108
        return false;

109
    QString extraScope;
Roberto Raggi's avatar
Roberto Raggi committed
110
    if (const Name *name = symbol->name()) {
111 112 113
        if (const QualifiedNameId *q = name->asQualifiedNameId()) {
            if (q->base())
                extraScope = overview.prettyName(q->base());
114 115 116 117 118 119
        }
    }
    QString fullScope = _scope;
    if (!_scope.isEmpty() && !extraScope.isEmpty())
        fullScope += QLatin1String("::");
    fullScope += extraScope;
120
    QString name = symbolName(symbol);
121 122
    QString scopedName = scopedSymbolName(name);
    QString type = overview.prettyType(symbol->type(),
123
                                       separateScope ? symbol->unqualifiedName() : 0);
124
    appendItem(separateScope ? type : scopedName,
125
               separateScope ? fullScope : type,
126
               ModelItemInfo::Method, symbol);
127 128 129 130 131
    return false;
}

bool SearchSymbols::visit(Namespace *symbol)
{
Roberto Raggi's avatar
Roberto Raggi committed
132
    QString name = scopedSymbolName(symbol);
133
    QString previousScope = switchScope(name);
134 135
    for (unsigned i = 0; i < symbol->memberCount(); ++i) {
        accept(symbol->memberAt(i));
136 137 138 139 140 141 142
    }
    (void) switchScope(previousScope);
    return false;
}

bool SearchSymbols::visit(Declaration *symbol)
{
143 144 145 146 147 148
    if (!(symbolsToSearchFor & Declarations))
        return false;

    QString name = symbolName(symbol);
    QString scopedName = scopedSymbolName(name);
    QString type = overview.prettyType(symbol->type(),
149
                                       separateScope ? symbol->unqualifiedName() : 0);
150 151 152
    appendItem(separateScope ? type : scopedName,
               separateScope ? _scope : type,
               ModelItemInfo::Declaration, symbol);
153 154 155 156 157 158
    return false;
}

bool SearchSymbols::visit(Class *symbol)
{
    QString name = symbolName(symbol);
159 160
    QString scopedName = scopedSymbolName(name);
    QString previousScope = switchScope(scopedName);
161 162 163 164 165
    if (symbolsToSearchFor & Classes) {
        appendItem(separateScope ? name : scopedName,
                   separateScope ? previousScope : QString(),
                   ModelItemInfo::Class, symbol);
    }
166 167
    for (unsigned i = 0; i < symbol->memberCount(); ++i) {
        accept(symbol->memberAt(i));
168 169 170 171 172
    }
    (void) switchScope(previousScope);
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
bool SearchSymbols::visit(CPlusPlus::UsingNamespaceDirective *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::UsingDeclaration *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::NamespaceAlias *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::Argument *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::TypenameArgument *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::BaseClass *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::Template *)
{
    return true;
}

bool SearchSymbols::visit(CPlusPlus::Block *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ForwardClassDeclaration *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCBaseClass *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCBaseProtocol *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCClass *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCForwardClassDeclaration *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCProtocol *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCForwardProtocolDeclaration *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCMethod *)
{
    return false;
}

bool SearchSymbols::visit(CPlusPlus::ObjCPropertyDeclaration *)
{
    return false;
}

258
QString SearchSymbols::scopedSymbolName(const QString &symbolName) const
259 260
{
    QString name = _scope;
261
    if (!name.isEmpty())
262
        name += QLatin1String("::");
263 264 265 266 267 268 269 270 271 272 273
    name += symbolName;
    return name;
}

QString SearchSymbols::scopedSymbolName(const Symbol *symbol) const
{
    return scopedSymbolName(symbolName(symbol));
}

QString SearchSymbols::symbolName(const Symbol *symbol) const
{
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    QString symbolName = overview.prettyName(symbol->name());
    if (symbolName.isEmpty()) {
        QString type;
        if (symbol->isNamespace()) {
            type = QLatin1String("namespace");
        } else if (symbol->isEnum()) {
            type = QLatin1String("enum");
        } else if (const Class *c = symbol->asClass())  {
            if (c->isUnion()) {
                type = QLatin1String("union");
            } else if (c->isStruct()) {
                type = QLatin1String("struct");
            } else {
                type = QLatin1String("class");
            }
        } else {
            type = QLatin1String("symbol");
        }
        symbolName = QLatin1String("<anonymous ");
        symbolName += type;
294
        symbolName += QLatin1Char('>');
295
    }
296 297 298 299 300 301
    return symbolName;
}

void SearchSymbols::appendItem(const QString &name,
                               const QString &info,
                               ModelItemInfo::ItemType type,
302
                               Symbol *symbol)
303
{
304 305 306
    if (!symbol->name())
        return;

307 308
    QStringList fullyQualifiedName;
    foreach (const Name *name, LookupContext::fullyQualifiedName(symbol))
Roberto Raggi's avatar
Roberto Raggi committed
309
        fullyQualifiedName.append(findOrInsert(overview.prettyName(name)));
310 311 312 313 314 315 316

    QString path = m_paths.value(symbol->fileId(), QString());
    if (path.isEmpty()) {
        path = QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
        m_paths.insert(symbol->fileId(), path);
    }

317
    const QIcon icon = icons.iconForSymbol(symbol);
Roberto Raggi's avatar
Roberto Raggi committed
318
    items.append(ModelItemInfo(findOrInsert(name), findOrInsert(info), type,
319
                               fullyQualifiedName,
320
                               path,
321
                               symbol->line(),
322
                               symbol->column() - 1, // 1-based vs 0-based column
323
                               icon));
324
}