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 "cpplocatorfilter.h"
35 #include "cppmodelmanager.h"
37 #include <texteditor/itexteditor.h>
38 #include <texteditor/basetexteditor.h>
40 #include <QtCore/QStringMatcher>
42 using namespace CppTools::Internal;
44 CppLocatorFilter::CppLocatorFilter(CppModelManager *manager)
46 m_forceNewSearchList(true)
48 setShortcutString(QString(QLatin1Char(':')));
49 setIncludedByDefault(false);
51 connect(manager, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
52 this, SLOT(onDocumentUpdated(CPlusPlus::Document::Ptr)));
54 connect(manager, SIGNAL(aboutToRemoveFiles(QStringList)),
55 this, SLOT(onAboutToRemoveFiles(QStringList)));
58 CppLocatorFilter::~CppLocatorFilter()
61 void CppLocatorFilter::onDocumentUpdated(CPlusPlus::Document::Ptr doc)
63 m_searchList[doc->fileName()] = search(doc);
66 void CppLocatorFilter::onAboutToRemoveFiles(const QStringList &files)
68 foreach (const QString &file, files)
69 m_searchList.remove(file);
72 void CppLocatorFilter::refresh(QFutureInterface<void> &future)
77 static bool compareLexigraphically(const Locator::FilterEntry &a,
78 const Locator::FilterEntry &b)
80 return a.displayName < b.displayName;
83 QList<Locator::FilterEntry> CppLocatorFilter::matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString &origEntry)
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())
93 bool hasWildcard = (entry.contains(asterisk) || entry.contains('?'));
95 QHashIterator<QString, QList<ModelItemInfo> > it(m_searchList);
96 while (it.hasNext()) {
97 if (future.isCanceled())
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)) {
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;
112 filterEntry.extraInfo = info.fileName;
114 if (info.symbolName.startsWith(entry))
115 betterEntries.append(filterEntry);
117 goodEntries.append(filterEntry);
122 if (goodEntries.size() < 1000)
123 qSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
124 if (betterEntries.size() < 1000)
125 qSort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);
127 betterEntries += goodEntries;
128 return betterEntries;
131 void CppLocatorFilter::accept(Locator::FilterEntry selection) const
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);
138 void CppLocatorFilter::reset()
140 m_searchList.clear();
141 m_previousResults.clear();
142 m_previousEntry.clear();
143 m_forceNewSearchList = true;