ClangCodeModel: Remove the old matchesFor() implementation

Rename some filter classes to conform to the names for cpp filters.
Remove some unneeded intermediate classes now.

Change-Id: Ib4fa295fa60da61c42c792556751d954ddb936e7
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Jarek Kobus
2023-04-25 00:17:52 +02:00
parent 74f6b96315
commit 7d87233c9c
5 changed files with 30 additions and 334 deletions

View File

@@ -6,21 +6,17 @@
#include "clangdclient.h"
#include "clangmodelmanagersupport.h"
#include <coreplugin/editormanager/editormanager.h>
#include <cppeditor/cppeditorconstants.h>
#include <cppeditor/cppeditortr.h>
#include <cppeditor/cpplocatorfilter.h>
#include <cppeditor/cppmodelmanager.h>
#include <cppeditor/indexitem.h>
#include <extensionsystem/pluginmanager.h>
#include <languageclient/currentdocumentsymbolsrequest.h>
#include <languageclient/languageclientmanager.h>
#include <languageclient/languageclientutils.h>
#include <languageclient/locatorfilter.h>
#include <projectexplorer/projectmanager.h>
#include <utils/algorithm.h>
#include <utils/async.h>
#include <utils/link.h>
#include <QHash>
@@ -31,124 +27,11 @@ using namespace ProjectExplorer;
using namespace TextEditor;
using namespace Utils;
namespace ClangCodeModel {
namespace Internal {
namespace ClangCodeModel::Internal {
const int MaxResultCount = 10000;
// TODO: Remove this class, it's used only internally by ClangGlobalSymbolFilter
class CppLocatorFilter : public CppEditor::CppLocatorFilter
{
public:
CppLocatorFilter()
{
setId({});
setDisplayName({});
setDescription({});
setDefaultShortcutString({});
setEnabled(false);
setHidden(true);
}
};
// TODO: Remove this class, it's used only internally by ClangGlobalSymbolFilter
class LspWorkspaceFilter : public WorkspaceLocatorFilter
{
public:
LspWorkspaceFilter()
{
setId({});
setDisplayName({});
setDescription({});
setDefaultShortcutString({});
setEnabled(false);
setHidden(true);
setMaxResultCount(MaxResultCount);
}
void prepareSearch(const QString &entry) override
{
prepareSearchForClients(entry, ClangModelManagerSupport::clientsForOpenProjects());
}
};
// TODO: Remove this class, it's used only internally by ClangClassesFilter
class CppClassesFilter : public CppEditor::CppClassesFilter
{
public:
CppClassesFilter()
{
setId({});
setDisplayName({});
setDescription({});
setDefaultShortcutString({});
setEnabled(false);
setHidden(true);
}
};
// TODO: Remove this class, it's used only internally by ClangClassesFilter
class LspClassesFilter : public WorkspaceClassLocatorFilter
{
public:
LspClassesFilter() {
setId({});
setDisplayName({});
setDescription({});
setDefaultShortcutString({});
setEnabled(false);
setHidden(true);
setMaxResultCount(MaxResultCount);
}
void prepareSearch(const QString &entry) override
{
prepareSearchForClients(entry, ClangModelManagerSupport::clientsForOpenProjects());
}
};
// TODO: Remove this class, it's used only internally by ClangFunctionsFilter
class CppFunctionsFilter : public CppEditor::CppFunctionsFilter
{
public:
CppFunctionsFilter()
{
setId({});
setDisplayName({});
setDescription({});
setDefaultShortcutString({});
setEnabled(false);
setHidden(true);
}
};
// TODO: Remove this class, it's used only internally by ClangFunctionsFilter
class LspFunctionsFilter : public WorkspaceMethodLocatorFilter
{
public:
LspFunctionsFilter()
{
setId({});
setDisplayName({});
setDescription({});
setDefaultShortcutString({});
setEnabled(false);
setHidden(true);
setMaxResultCount(MaxResultCount);
}
void prepareSearch(const QString &entry) override
{
prepareSearchForClients(entry, ClangModelManagerSupport::clientsForOpenProjects());
}
};
ClangGlobalSymbolFilter::ClangGlobalSymbolFilter()
: ClangGlobalSymbolFilter(new CppLocatorFilter, new LspWorkspaceFilter)
{
}
ClangGlobalSymbolFilter::ClangGlobalSymbolFilter(ILocatorFilter *cppFilter,
ILocatorFilter *lspFilter)
: m_cppFilter(cppFilter), m_lspFilter(lspFilter)
ClangdAllSymbolsFilter::ClangdAllSymbolsFilter()
{
setId(CppEditor::Constants::LOCATOR_FILTER_ID);
setDisplayName(::CppEditor::Tr::tr(CppEditor::Constants::LOCATOR_FILTER_DISPLAY_NAME));
@@ -157,33 +40,14 @@ ClangGlobalSymbolFilter::ClangGlobalSymbolFilter(ILocatorFilter *cppFilter,
setDefaultIncludedByDefault(false);
}
ClangGlobalSymbolFilter::~ClangGlobalSymbolFilter()
{
delete m_cppFilter;
delete m_lspFilter;
}
LocatorMatcherTasks ClangGlobalSymbolFilter::matchers()
LocatorMatcherTasks ClangdAllSymbolsFilter::matchers()
{
return CppEditor::cppMatchers(MatcherType::AllSymbols)
+ LanguageClient::languageClientMatchers(MatcherType::AllSymbols,
ClangModelManagerSupport::clientsForOpenProjects(), MaxResultCount);
}
void ClangGlobalSymbolFilter::prepareSearch(const QString &entry)
{
m_cppFilter->prepareSearch(entry);
m_lspFilter->prepareSearch(entry);
}
QList<LocatorFilterEntry> ClangGlobalSymbolFilter::matchesFor(
QFutureInterface<LocatorFilterEntry> &future, const QString &entry)
{
return m_cppFilter->matchesFor(future, entry) + m_lspFilter->matchesFor(future, entry);
}
ClangClassesFilter::ClangClassesFilter()
: ClangGlobalSymbolFilter(new CppClassesFilter, new LspClassesFilter)
ClangdClassesFilter::ClangdClassesFilter()
{
setId(CppEditor::Constants::CLASSES_FILTER_ID);
setDisplayName(::CppEditor::Tr::tr(CppEditor::Constants::CLASSES_FILTER_DISPLAY_NAME));
@@ -192,15 +56,14 @@ ClangClassesFilter::ClangClassesFilter()
setDefaultIncludedByDefault(false);
}
LocatorMatcherTasks ClangClassesFilter::matchers()
LocatorMatcherTasks ClangdClassesFilter::matchers()
{
return CppEditor::cppMatchers(MatcherType::Classes)
+ LanguageClient::languageClientMatchers(MatcherType::Classes,
ClangModelManagerSupport::clientsForOpenProjects(), MaxResultCount);
}
ClangFunctionsFilter::ClangFunctionsFilter()
: ClangGlobalSymbolFilter(new CppFunctionsFilter, new LspFunctionsFilter)
ClangdFunctionsFilter::ClangdFunctionsFilter()
{
setId(CppEditor::Constants::FUNCTIONS_FILTER_ID);
setDisplayName(::CppEditor::Tr::tr(CppEditor::Constants::FUNCTIONS_FILTER_DISPLAY_NAME));
@@ -209,129 +72,14 @@ ClangFunctionsFilter::ClangFunctionsFilter()
setDefaultIncludedByDefault(false);
}
LocatorMatcherTasks ClangFunctionsFilter::matchers()
LocatorMatcherTasks ClangdFunctionsFilter::matchers()
{
return CppEditor::cppMatchers(MatcherType::Functions)
+ LanguageClient::languageClientMatchers(MatcherType::Functions,
ClangModelManagerSupport::clientsForOpenProjects(), MaxResultCount);
}
// TODO: Remove this class, it's used only internally by ClangdCurrentDocumentFilter
class LspCurrentDocumentFilter : public DocumentLocatorFilter
{
public:
LspCurrentDocumentFilter()
{
setId({});
setDisplayName({});
setDescription({});
setDefaultShortcutString({});
setEnabled(false);
setHidden(true);
forceUse();
}
private:
void prepareSearch(const QString &entry) override
{
DocumentLocatorFilter::prepareSearch(entry);
m_content = TextEditor::TextDocument::currentTextDocument()->plainText();
}
// Filter out declarations for which a definition is also present.
QList<LocatorFilterEntry> matchesFor(QFutureInterface<LocatorFilterEntry> &future,
const QString &entry) override
{
struct Entry
{
LocatorFilterEntry entry;
DocumentSymbol symbol;
};
QList<Entry> docEntries;
const auto docSymbolModifier = [&docEntries](LocatorFilterEntry &entry,
const DocumentSymbol &info,
const LocatorFilterEntry &parent) {
entry.displayName = ClangdClient::displayNameFromDocumentSymbol(
static_cast<SymbolKind>(info.kind()), info.name(),
info.detail().value_or(QString()));
entry.extraInfo = parent.extraInfo;
if (!entry.extraInfo.isEmpty())
entry.extraInfo.append("::");
entry.extraInfo.append(parent.displayName);
// TODO: Can we extend clangd to send visibility information?
docEntries.append({entry, info});
};
const QList<LocatorFilterEntry> allMatches = matchesForImpl(future, entry,
docSymbolModifier);
if (docEntries.isEmpty())
return allMatches; // SymbolInformation case
QTC_CHECK(docEntries.size() == allMatches.size());
QHash<QString, QList<Entry>> possibleDuplicates;
for (const Entry &e : std::as_const(docEntries))
possibleDuplicates[e.entry.displayName + e.entry.extraInfo] << e;
const QTextDocument doc(m_content);
for (auto it = possibleDuplicates.cbegin(); it != possibleDuplicates.cend(); ++it) {
const QList<Entry> &duplicates = it.value();
if (duplicates.size() == 1)
continue;
QList<Entry> declarations;
QList<Entry> definitions;
for (const Entry &candidate : duplicates) {
const DocumentSymbol symbol = candidate.symbol;
const SymbolKind kind = static_cast<SymbolKind>(symbol.kind());
if (kind != SymbolKind::Class && kind != SymbolKind::Function)
break;
const Range range = symbol.range();
const Range selectionRange = symbol.selectionRange();
if (kind == SymbolKind::Class) {
if (range.end() == selectionRange.end())
declarations << candidate;
else
definitions << candidate;
continue;
}
const int startPos = selectionRange.end().toPositionInDocument(&doc);
const int endPos = range.end().toPositionInDocument(&doc);
const QString functionBody = m_content.mid(startPos, endPos - startPos);
// Hacky, but I don't see anything better.
if (functionBody.contains('{') && functionBody.contains('}'))
definitions << candidate;
else
declarations << candidate;
}
if (definitions.size() == 1
&& declarations.size() + definitions.size() == duplicates.size()) {
for (const Entry &decl : std::as_const(declarations)) {
Utils::erase(docEntries, [&decl](const Entry &e) {
return e.symbol == decl.symbol;
});
}
}
}
return Utils::transform(docEntries, [](const Entry &entry) { return entry.entry; });
}
QString m_content;
};
class ClangdCurrentDocumentFilter::Private
{
public:
~Private() { delete cppFilter; }
ILocatorFilter * const cppFilter
= CppEditor::CppModelManager::createAuxiliaryCurrentDocumentFilter();
LspCurrentDocumentFilter lspFilter;
ILocatorFilter *activeFilter = nullptr;
};
ClangdCurrentDocumentFilter::ClangdCurrentDocumentFilter() : d(new Private)
ClangdCurrentDocumentFilter::ClangdCurrentDocumentFilter()
{
setId(CppEditor::Constants::CURRENT_DOCUMENT_FILTER_ID);
setDisplayName(::CppEditor::Tr::tr(CppEditor::Constants::CURRENT_DOCUMENT_FILTER_DISPLAY_NAME));
@@ -344,8 +92,6 @@ ClangdCurrentDocumentFilter::ClangdCurrentDocumentFilter() : d(new Private)
this, [this](const IEditor *editor) { setEnabled(editor); });
}
ClangdCurrentDocumentFilter::~ClangdCurrentDocumentFilter() { delete d; }
static void filterCurrentResults(QPromise<void> &promise, const LocatorStorage &storage,
const CurrentDocumentSymbolsData &currentSymbolsData,
const QString &contents)
@@ -468,25 +214,4 @@ LocatorMatcherTasks ClangdCurrentDocumentFilter::matchers()
return CppEditor::cppMatchers(MatcherType::CurrentDocumentSymbols);
}
void ClangdCurrentDocumentFilter::prepareSearch(const QString &entry)
{
const auto doc = TextEditor::TextDocument::currentTextDocument();
QTC_ASSERT(doc, return);
if (const ClangdClient * const client = ClangModelManagerSupport::clientForFile
(doc->filePath()); client && client->reachable()) {
d->activeFilter = &d->lspFilter;
} else {
d->activeFilter = d->cppFilter;
}
d->activeFilter->prepareSearch(entry);
}
QList<LocatorFilterEntry> ClangdCurrentDocumentFilter::matchesFor(
QFutureInterface<LocatorFilterEntry> &future, const QString &entry)
{
QTC_ASSERT(d->activeFilter, return {});
return d->activeFilter->matchesFor(future, entry);
}
} // namespace Internal
} // namespace ClangCodeModel
} // namespace ClangCodeModel::Internal

