Commit 2bda8675 authored by con's avatar con
Browse files

C++ symbols find filter for advanced find.

Required refactoring of the search result window to show real trees of
search results.

The backend is the backend from the Locator filter, which is a bit
outdated now.
parent 80d85e28
...@@ -17,5 +17,6 @@ Alternatively, this plugin may be used under the terms of the GNU Lesser General ...@@ -17,5 +17,6 @@ Alternatively, this plugin may be used under the terms of the GNU Lesser General
<dependency name="TextEditor" version="2.1.80"/> <dependency name="TextEditor" version="2.1.80"/>
<dependency name="ProjectExplorer" version="2.1.80"/> <dependency name="ProjectExplorer" version="2.1.80"/>
<dependency name="Locator" version="2.1.80"/> <dependency name="Locator" version="2.1.80"/>
<dependency name="Find" version="2.1.80"/>
</dependencyList> </dependencyList>
</plugin> </plugin>
...@@ -31,8 +31,8 @@ ...@@ -31,8 +31,8 @@
using namespace CppTools::Internal; using namespace CppTools::Internal;
CppClassesFilter::CppClassesFilter(CppModelManager *manager, Core::EditorManager *editorManager) CppClassesFilter::CppClassesFilter(CppModelManager *manager)
: CppLocatorFilter(manager, editorManager) : CppLocatorFilter(manager)
{ {
setShortcutString("c"); setShortcutString("c");
setIncludedByDefault(false); setIncludedByDefault(false);
......
...@@ -40,7 +40,7 @@ class CppClassesFilter : public CppLocatorFilter ...@@ -40,7 +40,7 @@ class CppClassesFilter : public CppLocatorFilter
Q_OBJECT Q_OBJECT
public: public:
CppClassesFilter(CppModelManager *manager, Core::EditorManager *editorManager); CppClassesFilter(CppModelManager *manager);
~CppClassesFilter(); ~CppClassesFilter();
QString displayName() const { return tr("Classes"); } QString displayName() const { return tr("Classes"); }
......
...@@ -313,7 +313,11 @@ void CppFindReferences::searchFinished() ...@@ -313,7 +313,11 @@ void CppFindReferences::searchFinished()
void CppFindReferences::openEditor(const Find::SearchResultItem &item) void CppFindReferences::openEditor(const Find::SearchResultItem &item)
{ {
TextEditor::BaseTextEditor::openEditorAt(item.fileName, item.lineNumber, item.searchTermStart); if (item.path.size() > 0) {
TextEditor::BaseTextEditor::openEditorAt(item.path.first(), item.lineNumber, item.textMarkPos);
} else {
Core::EditorManager::instance()->openEditor(item.text);
}
} }
......
...@@ -31,8 +31,8 @@ ...@@ -31,8 +31,8 @@
using namespace CppTools::Internal; using namespace CppTools::Internal;
CppFunctionsFilter::CppFunctionsFilter(CppModelManager *manager, Core::EditorManager *editorManager) CppFunctionsFilter::CppFunctionsFilter(CppModelManager *manager)
: CppLocatorFilter(manager, editorManager) : CppLocatorFilter(manager)
{ {
setShortcutString(QString(QLatin1Char('m'))); setShortcutString(QString(QLatin1Char('m')));
setIncludedByDefault(false); setIncludedByDefault(false);
......
...@@ -40,7 +40,7 @@ class CppFunctionsFilter : public CppLocatorFilter ...@@ -40,7 +40,7 @@ class CppFunctionsFilter : public CppLocatorFilter
Q_OBJECT Q_OBJECT
public: public:
CppFunctionsFilter(CppModelManager *manager, Core::EditorManager *editorManager); CppFunctionsFilter(CppModelManager *manager);
~CppFunctionsFilter(); ~CppFunctionsFilter();
QString displayName() const { return tr("Methods"); } QString displayName() const { return tr("Methods"); }
......
...@@ -30,8 +30,6 @@ ...@@ -30,8 +30,6 @@
#include "cpplocatorfilter.h" #include "cpplocatorfilter.h"
#include "cppmodelmanager.h" #include "cppmodelmanager.h"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <texteditor/itexteditor.h> #include <texteditor/itexteditor.h>
#include <texteditor/basetexteditor.h> #include <texteditor/basetexteditor.h>
...@@ -39,9 +37,8 @@ ...@@ -39,9 +37,8 @@
using namespace CppTools::Internal; using namespace CppTools::Internal;
CppLocatorFilter::CppLocatorFilter(CppModelManager *manager, Core::EditorManager *editorManager) CppLocatorFilter::CppLocatorFilter(CppModelManager *manager)
: m_manager(manager), : m_manager(manager),
m_editorManager(editorManager),
m_forceNewSearchList(true) m_forceNewSearchList(true)
{ {
setShortcutString(QString(QLatin1Char(':'))); setShortcutString(QString(QLatin1Char(':')));
......
...@@ -34,10 +34,6 @@ ...@@ -34,10 +34,6 @@
#include <locator/ilocatorfilter.h> #include <locator/ilocatorfilter.h>
namespace Core {
class EditorManager;
}
namespace CppTools { namespace CppTools {
namespace Internal { namespace Internal {
...@@ -47,7 +43,7 @@ class CppLocatorFilter : public Locator::ILocatorFilter ...@@ -47,7 +43,7 @@ class CppLocatorFilter : public Locator::ILocatorFilter
{ {
Q_OBJECT Q_OBJECT
public: public:
CppLocatorFilter(CppModelManager *manager, Core::EditorManager *editorManager); CppLocatorFilter(CppModelManager *manager);
~CppLocatorFilter(); ~CppLocatorFilter();
QString displayName() const { return tr("Classes and Methods"); } QString displayName() const { return tr("Classes and Methods"); }
...@@ -66,7 +62,6 @@ private slots: ...@@ -66,7 +62,6 @@ private slots:
private: private:
CppModelManager *m_manager; CppModelManager *m_manager;
Core::EditorManager *m_editorManager;
struct Info { struct Info {
Info(): dirty(true) {} Info(): dirty(true) {}
......
...@@ -24,7 +24,8 @@ HEADERS += completionsettingspage.h \ ...@@ -24,7 +24,8 @@ HEADERS += completionsettingspage.h \
cppdoxygen.h \ cppdoxygen.h \
cppfilesettingspage.h \ cppfilesettingspage.h \
cppfindreferences.h \ cppfindreferences.h \
cppcodeformatter.h cppcodeformatter.h \
symbolsfindfilter.h
SOURCES += completionsettingspage.cpp \ SOURCES += completionsettingspage.cpp \
cppclassesfilter.cpp \ cppclassesfilter.cpp \
...@@ -40,7 +41,8 @@ SOURCES += completionsettingspage.cpp \ ...@@ -40,7 +41,8 @@ SOURCES += completionsettingspage.cpp \
cppfilesettingspage.cpp \ cppfilesettingspage.cpp \
abstracteditorsupport.cpp \ abstracteditorsupport.cpp \
cppfindreferences.cpp \ cppfindreferences.cpp \
cppcodeformatter.cpp cppcodeformatter.cpp \
symbolsfindfilter.cpp
FORMS += completionsettingspage.ui \ FORMS += completionsettingspage.ui \
cppfilesettingspage.ui cppfilesettingspage.ui
......
include($$IDE_SOURCE_TREE/src/libs/cplusplus/cplusplus.pri) include($$IDE_SOURCE_TREE/src/libs/cplusplus/cplusplus.pri)
include($$IDE_SOURCE_TREE/src/plugins/projectexplorer/projectexplorer.pri) include($$IDE_SOURCE_TREE/src/plugins/projectexplorer/projectexplorer.pri)
include($$IDE_SOURCE_TREE/src/plugins/texteditor/texteditor.pri) include($$IDE_SOURCE_TREE/src/plugins/texteditor/texteditor.pri)
include($$IDE_SOURCE_TREE/src/plugins/find/find.pri)
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include "cppmodelmanager.h" #include "cppmodelmanager.h"
#include "cpptoolsconstants.h" #include "cpptoolsconstants.h"
#include "cpplocatorfilter.h" #include "cpplocatorfilter.h"
#include "symbolsfindfilter.h"
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
...@@ -111,14 +112,13 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error) ...@@ -111,14 +112,13 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
CppCodeCompletion *completion = new CppCodeCompletion(m_modelManager); CppCodeCompletion *completion = new CppCodeCompletion(m_modelManager);
addAutoReleasedObject(completion); addAutoReleasedObject(completion);
CppLocatorFilter *locatorFilter = new CppLocatorFilter(m_modelManager, addAutoReleasedObject(new CppLocatorFilter(m_modelManager));
core->editorManager()); addAutoReleasedObject(new CppClassesFilter(m_modelManager));
addAutoReleasedObject(locatorFilter); addAutoReleasedObject(new CppFunctionsFilter(m_modelManager));
addAutoReleasedObject(new CppClassesFilter(m_modelManager, core->editorManager()));
addAutoReleasedObject(new CppFunctionsFilter(m_modelManager, core->editorManager()));
addAutoReleasedObject(new CppCurrentDocumentFilter(m_modelManager, core->editorManager())); addAutoReleasedObject(new CppCurrentDocumentFilter(m_modelManager, core->editorManager()));
addAutoReleasedObject(new CompletionSettingsPage); addAutoReleasedObject(new CompletionSettingsPage);
addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings)); addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings));
addAutoReleasedObject(new SymbolsFindFilter(m_modelManager));
// Menus // Menus
Core::ActionContainer *mtools = am->actionContainer(Core::Constants::M_TOOLS); Core::ActionContainer *mtools = am->actionContainer(Core::Constants::M_TOOLS);
......
...@@ -32,10 +32,17 @@ ...@@ -32,10 +32,17 @@
#include <Literals.h> #include <Literals.h>
#include <Scope.h> #include <Scope.h>
#include <Names.h> #include <Names.h>
#include <cplusplus/LookupContext.h>
using namespace CPlusPlus; using namespace CPlusPlus;
using namespace CppTools::Internal; using namespace CppTools::Internal;
SearchSymbols::SymbolTypes SearchSymbols::AllTypes =
SearchSymbols::Classes
| SearchSymbols::Functions
| SearchSymbols::Enums
| SearchSymbols::Declarations;
SearchSymbols::SearchSymbols(): SearchSymbols::SearchSymbols():
symbolsToSearchFor(Classes | Functions | Enums), symbolsToSearchFor(Classes | Functions | Enums),
separateScope(false) separateScope(false)
...@@ -204,13 +211,17 @@ QString SearchSymbols::symbolName(const Symbol *symbol) const ...@@ -204,13 +211,17 @@ QString SearchSymbols::symbolName(const Symbol *symbol) const
void SearchSymbols::appendItem(const QString &name, void SearchSymbols::appendItem(const QString &name,
const QString &info, const QString &info,
ModelItemInfo::ItemType type, ModelItemInfo::ItemType type,
const Symbol *symbol) Symbol *symbol)
{ {
if (!symbol->name()) if (!symbol->name())
return; return;
QStringList fullyQualifiedName;
foreach (const Name *name, LookupContext::fullyQualifiedName(symbol))
fullyQualifiedName.append(overview.prettyName(name));
const QIcon icon = icons.iconForSymbol(symbol); const QIcon icon = icons.iconForSymbol(symbol);
items.append(ModelItemInfo(name, info, type, items.append(ModelItemInfo(name, info, type,
fullyQualifiedName,
QString::fromUtf8(symbol->fileName(), symbol->fileNameLength()), QString::fromUtf8(symbol->fileName(), symbol->fileNameLength()),
symbol->line(), symbol->line(),
symbol->column() - 1, // 1-based vs 0-based column symbol->column() - 1, // 1-based vs 0-based column
......
...@@ -52,12 +52,14 @@ struct ModelItemInfo ...@@ -52,12 +52,14 @@ struct ModelItemInfo
ModelItemInfo() ModelItemInfo()
: type(Declaration), : type(Declaration),
line(0) line(0),
column(0)
{ } { }
ModelItemInfo(const QString &symbolName, ModelItemInfo(const QString &symbolName,
const QString &symbolType, const QString &symbolType,
ItemType type, ItemType type,
QStringList fullyQualifiedName,
const QString &fileName, const QString &fileName,
int line, int line,
int column, int column,
...@@ -65,15 +67,28 @@ struct ModelItemInfo ...@@ -65,15 +67,28 @@ struct ModelItemInfo
: symbolName(symbolName), : symbolName(symbolName),
symbolType(symbolType), symbolType(symbolType),
type(type), type(type),
fullyQualifiedName(fullyQualifiedName),
fileName(fileName), fileName(fileName),
line(line), line(line),
column(column), column(column),
icon(icon) icon(icon)
{ } { }
ModelItemInfo(const ModelItemInfo &otherInfo)
: symbolName(otherInfo.symbolName),
symbolType(otherInfo.symbolType),
type(otherInfo.type),
fullyQualifiedName(otherInfo.fullyQualifiedName),
fileName(otherInfo.fileName),
line(otherInfo.line),
column(otherInfo.column),
icon(otherInfo.icon)
{ }
QString symbolName; QString symbolName;
QString symbolType; QString symbolType;
ItemType type; ItemType type;
QStringList fullyQualifiedName;
QString fileName; QString fileName;
int line; int line;
int column; int column;
...@@ -90,8 +105,11 @@ public: ...@@ -90,8 +105,11 @@ public:
Enums = 0x4, Enums = 0x4,
Declarations = 0x8 Declarations = 0x8
}; };
Q_DECLARE_FLAGS(SymbolTypes, SymbolType) Q_DECLARE_FLAGS(SymbolTypes, SymbolType)
static SymbolTypes AllTypes;
SearchSymbols(); SearchSymbols();
void setSymbolsToSearchFor(SymbolTypes types); void setSymbolsToSearchFor(SymbolTypes types);
...@@ -121,7 +139,7 @@ protected: ...@@ -121,7 +139,7 @@ protected:
void appendItem(const QString &name, void appendItem(const QString &name,
const QString &info, const QString &info,
ModelItemInfo::ItemType type, ModelItemInfo::ItemType type,
const CPlusPlus::Symbol *symbol); CPlusPlus::Symbol *symbol);
private: private:
QString findOrInsert(const QString &s) QString findOrInsert(const QString &s)
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "symbolsfindfilter.h"
#include "cppmodelmanager.h"
#include "cpptoolsconstants.h"
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/icore.h>
#include <find/textfindconstants.h>
#include <qtconcurrent/runextensions.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <projectexplorer/project.h>
#include <QtCore/QSet>
#include <QtCore/QRegExp>
#include <QtGui/QGridLayout>
#include <QtGui/QLabel>
#include <QtGui/QButtonGroup>
using namespace CppTools;
using namespace CppTools::Internal;
namespace {
const char * const SETTINGS_GROUP = "CppSymbols";
const char * const SETTINGS_SYMBOLTYPES = "SymbolsToSearchFor";
const char * const SETTINGS_SEARCHSCOPE = "SearchScope";
void runSearch(QFutureInterface<Find::SearchResultItem> &future,
QString txt, Find::FindFlags findFlags, CPlusPlus::Snapshot snapshot,
SearchSymbols *search, QSet<QString> fileNames)
{
future.setProgressRange(0, snapshot.size());
future.setProgressValue(0);
int progress = 0;
CPlusPlus::Snapshot::const_iterator it = snapshot.begin();
QString findString = (findFlags & Find::FindRegularExpression ? txt : QRegExp::escape(txt));
if (findFlags & Find::FindWholeWords)
findString = QString::fromLatin1("\\b%1\\b").arg(findString);
QRegExp matcher(findString, (findFlags & Find::FindCaseSensitively ? Qt::CaseSensitive : Qt::CaseInsensitive));
while (it != snapshot.end() && !future.isCanceled()) {
if (fileNames.isEmpty() || fileNames.contains(it.value()->fileName())) {
QVector<Find::SearchResultItem> resultItems;
QList<ModelItemInfo> modelInfos = (*search)(it.value());
foreach (const ModelItemInfo &info, modelInfos) {
int index = matcher.indexIn(info.symbolName);
if (index != -1) {
QStringList path = info.fullyQualifiedName.mid(0, info.fullyQualifiedName.size() - 1);
Find::SearchResultItem item;
item.path = path;
item.text = info.symbolName;
item.textMarkPos = -1;
item.textMarkLength = 0;
item.icon = info.icon;
item.lineNumber = -1;
item.userData = qVariantFromValue(info);
resultItems << item;
}
}
if (!resultItems.isEmpty())
future.reportResults(resultItems);
}
++it;
++progress;
future.setProgressValue(progress);
}
}
} //namespace
SymbolsFindFilter::SymbolsFindFilter(CppModelManager *manager)
: m_manager(manager),
m_isRunning(false),
m_enabled(true),
m_symbolsToSearch(SearchSymbols::AllTypes),
m_scope(SearchProjectsOnly)
{
// for disabling while parser is running
connect(Core::ICore::instance()->progressManager(), SIGNAL(taskStarted(QString)),
this, SLOT(onTaskStarted(QString)));
connect(Core::ICore::instance()->progressManager(), SIGNAL(allTasksFinished(QString)),
this, SLOT(onAllTasksFinished(QString)));
connect(&m_watcher, SIGNAL(finished()),
this, SLOT(finish()));
connect(&m_watcher, SIGNAL(resultsReadyAt(int,int)),
this, SLOT(addResults(int, int)));
}
QString SymbolsFindFilter::id() const
{
return QLatin1String("CppSymbols");
}
QString SymbolsFindFilter::displayName() const
{
return tr("C++ Symbols");
}
bool SymbolsFindFilter::isEnabled() const
{
return !m_isRunning && m_enabled;
}
Find::FindFlags SymbolsFindFilter::supportedFindFlags() const
{
return Find::FindCaseSensitively | Find::FindRegularExpression | Find::FindWholeWords;
}
void SymbolsFindFilter::findAll(const QString &txt, Find::FindFlags findFlags)
{
m_isRunning = true;
emit changed();
Find::SearchResultWindow *window = Find::SearchResultWindow::instance();
Find::SearchResult *result = window->startNewSearch();
connect(result, SIGNAL(activated(Find::SearchResultItem)), this, SLOT(openEditor(Find::SearchResultItem)));
window->popup(true);
m_search.setSymbolsToSearchFor(m_symbolsToSearch);
m_search.setSeparateScope(true);
QSet<QString> projectFileNames;
if (m_scope == SymbolsFindFilter::SearchProjectsOnly) {
foreach (ProjectExplorer::Project *project,
ProjectExplorer::ProjectExplorerPlugin::instance()->session()->projects()) {
projectFileNames += project->files(ProjectExplorer::Project::AllFiles).toSet();
}
}
m_watcher.setFuture(QtConcurrent::run<Find::SearchResultItem, QString,
Find::FindFlags, CPlusPlus::Snapshot,
SearchSymbols *, QSet<QString> >(runSearch, txt, findFlags, m_manager->snapshot(),
&m_search, projectFileNames));
Core::ICore::instance()->progressManager()->addTask(m_watcher.future(),
tr("Searching"),
Find::Constants::TASK_SEARCH);
}
void SymbolsFindFilter::addResults(int begin, int end)
{
Find::SearchResultWindow *window = Find::SearchResultWindow::instance();
QList<Find::SearchResultItem> items;
for (int i = begin; i < end; ++i)
items << m_watcher.resultAt(i);
window->addResults(items, Find::SearchResultWindow::AddSorted);
}
void SymbolsFindFilter::finish()
{
Find::SearchResultWindow *window = Find::SearchResultWindow::instance();
window->finishSearch();
m_isRunning = false;
emit changed();
}
void SymbolsFindFilter::openEditor(const Find::SearchResultItem &item)
{
if (!item.userData.canConvert<ModelItemInfo>())
return;
ModelItemInfo info = item.userData.value<ModelItemInfo>();
TextEditor::BaseTextEditor::openEditorAt(info.fileName,
info.line,
info.column);
}
QWidget *SymbolsFindFilter::createConfigWidget()
{
return new SymbolsFindFilterConfigWidget(this);
}
void SymbolsFindFilter::writeSettings(QSettings *settings)
{
settings->beginGroup(QLatin1String(SETTINGS_GROUP));
settings->setValue(SETTINGS_SYMBOLTYPES, (int)m_symbolsToSearch);
settings->setValue(SETTINGS_SEARCHSCOPE, (int)m_scope);
settings->endGroup();
}