qmljsfunctionfilter.cpp 4.36 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22
** 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.
23
**
hjk's avatar
hjk committed
24 25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29 30 31 32

#include "qmljsfunctionfilter.h"
#include "qmljslocatordata.h"

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

35
#include <QStringMatcher>
36 37 38

using namespace QmlJSTools::Internal;

39
Q_DECLARE_METATYPE(LocatorData::Entry)
40 41

FunctionFilter::FunctionFilter(LocatorData *data, QObject *parent)
42
    : Core::ILocatorFilter(parent)
43 44
    , m_data(data)
{
hjk's avatar
hjk committed
45
    setId("Functions");
46
    setDisplayName(tr("QML Functions"));
47 48 49 50 51 52 53 54 55 56 57
    setShortcutString(QString(QLatin1Char('m')));
    setIncludedByDefault(false);
}

FunctionFilter::~FunctionFilter()
{ }

void FunctionFilter::refresh(QFutureInterface<void> &)
{
}

58 59
static bool compareLexigraphically(const Core::LocatorFilterEntry &a,
                                   const Core::LocatorFilterEntry &b)
60 61 62 63
{
    return a.displayName < b.displayName;
}

64
QList<Core::LocatorFilterEntry> FunctionFilter::matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future, const QString &origEntry)
65 66
{
    QString entry = trimWildcards(origEntry);
67 68
    QList<Core::LocatorFilterEntry> goodEntries;
    QList<Core::LocatorFilterEntry> betterEntries;
69 70
    const QChar asterisk = QLatin1Char('*');
    QStringMatcher matcher(entry, Qt::CaseInsensitive);
71
    QRegExp regexp(asterisk + entry+ asterisk, Qt::CaseInsensitive, QRegExp::Wildcard);
72 73
    if (!regexp.isValid())
        return goodEntries;
74
    bool hasWildcard = (entry.contains(asterisk) || entry.contains(QLatin1Char('?')));
75
    const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

    QHashIterator<QString, QList<LocatorData::Entry> > it(m_data->entries());
    while (it.hasNext()) {
        if (future.isCanceled())
            break;

        it.next();

        const QList<LocatorData::Entry> items = it.value();
        foreach (const LocatorData::Entry &info, items) {
            if (info.type != LocatorData::Function)
                continue;
            if ((hasWildcard && regexp.exactMatch(info.symbolName))
                    || (!hasWildcard && matcher.indexIn(info.symbolName) != -1)) {

                QVariant id = qVariantFromValue(info);
92
                Core::LocatorFilterEntry filterEntry(this, info.displayName, id/*, info.icon*/);
93 94
                filterEntry.extraInfo = info.extraInfo;

95
                if (info.symbolName.startsWith(entry, caseSensitivityForPrefix))
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                    betterEntries.append(filterEntry);
                else
                    goodEntries.append(filterEntry);
            }
        }
    }

    if (goodEntries.size() < 1000)
        qSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
    if (betterEntries.size() < 1000)
        qSort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);

    betterEntries += goodEntries;
    return betterEntries;
}

112
void FunctionFilter::accept(Core::LocatorFilterEntry selection) const
113 114
{
    const LocatorData::Entry entry = qvariant_cast<LocatorData::Entry>(selection.internalData);
Eike Ziller's avatar
Eike Ziller committed
115
    Core::EditorManager::openEditorAt(entry.fileName, entry.line, entry.column);
116
}