OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cpptools / cpplocatorfilter.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 "cpplocatorfilter.h"
35 #include "cppmodelmanager.h"
36
37 #include <texteditor/itexteditor.h>
38 #include <texteditor/basetexteditor.h>
39
40 #include <QtCore/QStringMatcher>
41
42 using namespace CppTools::Internal;
43
44 CppLocatorFilter::CppLocatorFilter(CppModelManager *manager)
45     : m_manager(manager),
46     m_forceNewSearchList(true)
47 {
48     setShortcutString(QString(QLatin1Char(':')));
49     setIncludedByDefault(false);
50
51     connect(manager, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
52             this, SLOT(onDocumentUpdated(CPlusPlus::Document::Ptr)));
53
54     connect(manager, SIGNAL(aboutToRemoveFiles(QStringList)),
55             this, SLOT(onAboutToRemoveFiles(QStringList)));
56 }
57
58 CppLocatorFilter::~CppLocatorFilter()
59 { }
60
61 void CppLocatorFilter::onDocumentUpdated(CPlusPlus::Document::Ptr doc)
62 {
63     m_searchList[doc->fileName()] = search(doc);
64 }
65
66 void CppLocatorFilter::onAboutToRemoveFiles(const QStringList &files)
67 {
68     foreach (const QString &file, files)
69         m_searchList.remove(file);
70 }
71
72 void CppLocatorFilter::refresh(QFutureInterface<void> &future)
73 {
74     Q_UNUSED(future)
75 }
76
77 static bool compareLexigraphically(const Locator::FilterEntry &a,
78                                    const Locator::FilterEntry &b)
79 {
80     return a.displayName < b.displayName;
81 }
82
83 QList<Locator::FilterEntry> CppLocatorFilter::matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString &origEntry)
84 {
85     QString entry = trimWildcards(origEntry);
86     QList<Locator::FilterEntry> goodEntries;
87     QList<Locator::FilterEntry> betterEntries;
88     const QChar asterisk = QLatin1Char('*');
89     QStringMatcher matcher(entry, Qt::CaseInsensitive);
90     const QRegExp regexp(asterisk + entry+ asterisk, Qt::CaseInsensitive, QRegExp::Wildcard);
91     if (!regexp.isValid())
92         return goodEntries;
93     bool hasWildcard = (entry.contains(asterisk) || entry.contains('?'));
94
95     QHashIterator<QString, QList<ModelItemInfo> > it(m_searchList);
96     while (it.hasNext()) {
97         if (future.isCanceled())
98             break;
99
100         it.next();
101
102         const QList<ModelItemInfo> items = it.value();
103         foreach (const ModelItemInfo &info, items) {
104             if ((hasWildcard && regexp.exactMatch(info.symbolName))
105                     || (!hasWildcard && matcher.indexIn(info.symbolName) != -1)) {
106
107                 QVariant id = qVariantFromValue(info);
108                 Locator::FilterEntry filterEntry(this, info.symbolName, id, info.icon);
109                 if (! info.symbolType.isEmpty())
110                     filterEntry.extraInfo = info.symbolType;
111                 else
112                     filterEntry.extraInfo = info.fileName;
113
114                 if (info.symbolName.startsWith(entry))
115                     betterEntries.append(filterEntry);
116                 else
117                     goodEntries.append(filterEntry);
118             }
119         }
120     }
121
122     if (goodEntries.size() < 1000)
123         qSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
124     if (betterEntries.size() < 1000)
125         qSort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);
126
127     betterEntries += goodEntries;
128     return betterEntries;
129 }
130
131 void CppLocatorFilter::accept(Locator::FilterEntry selection) const
132 {
133     ModelItemInfo info = qvariant_cast<CppTools::Internal::ModelItemInfo>(selection.internalData);
134     TextEditor::BaseTextEditor::openEditorAt(info.fileName, info.line, info.column,
135                                              QString(), Core::EditorManager::ModeSwitch);
136 }
137
138 void CppLocatorFilter::reset()
139 {
140     m_searchList.clear();
141     m_previousResults.clear();
142     m_previousEntry.clear();
143     m_forceNewSearchList = true;
144 }