searchsymbols.h 4.82 KB
Newer Older
1
/**************************************************************************
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
27
**
28
**************************************************************************/
29
30
31
32
33
34
35
36
37
38
39
40
41

#ifndef SEARCHSYMBOLS_H
#define SEARCHSYMBOLS_H

#include <cplusplus/CppDocument.h>
#include <cplusplus/Icons.h>
#include <cplusplus/Overview.h>
#include <Symbols.h>
#include <SymbolVisitor.h>

#include <QIcon>
#include <QMetaType>
#include <QString>
42
#include <QSet>
43

hjk's avatar
hjk committed
44
45
#include <functional>

46
47
48
49
50
namespace CppTools {
namespace Internal {

struct ModelItemInfo
{
51
    enum ItemType { Enum, Class, Method, Declaration };
52
53

    ModelItemInfo()
54
        : type(Declaration),
55
56
          line(0),
          column(0)
57
58
59
60
61
    { }

    ModelItemInfo(const QString &symbolName,
                  const QString &symbolType,
                  ItemType type,
62
                  QStringList fullyQualifiedName,
63
64
                  const QString &fileName,
                  int line,
65
                  int column,
66
67
68
69
                  const QIcon &icon)
        : symbolName(symbolName),
          symbolType(symbolType),
          type(type),
70
          fullyQualifiedName(fullyQualifiedName),
71
72
          fileName(fileName),
          line(line),
73
          column(column),
74
75
76
          icon(icon)
    { }

77
78
79
80
81
82
83
84
85
86
87
    ModelItemInfo(const ModelItemInfo &otherInfo)
        : symbolName(otherInfo.symbolName),
          symbolType(otherInfo.symbolType),
          type(otherInfo.type),
          fullyQualifiedName(otherInfo.fullyQualifiedName),
          fileName(otherInfo.fileName),
          line(otherInfo.line),
          column(otherInfo.column),
          icon(otherInfo.icon)
    {  }

88
89
90
    QString symbolName;
    QString symbolType;
    ItemType type;
91
    QStringList fullyQualifiedName;
92
93
    QString fileName;
    int line;
94
    int column;
95
96
97
98
99
100
101
102
    QIcon icon;
};

class SearchSymbols: public std::unary_function<CPlusPlus::Document::Ptr, QList<ModelItemInfo> >,
                     protected CPlusPlus::SymbolVisitor
{
public:
    enum SymbolType {
103
104
105
106
        Classes      = 0x1,
        Functions    = 0x2,
        Enums        = 0x4,
        Declarations = 0x8
107
    };
108

109
    Q_DECLARE_FLAGS(SymbolTypes, SymbolType)
110

111
112
    static SymbolTypes AllTypes;

113
114
    SearchSymbols();

115
    void setSymbolsToSearchFor(SymbolTypes types);
116
    void setSeparateScope(bool separateScope);
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

    QList<ModelItemInfo> operator()(CPlusPlus::Document::Ptr doc)
    { return operator()(doc, QString()); }

    QList<ModelItemInfo> operator()(CPlusPlus::Document::Ptr doc, const QString &scope);

protected:
    using SymbolVisitor::visit;

    void accept(CPlusPlus::Symbol *symbol)
    { CPlusPlus::Symbol::visitSymbol(symbol, this); }

    QString switchScope(const QString &scope);
    virtual bool visit(CPlusPlus::Enum *symbol);
    virtual bool visit(CPlusPlus::Function *symbol);
    virtual bool visit(CPlusPlus::Namespace *symbol);
    virtual bool visit(CPlusPlus::Declaration *symbol);
    virtual bool visit(CPlusPlus::Class *symbol);
135
136
137

    QString scopedSymbolName(const QString &symbolName) const;
    QString scopedSymbolName(const CPlusPlus::Symbol *symbol) const;
138
    QString symbolName(const CPlusPlus::Symbol *symbol) const;
139
140
141
    void appendItem(const QString &name,
                    const QString &info,
                    ModelItemInfo::ItemType type,
142
                    CPlusPlus::Symbol *symbol);
143
144

private:
145
146
147
148
149
    QString findOrInsert(const QString &s)
    { return *strings.insert(s); }

    QSet<QString> strings;            // Used to avoid QString duplication

150
151
152
153
    QString _scope;
    CPlusPlus::Overview overview;
    CPlusPlus::Icons icons;
    QList<ModelItemInfo> items;
154
    SymbolTypes symbolsToSearchFor;
155
    bool separateScope;
156
157
158
159
160
};

} // namespace Internal
} // namespace CppTools

161
Q_DECLARE_OPERATORS_FOR_FLAGS(CppTools::Internal::SearchSymbols::SymbolTypes)
162
163
164
Q_DECLARE_METATYPE(CppTools::Internal::ModelItemInfo)

#endif // SEARCHSYMBOLS_H