aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/classview/classviewparsertreeitem.cpp
blob: 415064df4eeeac38dfdeda1de746479efb0c2aa1 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright (C) 2016 Denis Mingulov
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "classviewparsertreeitem.h"

#include "classviewconstants.h"

#include <cplusplus/Icons.h>
#include <cplusplus/Overview.h>

#include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/projectmanager.h>

#include <QDebug>
#include <QHash>
#include <QStandardItem>

namespace ClassView {
namespace Internal {

static CPlusPlus::Overview g_overview;

///////////////////////////////// ParserTreeItemPrivate //////////////////////////////////

/*!
    \class ParserTreeItemPrivate
    \brief The ParserTreeItemPrivate class defines private class data for
    the ParserTreeItem class.
   \sa ParserTreeItem
 */
class ParserTreeItemPrivate
{
public:
    void mergeWith(const ParserTreeItem::ConstPtr &target);
    void mergeSymbol(const CPlusPlus::Symbol *symbol);
    ParserTreeItem::ConstPtr cloneTree() const;

    QHash<SymbolInformation, ParserTreeItem::ConstPtr> m_symbolInformations;
    QSet<SymbolLocation> m_symbolLocations;
    const Utils::FilePath m_projectFilePath;
};

void ParserTreeItemPrivate::mergeWith(const ParserTreeItem::ConstPtr &target)
{
    if (!target)
        return;

    m_symbolLocations.unite(target->d->m_symbolLocations);

    // merge children
    for (auto it = target->d->m_symbolInformations.cbegin();
              it != target->d->m_symbolInformations.cend(); ++it) {
        const SymbolInformation &inf = it.key();
        const ParserTreeItem::ConstPtr &targetChild = it.value();

        ParserTreeItem::ConstPtr child = m_symbolInformations.value(inf);
        if (child) {
            child->d->mergeWith(targetChild);
        } else {
            const ParserTreeItem::ConstPtr clone = targetChild ? targetChild->d->cloneTree()
                                                               : ParserTreeItem::ConstPtr();
            m_symbolInformations.insert(inf, clone);
        }
    }
}

void ParserTreeItemPrivate::mergeSymbol(const CPlusPlus::Symbol *symbol)
{
    if (!symbol)
        return;

    // easy solution - lets add any scoped symbol and
    // any symbol which does not contain :: in the name

    //! \todo collect statistics and reorder to optimize
    if (symbol->asForwardClassDeclaration()
        || symbol->isExtern()
        || symbol->isFriend()
        || symbol->isGenerated()
        || symbol->asUsingNamespaceDirective()
        || symbol->asUsingDeclaration()
        )
        return;

    const CPlusPlus::Name *symbolName = symbol->name();
    if (symbolName && symbolName->asQualifiedNameId())
        return;

    QString name = g_overview.prettyName(symbolName).trimmed();
    QString type = g_overview.prettyType(symbol->type()).trimmed();
    int iconType = CPlusPlus::Icons::iconTypeForSymbol(symbol);

    SymbolInformation information(name, type, iconType);

    // If next line will be removed, 5% speed up for the initial parsing.
    // But there might be a problem for some files ???
    // Better to improve qHash timing
    ParserTreeItem::ConstPtr childItem = m_symbolInformations.value(information);

    if (!childItem)
        childItem = ParserTreeItem::ConstPtr(new ParserTreeItem());

    // locations have 1-based column in Symbol, use the same here.
    SymbolLocation location(symbol->filePath(),
                            symbol->line(), symbol->column());

    childItem->d->m_symbolLocations.insert(location);

    // prevent showing a content of the functions
    if (!symbol->asFunction()) {
        if (const CPlusPlus::Scope *scope = symbol->asScope()) {
            CPlusPlus::Scope::iterator cur = scope->memberBegin();
            CPlusPlus::Scope::iterator last = scope->memberEnd();
            while (cur != last) {
                const CPlusPlus::Symbol *curSymbol = *cur;
                ++cur;
                if (!curSymbol)
                    continue;

                childItem->d->mergeSymbol(curSymbol);
            }
        }
    }

    // if item is empty and has not to be added
    if (!symbol->asNamespace() || childItem->childCount())
        m_symbolInformations.insert(information, childItem);
}

/*!
    Creates a deep clone of this tree.
*/
ParserTreeItem::ConstPtr ParserTreeItemPrivate::cloneTree() const
{
    ParserTreeItem::ConstPtr newItem(new ParserTreeItem(m_projectFilePath));
    newItem->d->m_symbolLocations = m_symbolLocations;

    for (auto it = m_symbolInformations.cbegin(); it != m_symbolInformations.cend(); ++it) {
        ParserTreeItem::ConstPtr child = it.value();
        if (!child)
            continue;
        newItem->d->m_symbolInformations.insert(it.key(), child->d->cloneTree());
    }

    return newItem;
}

///////////////////////////////// ParserTreeItem //////////////////////////////////

/*!
    \class ParserTreeItem
    \brief The ParserTreeItem class is an item for the internal Class View tree.

    Not virtual - to speed up its work.
*/

ParserTreeItem::ParserTreeItem()
    : d(new ParserTreeItemPrivate())
{
}

ParserTreeItem::ParserTreeItem(const Utils::FilePath &projectFilePath)
    : d(new ParserTreeItemPrivate({{}, {}, projectFilePath}))
{
}

ParserTreeItem::ParserTreeItem(const QHash<SymbolInformation, ConstPtr> &children)
    : d(new ParserTreeItemPrivate({children, {}, {}}))
{
}

ParserTreeItem::~ParserTreeItem()
{
    delete d;
}

Utils::FilePath ParserTreeItem::projectFilePath() const
{
    return d->m_projectFilePath;
}

/*!
    Gets information about symbol positions.
    \sa SymbolLocation, addSymbolLocation, removeSymbolLocation
*/

QSet<SymbolLocation> ParserTreeItem::symbolLocations() const
{
    return d->m_symbolLocations;
}

/*!
    Returns the child item specified by \a inf symbol information.
*/

ParserTreeItem::ConstPtr ParserTreeItem::child(const SymbolInformation &inf) const
{
    return d->m_symbolInformations.value(inf);
}

/*!
    Returns the amount of children of the tree item.
*/

int ParserTreeItem::childCount() const
{
    return d->m_symbolInformations.count();
}

ParserTreeItem::ConstPtr ParserTreeItem::parseDocument(const CPlusPlus::Document::Ptr &doc)
{
    ConstPtr item(new ParserTreeItem());

    const unsigned total = doc->globalSymbolCount();
    for (unsigned i = 0; i < total; ++i)
        item->d->mergeSymbol(doc->globalSymbolAt(i));

    return item;
}

ParserTreeItem::ConstPtr ParserTreeItem::mergeTrees(const Utils::FilePath &projectFilePath,
                                               const QList<ConstPtr> &docTrees)
{
    ConstPtr item(new ParserTreeItem(projectFilePath));
    for (const ConstPtr &docTree : docTrees)
        item->d->mergeWith(docTree);

    return item;
}

/*!
    Converts internal location container to QVariant compatible.
    \a locations specifies a set of symbol locations.
    Returns a list of variant locations that can be added to the data of an
    item.
*/

static QList<QVariant> locationsToRole(const QSet<SymbolLocation> &locations)
{
    QList<QVariant> locationsVar;
    for (const SymbolLocation &loc : locations)
        locationsVar.append(QVariant::fromValue(loc));

    return locationsVar;
}

/*!
    Checks \a item in a QStandardItemModel for lazy data population.
    Make sure this method is called only from the GUI thread.
*/
bool ParserTreeItem::canFetchMore(QStandardItem *item) const
{
    if (!item)
        return false;
    return item->rowCount() < d->m_symbolInformations.count();
}

/*!
    Appends this item to the QStandardIten item \a item.
    Make sure this method is called only from the GUI thread.
*/
void ParserTreeItem::fetchMore(QStandardItem *item) const
{
    using ProjectExplorer::ProjectManager;
    if (!item)
        return;

    // convert to map - to sort it
    QMap<SymbolInformation, ConstPtr> map;
    for (auto it = d->m_symbolInformations.cbegin(); it != d->m_symbolInformations.cend(); ++it)
        map.insert(it.key(), it.value());

    for (auto it = map.cbegin(); it != map.cend(); ++it) {
        const SymbolInformation &inf = it.key();
        ConstPtr ptr = it.value();

        auto add = new QStandardItem;
        add->setData(inf.name(), Constants::SymbolNameRole);
        add->setData(inf.type(), Constants::SymbolTypeRole);
        add->setData(inf.iconType(), Constants::IconTypeRole);

        if (ptr) {
            // icon
            const Utils::FilePath &filePath = ptr->projectFilePath();
            if (!filePath.isEmpty()) {
                ProjectExplorer::Project *project = ProjectManager::projectForFile(filePath);
                if (project)
                    add->setIcon(project->containerNode()->icon());
            }

            // draggable
            if (!ptr->symbolLocations().isEmpty())
                add->setFlags(add->flags() | Qt::ItemIsDragEnabled);

            // locations
            add->setData(locationsToRole(ptr->symbolLocations()), Constants::SymbolLocationsRole);
        }
        item->appendRow(add);
    }
}

/*!
    Debug dump.
*/

void ParserTreeItem::debugDump(int indent) const
{
    for (auto it = d->m_symbolInformations.cbegin(); it != d->m_symbolInformations.cend(); ++it) {
        const SymbolInformation &inf = it.key();
        const ConstPtr &child = it.value();
        qDebug() << QString(2 * indent, QLatin1Char(' ')) << inf.iconType() << inf.name()
                 << inf.type() << bool(child);
        if (child)
            child->debugDump(indent + 1);
    }
}

} // namespace Internal
} // namespace ClassView