OSDN Git Service

e6c1d2978462fab858b4a42fa27158adc1e5a331
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljstools / qmljsfunctionfilter.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "qmljsfunctionfilter.h"
35 #include "qmljslocatordata.h"
36
37 #include <texteditor/itexteditor.h>
38 #include <texteditor/basetexteditor.h>
39
40 #include <QtCore/QStringMatcher>
41
42 using namespace QmlJSTools::Internal;
43
44 Q_DECLARE_METATYPE(LocatorData::Entry)
45
46 FunctionFilter::FunctionFilter(LocatorData *data, QObject *parent)
47     : Locator::ILocatorFilter(parent)
48     , m_data(data)
49 {
50     setShortcutString(QString(QLatin1Char('m')));
51     setIncludedByDefault(false);
52 }
53
54 FunctionFilter::~FunctionFilter()
55 { }
56
57 void FunctionFilter::refresh(QFutureInterface<void> &)
58 {
59 }
60
61 static bool compareLexigraphically(const Locator::FilterEntry &a,
62                                    const Locator::FilterEntry &b)
63 {
64     return a.displayName < b.displayName;
65 }
66
67 QList<Locator::FilterEntry> FunctionFilter::matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString &origEntry)
68 {
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())
76         return goodEntries;
77     bool hasWildcard = (entry.contains(asterisk) || entry.contains('?'));
78
79     QHashIterator<QString, QList<LocatorData::Entry> > it(m_data->entries());
80     while (it.hasNext()) {
81         if (future.isCanceled())
82             break;
83
84         it.next();
85
86         const QList<LocatorData::Entry> items = it.value();
87         foreach (const LocatorData::Entry &info, items) {
88             if (info.type != LocatorData::Function)
89                 continue;
90             if ((hasWildcard && regexp.exactMatch(info.symbolName))
91                     || (!hasWildcard && matcher.indexIn(info.symbolName) != -1)) {
92
93                 QVariant id = qVariantFromValue(info);
94                 Locator::FilterEntry filterEntry(this, info.displayName, id/*, info.icon*/);
95                 filterEntry.extraInfo = info.extraInfo;
96
97                 if (info.symbolName.startsWith(entry))
98                     betterEntries.append(filterEntry);
99                 else
100                     goodEntries.append(filterEntry);
101             }
102         }
103     }
104
105     if (goodEntries.size() < 1000)
106         qSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
107     if (betterEntries.size() < 1000)
108         qSort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);
109
110     betterEntries += goodEntries;
111     return betterEntries;
112 }
113
114 void FunctionFilter::accept(Locator::FilterEntry selection) const
115 {
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);
119 }