cpplocatorfilter.cpp 4.56 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
60
61
{
    m_searchList[doc->fileName()] = Info(doc);
}

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;
}

con's avatar
con committed
79
QList<Locator::FilterEntry> CppLocatorFilter::matchesFor(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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

    QMutableMapIterator<QString, Info> it(m_searchList);
    while (it.hasNext()) {
        it.next();

        Info info = it.value();
        if (info.dirty) {
            info.dirty = false;
            info.items = search(info.doc);
            it.setValue(info);
        }

        QList<ModelItemInfo> items = info.items;

        foreach (ModelItemInfo info, items) {
            if ((hasWildcard && regexp.exactMatch(info.symbolName))
                    || (!hasWildcard && matcher.indexIn(info.symbolName) != -1)) {
107

con's avatar
con committed
108
                QVariant id = qVariantFromValue(info);
con's avatar
con committed
109
                Locator::FilterEntry filterEntry(this, info.symbolName, id, info.icon);
110
111
112
113
114
                if (! info.symbolType.isEmpty())
                    filterEntry.extraInfo = info.symbolType;
                else
                    filterEntry.extraInfo = info.fileName;

115
116
117
118
                if (info.symbolName.startsWith(entry))
                    betterEntries.append(filterEntry);
                else
                    goodEntries.append(filterEntry);
con's avatar
con committed
119
120
121
122
            }
        }
    }

123
124
125
126
    if (goodEntries.size() < 1000)
        qSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
    if (betterEntries.size() < 1000)
        qSort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);
127

128
129
    betterEntries += goodEntries;
    return betterEntries;
con's avatar
con committed
130
131
}

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