Clang: Locator: Add filters for classes, includes, functions

Filters are based on symbol query where they are
not implemented yet.

Change-Id: Id826beaf6bb47a81363c36780d7254a85a9a3cc9
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Ivan Donchevskii
2017-11-27 09:45:13 +01:00
parent 88c3e27722
commit 2daa5c7280
20 changed files with 133 additions and 29 deletions

View File

@@ -22,7 +22,8 @@ HEADERS += \
$$PWD/symbol.h \ $$PWD/symbol.h \
$$PWD/class.h \ $$PWD/class.h \
$$PWD/enum.h \ $$PWD/enum.h \
$$PWD/function.h $$PWD/function.h \
$$PWD/include.h
SOURCES += \ SOURCES += \
$$PWD/clangqueryexamplehighlighter.cpp \ $$PWD/clangqueryexamplehighlighter.cpp \

View File

@@ -59,6 +59,7 @@ QtcPlugin {
"function.h", "function.h",
"functionsfilter.cpp", "functionsfilter.cpp",
"functionsfilter.h", "functionsfilter.h",
"include.h",
"includesfilter.cpp", "includesfilter.cpp",
"includesfilter.h", "includesfilter.h",
"locatorfilter.cpp", "locatorfilter.cpp",

View File

@@ -173,9 +173,9 @@ void ClangRefactoringPlugin::initializeFilters()
CppTools::CppModelManager *modelManager = CppTools::CppModelManager::instance(); CppTools::CppModelManager *modelManager = CppTools::CppModelManager::instance();
modelManager->setLocatorFilter(std::make_unique<QtcreatorLocatorFilter>(d->symbolQuery)); modelManager->setLocatorFilter(std::make_unique<QtcreatorLocatorFilter>(d->symbolQuery));
modelManager->setClassesFilter(std::make_unique<QtcreatorClassesFilter>()); modelManager->setClassesFilter(std::make_unique<QtcreatorClassesFilter>(d->symbolQuery));
modelManager->setIncludesFilter(std::make_unique<QtcreatorIncludesFilter>()); modelManager->setIncludesFilter(std::make_unique<QtcreatorIncludesFilter>(d->symbolQuery));
modelManager->setFunctionsFilter(std::make_unique<QtcreatorFunctionsFilter>()); modelManager->setFunctionsFilter(std::make_unique<QtcreatorFunctionsFilter>(d->symbolQuery));
modelManager->setSymbolsFindFilter(std::make_unique<QtcreatorSymbolsFindFilter>()); modelManager->setSymbolsFindFilter(std::make_unique<QtcreatorSymbolsFindFilter>());
} }

View File

@@ -27,9 +27,12 @@
#include <cpptools/cpptoolsconstants.h> #include <cpptools/cpptoolsconstants.h>
#include <utils/algorithm.h>
namespace ClangRefactoring { namespace ClangRefactoring {
ClassesFilter::ClassesFilter() ClassesFilter::ClassesFilter(SymbolQueryInterface &symbolQuery)
: m_symbolQuery(symbolQuery)
{ {
setId(CppTools::Constants::CLASSES_FILTER_ID); setId(CppTools::Constants::CLASSES_FILTER_ID);
setDisplayName(CppTools::Constants::CLASSES_FILTER_DISPLAY_NAME); setDisplayName(CppTools::Constants::CLASSES_FILTER_DISPLAY_NAME);
@@ -38,19 +41,26 @@ ClassesFilter::ClassesFilter()
} }
QList<Core::LocatorFilterEntry> ClassesFilter::matchesFor( QList<Core::LocatorFilterEntry> ClassesFilter::matchesFor(
QFutureInterface<Core::LocatorFilterEntry> &, const QString &) QFutureInterface<Core::LocatorFilterEntry> &, const QString &entry)
{ {
return QList<Core::LocatorFilterEntry>(); using EntryList = QList<Core::LocatorFilterEntry>;
const SymbolString entryString(entry);
const Classes classes = m_symbolQuery.symbolsContaining(SymbolType::Class, entryString);
return Utils::transform<EntryList>(classes, [this](const Class &classInfo) {
Core::LocatorFilterEntry entry{this,
classInfo.name.toQString(),
qVariantFromValue(classInfo)};
entry.extraInfo = classInfo.path.path().toQString();
return entry;
});
} }
void ClassesFilter::accept(Core::LocatorFilterEntry, QString *, int *, int *) const void ClassesFilter::accept(Core::LocatorFilterEntry, QString *, int *, int *) const
{ {
} }
void ClassesFilter::refresh(QFutureInterface<void> &) void ClassesFilter::refresh(QFutureInterface<void> &)
{ {
} }
} // namespace ClangRefactoring } // namespace ClangRefactoring

View File

@@ -25,6 +25,8 @@
#pragma once #pragma once
#include "symbolqueryinterface.h"
#include <coreplugin/locator/ilocatorfilter.h> #include <coreplugin/locator/ilocatorfilter.h>
namespace ClangRefactoring { namespace ClangRefactoring {
@@ -33,13 +35,15 @@ class ClassesFilter : public Core::ILocatorFilter
{ {
Q_OBJECT Q_OBJECT
public: public:
ClassesFilter(); ClassesFilter(SymbolQueryInterface &symbolQuery);
QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future, QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future,
const QString &entry) override; const QString &entry) override;
void accept(Core::LocatorFilterEntry selection, void accept(Core::LocatorFilterEntry selection,
QString *newText, int *selectionStart, int *selectionLength) const override; QString *newText, int *selectionStart, int *selectionLength) const override;
void refresh(QFutureInterface<void> &future) override; void refresh(QFutureInterface<void> &future) override;
private:
SymbolQueryInterface &m_symbolQuery;
}; };
} // namespace ClangRefactoring } // namespace ClangRefactoring

