cpplocatorfilter.cpp 4.63 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
12 13 14
** 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.
15
**
16 17 18 19 20 21 22
** 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.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
hjk's avatar
hjk committed
25

26
#include "cpplocatorfilter.h"
27
#include "cppmodelmanager.h"
con's avatar
con committed
28

29 30
#include <coreplugin/editormanager/editormanager.h>

31
#include <QStringMatcher>
Robert Loehning's avatar
Robert Loehning committed
32

33
using namespace CppTools;
con's avatar
con committed
34 35
using namespace CppTools::Internal;

36 37
CppLocatorFilter::CppLocatorFilter(CppLocatorData *locatorData)
    : m_data(locatorData)
con's avatar
con committed
38
{
hjk's avatar
hjk committed
39
    setId("Classes and Methods");
40
    setDisplayName(tr("C++ Classes, Enums and Functions"));
41
    setShortcutString(QString(QLatin1Char(':')));
con's avatar
con committed
42 43 44
    setIncludedByDefault(false);
}

45
CppLocatorFilter::~CppLocatorFilter()
con's avatar
con committed
46 47 48
{
}

49
Core::LocatorFilterEntry CppLocatorFilter::filterEntryFromIndexItem(IndexItem::Ptr info)
50 51
{
    const QVariant id = qVariantFromValue(info);
52
    Core::LocatorFilterEntry filterEntry(this, info->scopedSymbolName(), id, info->icon());
53
    if (info->type() == IndexItem::Class || info->type() == IndexItem::Enum)
54 55 56
        filterEntry.extraInfo = info->shortNativeFilePath();
    else
        filterEntry.extraInfo = info->symbolType();
57 58 59 60

    return filterEntry;
}

61
void CppLocatorFilter::refresh(QFutureInterface<void> &future)
con's avatar
con committed
62
{
63
    Q_UNUSED(future)
con's avatar
con committed
64 65
}

66 67
static bool compareLexigraphically(const Core::LocatorFilterEntry &a,
                                   const Core::LocatorFilterEntry &b)
68 69 70 71
{
    return a.displayName < b.displayName;
}

72 73
QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor(
        QFutureInterface<Core::LocatorFilterEntry> &future, const QString &origEntry)
con's avatar
con committed
74 75
{
    QString entry = trimWildcards(origEntry);
76 77
    QList<Core::LocatorFilterEntry> goodEntries;
    QList<Core::LocatorFilterEntry> betterEntries;
78
    const QChar asterisk = QLatin1Char('*');
con's avatar
con committed
79
    QStringMatcher matcher(entry, Qt::CaseInsensitive);
80
    QRegExp regexp(asterisk + entry+ asterisk, Qt::CaseInsensitive, QRegExp::Wildcard);
con's avatar
con committed
81
    if (!regexp.isValid())
82
        return goodEntries;
83
    bool hasWildcard = (entry.contains(asterisk) || entry.contains(QLatin1Char('?')));
84
    bool hasColonColon = entry.contains(QLatin1String("::"));
85
    const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);
86
    const IndexItem::ItemType wanted = matchTypes();
con's avatar
con committed
87

88 89 90 91 92 93 94
    m_data->filterAllFiles([&](const IndexItem::Ptr &info) -> IndexItem::VisitorResult {
        if (future.isCanceled())
            return IndexItem::Break;
        if (info->type() & wanted) {
            const QString matchString = hasColonColon ? info->scopedSymbolName() : info->symbolName();
            if ((hasWildcard && regexp.exactMatch(matchString)) ||
                    (!hasWildcard && matcher.indexIn(matchString) != -1)) {
95
                const Core::LocatorFilterEntry filterEntry = filterEntryFromIndexItem(info);
96
                if (matchString.startsWith(entry, caseSensitivityForPrefix))
97 98 99
                    betterEntries.append(filterEntry);
                else
                    goodEntries.append(filterEntry);
con's avatar
con committed
100 101
            }
        }
102 103 104 105 106 107

        if (info->type() & IndexItem::Enum)
            return IndexItem::Continue;
        else
            return IndexItem::Recurse;
    });
con's avatar
con committed
108

109
    if (goodEntries.size() < 1000)
110
        qStableSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
111
    if (betterEntries.size() < 1000)
112
        qStableSort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);
113

114 115
    betterEntries += goodEntries;
    return betterEntries;
con's avatar
con committed
116 117
}

118
void CppLocatorFilter::accept(Core::LocatorFilterEntry selection) const
con's avatar
con committed
119
{
120
    IndexItem::Ptr info = qvariant_cast<IndexItem::Ptr>(selection.internalData);
121
    Core::EditorManager::openEditorAt(info->fileName(), info->line(), info->column());
con's avatar
con committed
122
}