OSDN Git Service

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