cpplocatorfilter.cpp 4.66 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

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

#include <texteditor/itexteditor.h>
#include <texteditor/basetexteditor.h>

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

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

40
CppLocatorFilter::CppLocatorFilter(CppModelManager *manager)
con's avatar
con committed
41 42 43
    : m_manager(manager),
    m_forceNewSearchList(true)
{
44
    setShortcutString(QString(QLatin1Char(':')));
con's avatar
con committed
45 46 47 48 49 50 51 52 53
    setIncludedByDefault(false);

    connect(manager, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
            this, SLOT(onDocumentUpdated(CPlusPlus::Document::Ptr)));

    connect(manager, SIGNAL(aboutToRemoveFiles(QStringList)),
            this, SLOT(onAboutToRemoveFiles(QStringList)));
}

con's avatar
con committed
54
CppLocatorFilter::~CppLocatorFilter()
con's avatar
con committed
55 56
{ }

con's avatar
con committed
57
void CppLocatorFilter::onDocumentUpdated(CPlusPlus::Document::Ptr doc)
con's avatar
con committed
58
{
59
    m_searchList[doc->fileName()] = search(doc);
con's avatar
con committed
60 61
}

con's avatar
con committed
62
void CppLocatorFilter::onAboutToRemoveFiles(const QStringList &files)
con's avatar
con committed
63
{
64
    foreach (const QString &file, files)
con's avatar
con committed
65 66 67
        m_searchList.remove(file);
}

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

con's avatar
con committed
73 74
static bool compareLexigraphically(const Locator::FilterEntry &a,
                                   const Locator::FilterEntry &b)
75 76 77 78
{
    return a.displayName < b.displayName;
}

79
QList<Locator::FilterEntry> CppLocatorFilter::matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString &origEntry)
con's avatar
con committed
80 81
{
    QString entry = trimWildcards(origEntry);
con's avatar
con committed
82 83
    QList<Locator::FilterEntry> goodEntries;
    QList<Locator::FilterEntry> betterEntries;
84
    const QChar asterisk = QLatin1Char('*');
con's avatar
con committed
85
    QStringMatcher matcher(entry, Qt::CaseInsensitive);
86
    const QRegExp regexp(asterisk + entry+ asterisk, Qt::CaseInsensitive, QRegExp::Wildcard);
con's avatar
con committed
87
    if (!regexp.isValid())
88
        return goodEntries;
89
    bool hasWildcard = (entry.contains(asterisk) || entry.contains('?'));
con's avatar
con committed
90

91
    QHashIterator<QString, QList<ModelItemInfo> > it(m_searchList);
con's avatar
con committed
92
    while (it.hasNext()) {
93 94 95
        if (future.isCanceled())
            break;

con's avatar
con committed
96 97
        it.next();

98 99
        const QList<ModelItemInfo> items = it.value();
        foreach (const ModelItemInfo &info, items) {
con's avatar
con committed
100 101
            if ((hasWildcard && regexp.exactMatch(info.symbolName))
                    || (!hasWildcard && matcher.indexIn(info.symbolName) != -1)) {
102

con's avatar
con committed
103
                QVariant id = qVariantFromValue(info);
con's avatar
con committed
104
                Locator::FilterEntry filterEntry(this, info.symbolName, id, info.icon);
105 106 107 108 109
                if (! info.symbolType.isEmpty())
                    filterEntry.extraInfo = info.symbolType;
                else
                    filterEntry.extraInfo = info.fileName;

110 111 112 113
                if (info.symbolName.startsWith(entry))
                    betterEntries.append(filterEntry);
                else
                    goodEntries.append(filterEntry);
con's avatar
con committed
114 115 116 117
            }
        }
    }

118 119 120 121
    if (goodEntries.size() < 1000)
        qSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
    if (betterEntries.size() < 1000)
        qSort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);
122

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

con's avatar
con committed
127
void CppLocatorFilter::accept(Locator::FilterEntry selection) const
con's avatar
con committed
128 129
{
    ModelItemInfo info = qvariant_cast<CppTools::Internal::ModelItemInfo>(selection.internalData);
130
    TextEditor::BaseTextEditor::openEditorAt(info.fileName, info.line, info.column);
con's avatar
con committed
131
}
Roberto Raggi's avatar
Roberto Raggi committed
132 133 134 135 136 137 138 139

void CppLocatorFilter::reset()
{
    m_searchList.clear();
    m_previousResults.clear();
    m_previousEntry.clear();
    m_forceNewSearchList = true;
}