cpplocatorfilter.cpp 5.17 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

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

29
#include <coreplugin/editormanager/editormanager.h>
30
#include <utils/algorithm.h>
31

32
#include <QRegularExpression>
Robert Loehning's avatar
Robert Loehning committed
33

34 35
#include <algorithm>

36
using namespace CppTools;
con's avatar
con committed
37 38
using namespace CppTools::Internal;

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

con's avatar
con committed
48
CppLocatorFilter::~CppLocatorFilter()
con's avatar
con committed
49 50 51
{
}

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

    return filterEntry;
}

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

69
QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor(
70
        QFutureInterface<Core::LocatorFilterEntry> &future, const QString &entry)
con's avatar
con committed
71
{
72 73
    QList<Core::LocatorFilterEntry> goodEntries;
    QList<Core::LocatorFilterEntry> betterEntries;
74 75
    QList<Core::LocatorFilterEntry> bestEntries;
    const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);
76
    bool hasColonColon = entry.contains(QLatin1String("::"));
77
    const IndexItem::ItemType wanted = matchTypes();
78

79
    const QRegularExpression regexp = createRegExp(entry);
80 81
    if (!regexp.isValid())
        return goodEntries;
con's avatar
con committed
82

83 84 85
    m_data->filterAllFiles([&](const IndexItem::Ptr &info) -> IndexItem::VisitorResult {
        if (future.isCanceled())
            return IndexItem::Break;
86 87
        const IndexItem::ItemType type = info->type();
        if (type & wanted) {
88
            QString matchString = hasColonColon ? info->scopedSymbolName() : info->symbolName();
89 90
            if (type == IndexItem::Function)
                matchString += info->symbolType();
91 92
            QRegularExpressionMatch match = regexp.match(matchString);
            if (match.hasMatch()) {
93 94
                Core::LocatorFilterEntry filterEntry = filterEntryFromIndexItem(info);

95 96 97 98
                // Highlight the matched characters, therefore it may be necessary
                // to update the match if the displayName is different from matchString
                if (matchString != filterEntry.displayName)
                    match = regexp.match(filterEntry.displayName);
99
                filterEntry.highlightInfo = highlightInfo(match);
100 101 102 103

                if (matchString.startsWith(entry, caseSensitivityForPrefix))
                    bestEntries.append(filterEntry);
                else if (matchString.contains(entry, caseSensitivityForPrefix))
104 105 106
                    betterEntries.append(filterEntry);
                else
                    goodEntries.append(filterEntry);
con's avatar
con committed
107 108
            }
        }
109 110 111 112 113 114

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

116
    if (goodEntries.size() < 1000)
117
        Utils::sort(goodEntries, Core::LocatorFilterEntry::compareLexigraphically);
118
    if (betterEntries.size() < 1000)
119
        Utils::sort(betterEntries, Core::LocatorFilterEntry::compareLexigraphically);
120 121
    if (bestEntries.size() < 1000)
        Utils::sort(bestEntries, Core::LocatorFilterEntry::compareLexigraphically);
122

123 124 125
    bestEntries += betterEntries;
    bestEntries += goodEntries;
    return bestEntries;
con's avatar
con committed
126 127
}

128 129
void CppLocatorFilter::accept(Core::LocatorFilterEntry selection,
                              QString *newText, int *selectionStart, int *selectionLength) const
con's avatar
con committed
130
{
131 132 133
    Q_UNUSED(newText)
    Q_UNUSED(selectionStart)
    Q_UNUSED(selectionLength)
134
    IndexItem::Ptr info = qvariant_cast<IndexItem::Ptr>(selection.internalData);
135
    Core::EditorManager::openEditorAt(info->fileName(), info->line(), info->column());
con's avatar
con committed
136
}