View File

@@ -5,43 +5,30 @@
#include <coreplugin/locator/ilocatorfilter.h>
namespace LanguageClient { class WorkspaceLocatorFilter; }
namespace ClangCodeModel::Internal {
namespace ClangCodeModel {
namespace Internal {
class ClangGlobalSymbolFilter : public Core::ILocatorFilter
class ClangdAllSymbolsFilter : public Core::ILocatorFilter
{
public:
ClangGlobalSymbolFilter();
ClangGlobalSymbolFilter(Core::ILocatorFilter *cppFilter,
Core::ILocatorFilter *lspFilter);
~ClangGlobalSymbolFilter() override;
private:
Core::LocatorMatcherTasks matchers() override;
void prepareSearch(const QString &entry) override;
QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future,
const QString &entry) override;
Core::ILocatorFilter * const m_cppFilter;
Core::ILocatorFilter * const m_lspFilter;
};
// TODO: Don't derive, flatten the hierarchy
class ClangClassesFilter : public ClangGlobalSymbolFilter
{
public:
ClangClassesFilter();
ClangdAllSymbolsFilter();
private:
Core::LocatorMatcherTasks matchers() final;
};
// TODO: Don't derive, flatten the hierarchy
class ClangFunctionsFilter : public ClangGlobalSymbolFilter
class ClangdClassesFilter : public Core::ILocatorFilter
{
public:
ClangFunctionsFilter();
ClangdClassesFilter();
private:
Core::LocatorMatcherTasks matchers() final;
};
class ClangdFunctionsFilter : public Core::ILocatorFilter
{
public:
ClangdFunctionsFilter();
private:
Core::LocatorMatcherTasks matchers() final;
@@ -51,16 +38,9 @@ class ClangdCurrentDocumentFilter : public Core::ILocatorFilter
{
public:
ClangdCurrentDocumentFilter();
~ClangdCurrentDocumentFilter() override;
private:
Core::LocatorMatcherTasks matchers() final;
void prepareSearch(const QString &entry) override;
QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future,
const QString &entry) override;
class Private;
Private * const d;
};
} // namespace Internal
} // namespace ClangCodeModel
} // namespace ClangCodeModel::Internal

