searchsymbols.cpp 6.69 KB
Newer Older
1 2 3 4
/***************************************************************************
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception
30
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
31 32 33 34 35 36 37
**
***************************************************************************/

#include "searchsymbols.h"

#include <Literals.h>
#include <Scope.h>
38
#include <Names.h>
39 40 41 42 43

using namespace CPlusPlus;
using namespace CppTools::Internal;

SearchSymbols::SearchSymbols():
44 45
    symbolsToSearchFor(Classes | Functions | Enums),
    separateScope(false)
46 47 48
{
}

49
void SearchSymbols::setSymbolsToSearchFor(SymbolTypes types)
50
{
51
    symbolsToSearchFor = types;
52 53
}

54 55 56 57 58
void SearchSymbols::setSeparateScope(bool separateScope)
{
    this->separateScope = separateScope;
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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);
    return items;
}

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

bool SearchSymbols::visit(Enum *symbol)
{
79
    if (!(symbolsToSearchFor & Enums))
80 81 82
        return false;

    QString name = symbolName(symbol);
83 84 85 86 87
    QString scopedName = scopedSymbolName(name);
    QString previousScope = switchScope(scopedName);
    appendItem(separateScope ? name : scopedName,
               separateScope ? previousScope : QString(),
               ModelItemInfo::Enum, symbol);
88 89 90 91 92 93 94 95 96 97
    Scope *members = symbol->members();
    for (unsigned i = 0; i < members->symbolCount(); ++i) {
        accept(members->symbolAt(i));
    }
    (void) switchScope(previousScope);
    return false;
}

bool SearchSymbols::visit(Function *symbol)
{
98
    if (!(symbolsToSearchFor & Functions))
99 100
        return false;

101 102 103 104 105 106 107 108 109 110 111 112
    QString extraScope;
    if (Name *name = symbol->name()) {
        if (QualifiedNameId *nameId = name->asQualifiedNameId()) {
            if (nameId->nameCount() > 1) {
                extraScope = overview.prettyName(nameId->nameAt(nameId->nameCount() - 2));
            }
        }
    }
    QString fullScope = _scope;
    if (!_scope.isEmpty() && !extraScope.isEmpty())
        fullScope += QLatin1String("::");
    fullScope += extraScope;
113
    QString name = symbolName(symbol);
114 115
    QString scopedName = scopedSymbolName(name);
    QString type = overview.prettyType(symbol->type(),
116
                                       separateScope ? symbol->identity() : 0);
117
    appendItem(separateScope ? type : scopedName,
118
               separateScope ? fullScope : type,
119
               ModelItemInfo::Method, symbol);
120 121 122 123 124
    return false;
}

bool SearchSymbols::visit(Namespace *symbol)
{
125
    QString name = findOrInsert(scopedSymbolName(symbol));
126 127 128 129 130 131 132 133 134 135 136 137 138
    QString previousScope = switchScope(name);
    Scope *members = symbol->members();
    for (unsigned i = 0; i < members->symbolCount(); ++i) {
        accept(members->symbolAt(i));
    }
    (void) switchScope(previousScope);
    return false;
}

#if 0
bool SearchSymbols::visit(Declaration *symbol)
{
    if (symbol->type()->isFunction()) {
139
        QString name = scopedSymbolName(symbol);
140
        QString type = overview.prettyType(symbol->type());
141
        appendItems(name, type, ModelItemInfo::Method, symbol->fileName());
142 143 144 145 146 147 148
    }
    return false;
}
#endif

bool SearchSymbols::visit(Class *symbol)
{
149 150 151
    if (!(symbolsToSearchFor & Classes))
        return false;

152
    QString name = symbolName(symbol);
153 154 155 156 157
    QString scopedName = scopedSymbolName(name);
    QString previousScope = switchScope(scopedName);
    appendItem(separateScope ? name : scopedName,
               separateScope ? previousScope : QString(),
               ModelItemInfo::Class, symbol);
158 159 160 161 162 163 164 165
    Scope *members = symbol->members();
    for (unsigned i = 0; i < members->symbolCount(); ++i) {
        accept(members->symbolAt(i));
    }
    (void) switchScope(previousScope);
    return false;
}

166
QString SearchSymbols::scopedSymbolName(const QString &symbolName) const
167 168
{
    QString name = _scope;
169
    if (!name.isEmpty())
170
        name += QLatin1String("::");
171 172 173 174 175 176 177 178 179 180 181
    name += symbolName;
    return name;
}

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

QString SearchSymbols::symbolName(const Symbol *symbol) const
{
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    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;
        symbolName += QLatin1String(">");
    }
204 205 206 207 208 209
    return symbolName;
}

void SearchSymbols::appendItem(const QString &name,
                               const QString &info,
                               ModelItemInfo::ItemType type,
210
                               const Symbol *symbol)
211
{
212 213 214
    if (!symbol->name())
        return;

215 216 217 218 219
    const QIcon icon = icons.iconForSymbol(symbol);
    items.append(ModelItemInfo(name, info, type,
                               QString::fromUtf8(symbol->fileName(), symbol->fileNameLength()),
                               symbol->line(),
                               icon));
220
}