1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "qmljsfunctionfilter.h"
35 #include "qmljslocatordata.h"
37 #include <texteditor/itexteditor.h>
38 #include <texteditor/basetexteditor.h>
40 #include <QtCore/QStringMatcher>
42 using namespace QmlJSTools::Internal;
44 Q_DECLARE_METATYPE(LocatorData::Entry)
46 FunctionFilter::FunctionFilter(LocatorData *data, QObject *parent)
47 : Locator::ILocatorFilter(parent)
50 setShortcutString(QString(QLatin1Char('m')));
51 setIncludedByDefault(false);
54 FunctionFilter::~FunctionFilter()
57 void FunctionFilter::refresh(QFutureInterface<void> &)
61 static bool compareLexigraphically(const Locator::FilterEntry &a,
62 const Locator::FilterEntry &b)
64 return a.displayName < b.displayName;
67 QList<Locator::FilterEntry> FunctionFilter::matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString &origEntry)
69 QString entry = trimWildcards(origEntry);
70 QList<Locator::FilterEntry> goodEntries;
71 QList<Locator::FilterEntry> betterEntries;
72 const QChar asterisk = QLatin1Char('*');
73 QStringMatcher matcher(entry, Qt::CaseInsensitive);
74 const QRegExp regexp(asterisk + entry+ asterisk, Qt::CaseInsensitive, QRegExp::Wildcard);
75 if (!regexp.isValid())
77 bool hasWildcard = (entry.contains(asterisk) || entry.contains('?'));
79 QHashIterator<QString, QList<LocatorData::Entry> > it(m_data->entries());
80 while (it.hasNext()) {
81 if (future.isCanceled())
86 const QList<LocatorData::Entry> items = it.value();
87 foreach (const LocatorData::Entry &info, items) {
88 if (info.type != LocatorData::Function)
90 if ((hasWildcard && regexp.exactMatch(info.symbolName))
91 || (!hasWildcard && matcher.indexIn(info.symbolName) != -1)) {
93 QVariant id = qVariantFromValue(info);
94 Locator::FilterEntry filterEntry(this, info.displayName, id/*, info.icon*/);
95 filterEntry.extraInfo = info.extraInfo;
97 if (info.symbolName.startsWith(entry))
98 betterEntries.append(filterEntry);
100 goodEntries.append(filterEntry);
105 if (goodEntries.size() < 1000)
106 qSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
107 if (betterEntries.size() < 1000)
108 qSort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);
110 betterEntries += goodEntries;
111 return betterEntries;
114 void FunctionFilter::accept(Locator::FilterEntry selection) const
116 const LocatorData::Entry entry = qvariant_cast<LocatorData::Entry>(selection.internalData);
117 TextEditor::BaseTextEditorWidget::openEditorAt(entry.fileName, entry.line, entry.column,
118 QString(), Core::EditorManager::ModeSwitch);