View File

@@ -27,9 +27,12 @@
#include <cpptools/cpptoolsconstants.h> #include <cpptools/cpptoolsconstants.h>
#include <utils/algorithm.h>
namespace ClangRefactoring { namespace ClangRefactoring {
FunctionsFilter::FunctionsFilter() FunctionsFilter::FunctionsFilter(SymbolQueryInterface &symbolQuery)
: m_symbolQuery(symbolQuery)
{ {
setId(CppTools::Constants::FUNCTIONS_FILTER_ID); setId(CppTools::Constants::FUNCTIONS_FILTER_ID);
setDisplayName(CppTools::Constants::FUNCTIONS_FILTER_DISPLAY_NAME); setDisplayName(CppTools::Constants::FUNCTIONS_FILTER_DISPLAY_NAME);
@@ -38,19 +41,26 @@ FunctionsFilter::FunctionsFilter()
} }
QList<Core::LocatorFilterEntry> FunctionsFilter::matchesFor( QList<Core::LocatorFilterEntry> FunctionsFilter::matchesFor(
QFutureInterface<Core::LocatorFilterEntry> &, const QString &) QFutureInterface<Core::LocatorFilterEntry> &, const QString &entry)
{ {
return QList<Core::LocatorFilterEntry>(); using EntryList = QList<Core::LocatorFilterEntry>;
const SymbolString entryString(entry);
const Functions functions = m_symbolQuery.functionsContaining(entryString);
return Utils::transform<EntryList>(functions, [this](const Function &function) {
const auto data = qVariantFromValue(static_cast<const Symbol &>(function));
Core::LocatorFilterEntry entry{this, function.name.toQString(), data};
entry.extraInfo = function.path.path().toQString();
return entry;
});
} }
void FunctionsFilter::accept(Core::LocatorFilterEntry, QString *, int *, int *) const void FunctionsFilter::accept(Core::LocatorFilterEntry, QString *, int *, int *) const
{ {
} }
void FunctionsFilter::refresh(QFutureInterface<void> &) void FunctionsFilter::refresh(QFutureInterface<void> &)
{ {
} }
} // namespace ClangRefactoring } // namespace ClangRefactoring

View File

@@ -25,6 +25,8 @@
#pragma once #pragma once
#include "symbolqueryinterface.h"
#include <coreplugin/locator/ilocatorfilter.h> #include <coreplugin/locator/ilocatorfilter.h>
namespace ClangRefactoring { namespace ClangRefactoring {
@@ -33,13 +35,15 @@ class FunctionsFilter : public Core::ILocatorFilter
{ {
Q_OBJECT Q_OBJECT
public: public:
FunctionsFilter(); FunctionsFilter(SymbolQueryInterface &symbolQuery);
QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future, QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future,
const QString &entry) override; const QString &entry) override;
void accept(Core::LocatorFilterEntry selection, void accept(Core::LocatorFilterEntry selection,
QString *newText, int *selectionStart, int *selectionLength) const override; QString *newText, int *selectionStart, int *selectionLength) const override;
void refresh(QFutureInterface<void> &future) override; void refresh(QFutureInterface<void> &future) override;
private:
SymbolQueryInterface &m_symbolQuery;
}; };
} // namespace ClangRefactoring } // namespace ClangRefactoring

View File

@@ -0,0 +1,33 @@
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "symbol.h"
namespace ClangRefactoring {
using Include = Symbol;
using Includes = std::vector<Include>;
} // namespace ClangRefactoring

View File

