cpplocatorfilter.cpp 4.94 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

31
#include "cpplocatorfilter.h"
32
#include "cppmodelmanager.h"
con's avatar
con committed
33

34 35
#include <coreplugin/editormanager/editormanager.h>

36
#include <QStringMatcher>
Robert Loehning's avatar
Robert Loehning committed
37

38
using namespace CppTools;
con's avatar
con committed
39 40
using namespace CppTools::Internal;

41 42
CppLocatorFilter::CppLocatorFilter(CppLocatorData *locatorData)
    : m_data(locatorData)
con's avatar
con committed
43
{
hjk's avatar
hjk committed
44
    setId("Classes and Methods");
45
    setDisplayName(tr("C++ Classes, Enums and Functions"));
46
    setShortcutString(QString(QLatin1Char(':')));
con's avatar
con committed
47 48 49
    setIncludedByDefault(false);
}

50
CppLocatorFilter::~CppLocatorFilter()
con's avatar
con committed
51 52 53
{
}

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

    return filterEntry;
}

66
void CppLocatorFilter::refresh(QFutureInterface<void> &future)
con's avatar
con committed
67
{
68
    Q_UNUSED(future)
con's avatar
con committed
69 70
}

71 72
static bool compareLexigraphically(const Core::LocatorFilterEntry &a,
                                   const Core::LocatorFilterEntry &b)
73 74 75 76
{
    return a.displayName < b.displayName;
}

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

93 94 95 96 97 98 99
    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)) {
100
                const Core::LocatorFilterEntry filterEntry = filterEntryFromIndexItem(info);
101
                if (matchString.startsWith(entry, caseSensitivityForPrefix))
102 103 104
                    betterEntries.append(filterEntry);
                else
                    goodEntries.append(filterEntry);
con's avatar
con committed
105 106
            }
        }
107 108 109 110 111 112

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

114
    if (goodEntries.size() < 1000)
115
        qStableSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
116
    if (betterEntries.size() < 1000)
117
        qStableSort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);
118

119 120
    betterEntries += goodEntries;
    return betterEntries;
con's avatar
con committed
121 122
}

123
void CppLocatorFilter::accept(Core::LocatorFilterEntry selection) const
con's avatar
con committed
124
{
125
    IndexItem::Ptr info = qvariant_cast<IndexItem::Ptr>(selection.internalData);
126
    Core::EditorManager::openEditorAt(info->fileName(), info->line(), info->column());
con's avatar
con committed
127
}