View File

@@ -202,9 +202,9 @@ ClangModelManagerSupport::ClangModelManagerSupport()
setupClangdConfigFile();
checkSystemForClangdSuitability();
cppModelManager()->setCurrentDocumentFilter(std::make_unique<ClangdCurrentDocumentFilter>());
cppModelManager()->setLocatorFilter(std::make_unique<ClangGlobalSymbolFilter>());
cppModelManager()->setClassesFilter(std::make_unique<ClangClassesFilter>());
cppModelManager()->setFunctionsFilter(std::make_unique<ClangFunctionsFilter>());
cppModelManager()->setLocatorFilter(std::make_unique<ClangdAllSymbolsFilter>());
cppModelManager()->setClassesFilter(std::make_unique<ClangdClassesFilter>());
cppModelManager()->setFunctionsFilter(std::make_unique<ClangdFunctionsFilter>());
// Setup matchers
LocatorMatcher::addMatcherCreator(MatcherType::AllSymbols, [] {
return LanguageClient::languageClientMatchers(

View File

@@ -2020,13 +2020,6 @@ void CppModelManager::switchDeclDef(const CursorInEditor &data,
instance()->modelManagerSupport(backend)->switchDeclDef(data, processLinkCallback);
}
ILocatorFilter *CppModelManager::createAuxiliaryCurrentDocumentFilter()
{
const auto filter = new Internal::CppCurrentDocumentFilter;
filter->makeAuxiliary();
return filter;
}
BaseEditorDocumentProcessor *CppModelManager::createEditorDocumentProcessor(
TextEditor::TextDocument *baseTextDocument) const
{

View File

@@ -196,8 +196,6 @@ public:
const CPlusPlus::LookupContext &context,
const Utils::LinkHandler &callback);
static Core::ILocatorFilter *createAuxiliaryCurrentDocumentFilter();
CppIndexingSupport *indexingSupport();
Utils::FilePaths projectFiles();