@@ -27,9 +27,12 @@
#include <cpptools/cpptoolsconstants.h> #include <cpptools/cpptoolsconstants.h>
#include <utils/algorithm.h>
namespace ClangRefactoring { namespace ClangRefactoring {
IncludesFilter::IncludesFilter() IncludesFilter::IncludesFilter(SymbolQueryInterface &symbolQuery)
: m_symbolQuery(symbolQuery)
{ {
setId(CppTools::Constants::INCLUDES_FILTER_ID); setId(CppTools::Constants::INCLUDES_FILTER_ID);
setDisplayName(CppTools::Constants::INCLUDES_FILTER_DISPLAY_NAME); setDisplayName(CppTools::Constants::INCLUDES_FILTER_DISPLAY_NAME);
@@ -39,19 +42,26 @@ IncludesFilter::IncludesFilter()
} }
QList<Core::LocatorFilterEntry> IncludesFilter::matchesFor( QList<Core::LocatorFilterEntry> IncludesFilter::matchesFor(
QFutureInterface<Core::LocatorFilterEntry> &, const QString &) QFutureInterface<Core::LocatorFilterEntry> &, const QString &entry)
{ {
return QList<Core::LocatorFilterEntry>(); using EntryList = QList<Core::LocatorFilterEntry>;
const SymbolString entryString(entry);
const Includes includes = m_symbolQuery.symbolsContaining(SymbolType::Include, entryString);
return Utils::transform<EntryList>(includes, [this](const Include &includeInfo) {
Core::LocatorFilterEntry entry{this,
includeInfo.name.toQString(),
qVariantFromValue(includeInfo)};
entry.extraInfo = includeInfo.path.path().toQString();
return entry;
});
} }
void IncludesFilter::accept(Core::LocatorFilterEntry, QString *, int *, int *) const void IncludesFilter::accept(Core::LocatorFilterEntry, QString *, int *, int *) const
{ {
} }
void IncludesFilter::refresh(QFutureInterface<void> &) void IncludesFilter::refresh(QFutureInterface<void> &)
{ {
} }
} // namespace ClangRefactoring } // namespace ClangRefactoring

View File

@@ -25,6 +25,8 @@
#pragma once #pragma once
#include "symbolqueryinterface.h"
#include <coreplugin/locator/ilocatorfilter.h> #include <coreplugin/locator/ilocatorfilter.h>
namespace ClangRefactoring { namespace ClangRefactoring {
@@ -33,13 +35,15 @@ class IncludesFilter : public Core::ILocatorFilter
{ {
Q_OBJECT Q_OBJECT
public: public:
IncludesFilter(); IncludesFilter(SymbolQueryInterface &symbolQuery);
QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future, QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future,
const QString &entry) override; const QString &entry) override;
void accept(Core::LocatorFilterEntry selection, void accept(Core::LocatorFilterEntry selection,
QString *newText, int *selectionStart, int *selectionLength) const override; QString *newText, int *selectionStart, int *selectionLength) const override;
void refresh(QFutureInterface<void> &future) override; void refresh(QFutureInterface<void> &future) override;
private:
SymbolQueryInterface &m_symbolQuery;
}; };
} // namespace ClangRefactoring } // namespace ClangRefactoring

View File

