summaryrefslogtreecommitdiff
path: root/src/plugins/classview/classviewparsertreeitem.cpp
blob: cc5f79d3425a4951ae86d6a0abf2bb2df3306bd2 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/****************************************************************************
**
** Copyright (C) 2016 Denis Mingulov
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "classviewparsertreeitem.h"
#include "classviewsymbollocation.h"
#include "classviewsymbolinformation.h"
#include "classviewconstants.h"
#include "classviewutils.h"

#include <cplusplus/Icons.h>
#include <cplusplus/Name.h>
#include <cplusplus/Overview.h>
#include <cplusplus/Symbol.h>
#include <cplusplus/Symbols.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/session.h>
#include <utils/algorithm.h>

#include <QHash>
#include <QPair>
#include <QIcon>
#include <QStandardItem>

#include <QDebug>

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.isNull())
        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.isNull()) {
            child->d->mergeWith(targetChild);
        } else {
            const ParserTreeItem::ConstPtr clone = targetChild.isNull() ? ParserTreeItem::ConstPtr()
                                                                   : targetChild->d->cloneTree();
            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.isNull())
        childItem = ParserTreeItem::ConstPtr(new ParserTreeItem());

    // locations have 1-based column in Symbol, use the same here.
    SymbolLocation location(QString::fromUtf8(symbol->fileName() , symbol->fileNameLength()),
                            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.isNull())
            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::SessionManager;
    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.isNull()) {
            // icon
            const Utils::FilePath &filePath = ptr->projectFilePath();
            if (!filePath.isEmpty()) {
                ProjectExplorer::Project *project = SessionManager::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() << child.isNull();
        if (!child.isNull())
            child->debugDump(indent + 1);
    }
}

} // namespace Internal
} // namespace ClassView