1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "qmljsfunctionfilter.h"
34 #include "qmljslocatordata.h"
36 #include <texteditor/itexteditor.h>
37 #include <texteditor/basetexteditor.h>
39 #include <QtCore/QStringMatcher>
41 using namespace QmlJSTools::Internal;
43 Q_DECLARE_METATYPE(LocatorData::Entry)
45 FunctionFilter::FunctionFilter(LocatorData *data, QObject *parent)
46 : Locator::ILocatorFilter(parent)
49 setShortcutString(QString(QLatin1Char('m')));
50 setIncludedByDefault(false);
53 FunctionFilter::~FunctionFilter()
56 void FunctionFilter::refresh(QFutureInterface<void> &)
60 static bool compareLexigraphically(const Locator::FilterEntry &a,
61 const Locator::FilterEntry &b)
63 return a.displayName < b.displayName;
66 QList<Locator::FilterEntry> FunctionFilter::matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString &origEntry)
68 QString entry = trimWildcards(origEntry);
69 QList<Locator::FilterEntry> goodEntries;
70 QList<Locator::FilterEntry> betterEntries;
71 const QChar asterisk = QLatin1Char('*');
72 QStringMatcher matcher(entry, Qt::CaseInsensitive);
73 const QRegExp regexp(asterisk + entry+ asterisk, Qt::CaseInsensitive, QRegExp::Wildcard);
74 if (!regexp.isValid())
76 bool hasWildcard = (entry.contains(asterisk) || entry.contains('?'));
78 QHashIterator<QString, QList<LocatorData::Entry> > it(m_data->entries());
79 while (it.hasNext()) {
80 if (future.isCanceled())
85 const QList<LocatorData::Entry> items = it.value();
86 foreach (const LocatorData::Entry &info, items) {
87 if (info.type != LocatorData::Function)
89 if ((hasWildcard && regexp.exactMatch(info.symbolName))
90 || (!hasWildcard && matcher.indexIn(info.symbolName) != -1)) {
92 QVariant id = qVariantFromValue(info);
93 Locator::FilterEntry filterEntry(this, info.displayName, id/*, info.icon*/);
94 filterEntry.extraInfo = info.extraInfo;
96 if (info.symbolName.startsWith(entry))
97 betterEntries.append(filterEntry);
99 goodEntries.append(filterEntry);
104 if (goodEntries.size() < 1000)
105 qSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
106 if (betterEntries.size() < 1000)
107 qSort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);
109 betterEntries += goodEntries;
110 return betterEntries;
113 void FunctionFilter::accept(Locator::FilterEntry selection) const
115 const LocatorData::Entry entry = qvariant_cast<LocatorData::Entry>(selection.internalData);
116 TextEditor::BaseTextEditorWidget::openEditorAt(entry.fileName, entry.line, entry.column,
117 QString(), Core::EditorManager::ModeSwitch);