@@ -25,11 +25,16 @@
#include "qtcreatorclassesfilter.h" #include "qtcreatorclassesfilter.h"
#include <coreplugin/editormanager/editormanager.h>
namespace ClangRefactoring { namespace ClangRefactoring {
void QtcreatorClassesFilter::accept(Core::LocatorFilterEntry, void QtcreatorClassesFilter::accept(Core::LocatorFilterEntry selection,
QString *, int *, int *) const QString *, int *, int *) const
{ {
auto info = qvariant_cast<Symbol>(selection.internalData);
Core::EditorManager::openEditorAt(info.path.path().toQString(), info.lineColumn.line,
info.lineColumn.column);
} }
} // namespace ClangRefactoring } // namespace ClangRefactoring

View File

@@ -33,6 +33,7 @@ class QtcreatorClassesFilter : public ClassesFilter
{ {
Q_OBJECT Q_OBJECT
public: public:
QtcreatorClassesFilter(SymbolQueryInterface &symbolQuery) : ClassesFilter(symbolQuery) {}
void accept(Core::LocatorFilterEntry selection, void accept(Core::LocatorFilterEntry selection,
QString *newText, int *selectionStart, int *selectionLength) const override; QString *newText, int *selectionStart, int *selectionLength) const override;
}; };

View File

@@ -25,11 +25,16 @@
#include "qtcreatorfunctionsfilter.h" #include "qtcreatorfunctionsfilter.h"
#include <coreplugin/editormanager/editormanager.h>
namespace ClangRefactoring { namespace ClangRefactoring {
void QtcreatorFunctionsFilter::accept(Core::LocatorFilterEntry, void QtcreatorFunctionsFilter::accept(Core::LocatorFilterEntry selection,
QString *, int *, int *) const QString *, int *, int *) const
{ {
auto info = qvariant_cast<Symbol>(selection.internalData);
Core::EditorManager::openEditorAt(info.path.path().toQString(), info.lineColumn.line,
info.lineColumn.column);
} }
} // namespace ClangRefactoring } // namespace ClangRefactoring

View File

@@ -33,6 +33,7 @@ class QtcreatorFunctionsFilter : public FunctionsFilter
{ {
Q_OBJECT Q_OBJECT
public: public:
QtcreatorFunctionsFilter(SymbolQueryInterface &symbolQuery) : FunctionsFilter(symbolQuery) {}
void accept(Core::LocatorFilterEntry selection, void accept(Core::LocatorFilterEntry selection,
QString *newText, int *selectionStart, int *selectionLength) const override; QString *newText, int *selectionStart, int *selectionLength) const override;
}; };

View File

@@ -25,11 +25,14 @@
#include "qtcreatorincludesfilter.h" #include "qtcreatorincludesfilter.h"
#include <coreplugin/editormanager/editormanager.h>
namespace ClangRefactoring { namespace ClangRefactoring {
void QtcreatorIncludesFilter::accept(Core::LocatorFilterEntry, void QtcreatorIncludesFilter::accept(Core::LocatorFilterEntry selection,
QString *, int *, int *) const QString *, int *, int *) const
{ {
Core::EditorManager::openEditorAt(selection.displayName, 1, 1);
} }
} // namespace ClangRefactoring } // namespace ClangRefactoring

View File

@@ -33,6 +33,7 @@ class QtcreatorIncludesFilter : public IncludesFilter
{ {
Q_OBJECT Q_OBJECT
public: public:
QtcreatorIncludesFilter(SymbolQueryInterface &symbolQuery) : IncludesFilter(symbolQuery) {}
void accept(Core::LocatorFilterEntry selection, void accept(Core::LocatorFilterEntry selection,
QString *newText, int *selectionStart, int *selectionLength) const override; QString *newText, int *selectionStart, int *selectionLength) const override;
}; };

View File

@@ -30,6 +30,7 @@
#include "class.h" #include "class.h"
#include "enum.h" #include "enum.h"
#include "function.h" #include "function.h"
#include "include.h"
#include "symbol.h" #include "symbol.h"
#include <cpptools/usages.h> #include <cpptools/usages.h>
@@ -39,7 +40,8 @@ namespace ClangRefactoring {
enum class SymbolType enum class SymbolType
{ {
Class = 0, Class = 0,
Enum = 1 Enum = 1,
Include = 2
}; };
class SymbolQueryInterface class SymbolQueryInterface

View File

@@ -25,6 +25,8 @@
#include "googletest.h" #include "googletest.h"
#include "mocksymbolquery.h"
#include <clangrefactoring/classesfilter.h> #include <clangrefactoring/classesfilter.h>
namespace { namespace {
@@ -32,7 +34,8 @@ namespace {
class ClassesFilter : public ::testing::Test class ClassesFilter : public ::testing::Test
{ {
protected: protected:
ClangRefactoring::ClassesFilter classesFilter; MockSymbolQuery mockSymbolQuery;
ClangRefactoring::ClassesFilter classesFilter {mockSymbolQuery};
}; };
TEST_F(ClassesFilter, MatchesFor) TEST_F(ClassesFilter, MatchesFor)

View File

@@ -25,6 +25,8 @@
#include "googletest.h" #include "googletest.h"
#include "mocksymbolquery.h"
#include <clangrefactoring/functionsfilter.h> #include <clangrefactoring/functionsfilter.h>
namespace { namespace {
@@ -32,7 +34,8 @@ namespace {
class FunctionsFilter : public ::testing::Test class FunctionsFilter : public ::testing::Test
{ {
protected: protected:
ClangRefactoring::FunctionsFilter functionsFilter; MockSymbolQuery mockSymbolQuery;
ClangRefactoring::FunctionsFilter functionsFilter {mockSymbolQuery};
}; };
TEST_F(FunctionsFilter, MatchesFor) TEST_F(FunctionsFilter, MatchesFor)

View File

@@ -25,6 +25,8 @@
#include "googletest.h" #include "googletest.h"
#include "mocksymbolquery.h"
#include <clangrefactoring/includesfilter.h> #include <clangrefactoring/includesfilter.h>
namespace { namespace {
@@ -32,7 +34,8 @@ namespace {
class IncludesFilter : public ::testing::Test class IncludesFilter : public ::testing::Test
{ {
protected: protected:
ClangRefactoring::IncludesFilter includesFilter; MockSymbolQuery mockSymbolQuery;
ClangRefactoring::IncludesFilter includesFilter {mockSymbolQuery};
}; };
TEST_F(IncludesFilter, MatchesFor) TEST_F(IncludesFilter, MatchesFor)