searchsymbols.cpp 6.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** 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
** version 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/

#include "searchsymbols.h"

#include <Literals.h>
#include <Scope.h>

using namespace CPlusPlus;
using namespace CppTools::Internal;

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

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

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

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
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)
{
78
    if (!(symbolsToSearchFor & Enums))
79 80 81
        return false;

    QString name = symbolName(symbol);
82 83 84 85 86
    QString scopedName = scopedSymbolName(name);
    QString previousScope = switchScope(scopedName);
    appendItem(separateScope ? name : scopedName,
               separateScope ? previousScope : QString(),
               ModelItemInfo::Enum, symbol);
87 88 89 90 91 92 93 94 95 96
    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)
{
97
    if (!(symbolsToSearchFor & Functions))
98 99 100
        return false;

    QString name = symbolName(symbol);
101 102 103 104 105 106
    QString scopedName = scopedSymbolName(name);
    QString type = overview.prettyType(symbol->type(),
                                       separateScope ? symbol->name() : 0);
    appendItem(separateScope ? type : scopedName,
               separateScope ? _scope : type,
               ModelItemInfo::Method, symbol);
107 108 109 110 111
    return false;
}

bool SearchSymbols::visit(Namespace *symbol)
{
112
    QString name = findOrInsert(scopedSymbolName(symbol));
113 114 115 116 117 118 119 120 121 122 123 124 125
    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()) {
126
        QString name = scopedSymbolName(symbol);
127
        QString type = overview.prettyType(symbol->type());
128
        appendItems(name, type, ModelItemInfo::Method, symbol->fileName());
129 130 131 132 133 134 135
    }
    return false;
}
#endif

bool SearchSymbols::visit(Class *symbol)
{
136 137 138
    if (!(symbolsToSearchFor & Classes))
        return false;

139
    QString name = symbolName(symbol);
140 141 142 143 144
    QString scopedName = scopedSymbolName(name);
    QString previousScope = switchScope(scopedName);
    appendItem(separateScope ? name : scopedName,
               separateScope ? previousScope : QString(),
               ModelItemInfo::Class, symbol);
145 146 147 148 149 150 151 152
    Scope *members = symbol->members();
    for (unsigned i = 0; i < members->symbolCount(); ++i) {
        accept(members->symbolAt(i));
    }
    (void) switchScope(previousScope);
    return false;
}

153
QString SearchSymbols::scopedSymbolName(const QString &symbolName) const
154 155 156 157
{
    QString name = _scope;
    if (! name.isEmpty())
        name += QLatin1String("::");
158 159 160 161 162 163 164 165 166 167 168
    name += symbolName;
    return name;
}

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

QString SearchSymbols::symbolName(const Symbol *symbol) const
{
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    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(">");
    }
191 192 193 194 195 196
    return symbolName;
}

void SearchSymbols::appendItem(const QString &name,
                               const QString &info,
                               ModelItemInfo::ItemType type,
197
                               const Symbol *symbol)
198 199 200 201 202 203
{
    const QIcon icon = icons.iconForSymbol(symbol);
    items.append(ModelItemInfo(name, info, type,
                               QString::fromUtf8(symbol->fileName(), symbol->fileNameLength()),
                               symbol->line(),
                               icon));
204
}