ClangCodeModel: Remove libclang fallback for "follow symbol"

... and "switch between declaration/definition".
It's either clangd or built-in code model now.
Use the opportunity to dissolve the pointless FollowSymbolInterface
class hierarchy, which introduced a confusing parallel inheritance
chain.

Change-Id: I792ad55656c5dd9f10c6b4db7c5c36cf7be45125
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-04-25 13:37:58 +02:00
parent 8e586dfa02
commit f2c267f328
23 changed files with 117 additions and 523 deletions

View File

@@ -35,7 +35,6 @@ add_qtc_plugin(ClangCodeModel
clangeditordocumentprocessor.cpp clangeditordocumentprocessor.h
clangfixitoperation.cpp clangfixitoperation.h
clangfixitoperationsextractor.cpp clangfixitoperationsextractor.h
clangfollowsymbol.cpp clangfollowsymbol.h
clangfunctionhintmodel.cpp clangfunctionhintmodel.h
clangdlocatorfilters.cpp clangdlocatorfilters.h
clanghighlightingresultreporter.cpp clanghighlightingresultreporter.h

View File

@@ -78,8 +78,6 @@ QtcPlugin {
"clangfixitoperation.h",
"clangfixitoperationsextractor.cpp",
"clangfixitoperationsextractor.h",
"clangfollowsymbol.cpp",
"clangfollowsymbol.h",
"clangfunctionhintmodel.cpp",
"clangfunctionhintmodel.h",
"clanghighlightingresultreporter.cpp",

View File

@@ -1,274 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2017 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.
**
****************************************************************************/
#include "clangfollowsymbol.h"
#include "clangdclient.h"
#include "clangeditordocumentprocessor.h"
#include "clangmodelmanagersupport.h"
#include <coreplugin/editormanager/editormanager.h>
#include <cppeditor/cppmodelmanager.h>
#include <cppeditor/cppfollowsymbolundercursor.h>
#include <texteditor/texteditor.h>
#include <clangsupport/tokeninfocontainer.h>
#include <utils/textutils.h>
#include <utils/algorithm.h>
#include <memory>
namespace ClangCodeModel {
namespace Internal {
// Returns invalid Mark if it is not found at (line, column)
static bool findMark(const QVector<ClangBackEnd::TokenInfoContainer> &marks,
int line,
int column,
ClangBackEnd::TokenInfoContainer &mark)
{
mark = Utils::findOrDefault(marks,
[line, column](const ClangBackEnd::TokenInfoContainer &curMark) {
if (curMark.line != line)
return false;
if (curMark.column == column)
return true;
if (curMark.column < column && curMark.column + curMark.length > column)
return true;
return false;
});
if (mark.isInvalid())
return false;
return true;
}
static int getMarkPos(QTextCursor cursor, const ClangBackEnd::TokenInfoContainer &mark)
{
cursor.setPosition(0);
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, mark.line - 1);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, mark.column - 1);
return cursor.position();
}
static bool isValidIncludePathToken(const ClangBackEnd::TokenInfoContainer &token)
{
if (!token.extraInfo.includeDirectivePath)
return false;
const Utf8String &tokenName = token.extraInfo.token;
return !tokenName.startsWith("include") && tokenName != Utf8String("<")
&& tokenName != Utf8String(">") && tokenName != Utf8String("#");
}
static int includePathStartIndex(const QVector<ClangBackEnd::TokenInfoContainer> &marks,
int currentIndex)
{
int startIndex = currentIndex - 1;
while (startIndex >= 0 && isValidIncludePathToken(marks[startIndex]))
--startIndex;
return startIndex + 1;
}
static int includePathEndIndex(const QVector<ClangBackEnd::TokenInfoContainer> &marks,
int currentIndex)
{
int endIndex = currentIndex + 1;
while (endIndex < marks.size() && isValidIncludePathToken(marks[endIndex]))
++endIndex;
return endIndex - 1;
}
static Utils::Link linkAtCursor(const QTextCursor &cursor,
const QString &filePath,
uint line,
uint column,
ClangEditorDocumentProcessor *processor)
{
using Link = Utils::Link;
const QVector<ClangBackEnd::TokenInfoContainer> &marks
= processor->tokenInfos();
ClangBackEnd::TokenInfoContainer mark;
if (!findMark(marks, line, column, mark))
return Link();
if (mark.extraInfo.includeDirectivePath && !isValidIncludePathToken(mark))
return Link();
Link token(Utils::FilePath::fromString(filePath), mark.line, mark.column);
token.linkTextStart = getMarkPos(cursor, mark);
token.linkTextEnd = token.linkTextStart + mark.length;
if (mark.extraInfo.includeDirectivePath) {
// Tweak include paths to cover everything between "" or <>.
if (mark.extraInfo.token.startsWith("\"")) {
token.linkTextStart++;
token.linkTextEnd--;
} else {
// '#include <path/file.h>' case. Clang gives us a separate token for each part of
// the path. We want to have the full range instead therefore we search for < and >
// tokens around the current token.
const int index = marks.indexOf(mark);
const int startIndex = includePathStartIndex(marks, index);
const int endIndex = includePathEndIndex(marks, index);
if (startIndex != index)
token.linkTextStart = getMarkPos(cursor, marks[startIndex]);
if (endIndex != index)
token.linkTextEnd = getMarkPos(cursor, marks[endIndex]) + marks[endIndex].length;
}
return token;
}
if (mark.extraInfo.identifier || mark.extraInfo.token == "operator"
|| mark.extraInfo.token == "auto") {
return token;
}
return Link();
}
static ::Utils::ProcessLinkCallback extendedCallback(::Utils::ProcessLinkCallback &&callback,
const CppEditor::SymbolInfo &result)
{
// If globalFollowSymbol finds nothing follow to the declaration.
return [original_callback = std::move(callback), result](const ::Utils::Link &link) {
if (link.linkTextStart < 0 && result.isResultOnlyForFallBack) {
return original_callback(Utils::Link(
Utils::FilePath::fromString(result.fileName).cleanPath(),
result.startLine,
result.startColumn - 1));
}
return original_callback(link);
};
}
static bool isSameInvocationContext(const Utils::FilePath &filePath)
{
return TextEditor::BaseTextEditor::currentTextEditor()->editorWidget()->isVisible()
&& Core::EditorManager::currentDocument()->filePath() == filePath;
}
void ClangFollowSymbol::findLink(const CppEditor::CursorInEditor &data,
::Utils::ProcessLinkCallback &&processLinkCallback,
bool resolveTarget,
const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
CppEditor::SymbolFinder *symbolFinder,
bool inNextSplit)
{
ClangdClient * const client
= ClangModelManagerSupport::instance()->clientForFile(data.filePath());
if (client && client->isFullyIndexed()) {
client->followSymbol(data.textDocument(), data.cursor(), data.editorWidget(),
std::move(processLinkCallback), resolveTarget, inNextSplit);
return;
}
int line = 0;
int column = 0;
QTextCursor cursor = Utils::Text::wordStartCursor(data.cursor());
Utils::Text::convertPosition(cursor.document(), cursor.position(), &line, &column);
ClangEditorDocumentProcessor *processor = ClangEditorDocumentProcessor::get(
data.filePath().toString());
if (!processor)
return processLinkCallback(Utils::Link());
if (!resolveTarget) {
Utils::Link link = linkAtCursor(cursor,
data.filePath().toString(),
static_cast<uint>(line),
static_cast<uint>(column),
processor);
if (link == Utils::Link()) {
CppEditor::FollowSymbolUnderCursor followSymbol;
return followSymbol.findLink(data,
std::move(processLinkCallback),
false,
snapshot,
documentFromSemanticInfo,
symbolFinder,
inNextSplit);
}
return processLinkCallback(link);
}
QFuture<CppEditor::SymbolInfo> infoFuture
= processor->requestFollowSymbol(static_cast<int>(line),
static_cast<int>(column));
if (infoFuture.isCanceled())
return processLinkCallback(Utils::Link());
if (m_watcher)
m_watcher->cancel();
m_watcher = std::make_unique<FutureSymbolWatcher>();
QObject::connect(m_watcher.get(), &FutureSymbolWatcher::finished, [=, filePath=data.filePath(),
callback=std::move(processLinkCallback)]() mutable {
if (m_watcher->isCanceled() || !isSameInvocationContext(filePath))
return callback(Utils::Link());
CppEditor::SymbolInfo result = m_watcher->result();
// We did not fail but the result is empty
if (result.fileName.isEmpty() || result.isResultOnlyForFallBack) {
const CppEditor::RefactoringEngineInterface &refactoringEngine
= *CppEditor::CppModelManager::instance();
refactoringEngine.globalFollowSymbol(data,
extendedCallback(std::move(callback), result),
snapshot,
documentFromSemanticInfo,
symbolFinder,
inNextSplit);
} else {
callback(Link(Utils::FilePath::fromString(result.fileName).cleanPath(),
result.startLine,
result.startColumn - 1));
}
});
m_watcher->setFuture(infoFuture);
}
void ClangFollowSymbol::switchDeclDef(const CppEditor::CursorInEditor &data,
Utils::ProcessLinkCallback &&processLinkCallback,
const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
CppEditor::SymbolFinder *symbolFinder)
{
ClangdClient * const client
= ClangModelManagerSupport::instance()->clientForFile(data.filePath());
if (client && client->isFullyIndexed()) {
client->switchDeclDef(data.textDocument(), data.cursor(), data.editorWidget(),
std::move(processLinkCallback));
return;
}
CppEditor::CppModelManager::builtinFollowSymbol().switchDeclDef(
data, std::move(processLinkCallback), snapshot, documentFromSemanticInfo,
symbolFinder);
}
} // namespace Internal
} // namespace ClangCodeModel

View File

@@ -1,60 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2017 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 <cppeditor/cpptoolsreuse.h>
#include <cppeditor/followsymbolinterface.h>
#include <QFutureWatcher>
namespace ClangCodeModel {
namespace Internal {
class ClangFollowSymbol : public CppEditor::FollowSymbolInterface
{
public:
void findLink(const CppEditor::CursorInEditor &data,
::Utils::ProcessLinkCallback &&processLinkCallback,
bool resolveTarget,
const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
CppEditor::SymbolFinder *symbolFinder,
bool inNextSplit) override;
void switchDeclDef(const CppEditor::CursorInEditor &data,
Utils::ProcessLinkCallback &&processLinkCallback,
const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
CppEditor::SymbolFinder *symbolFinder) override;
private:
using FutureSymbolWatcher = QFutureWatcher<CppEditor::SymbolInfo>;
std::unique_ptr<FutureSymbolWatcher> m_watcher;
};
} // namespace Internal
} // namespace ClangCodeModel

View File

@@ -29,7 +29,6 @@
#include "clangdclient.h"
#include "clangdquickfixfactory.h"
#include "clangeditordocumentprocessor.h"
#include "clangfollowsymbol.h"
#include "clangdlocatorfilters.h"
#include "clanghoverhandler.h"
#include "clangoverviewmodel.h"
@@ -45,11 +44,13 @@
#include <cppeditor/cppcodemodelsettings.h>
#include <cppeditor/cppeditorconstants.h>
#include <cppeditor/cppeditorwidget.h>
#include <cppeditor/cppfollowsymbolundercursor.h>
#include <cppeditor/cppmodelmanager.h>
#include <cppeditor/cppprojectfile.h>
#include <cppeditor/cpptoolsreuse.h>
#include <cppeditor/editordocumenthandle.h>
#include <cppeditor/symbolfinder.h>
#include <languageclient/languageclientmanager.h>
@@ -106,7 +107,6 @@ static const QList<TextEditor::TextDocument *> allCppDocuments()
ClangModelManagerSupport::ClangModelManagerSupport()
: m_completionAssistProvider(m_communicator, CompletionType::Other)
, m_functionHintAssistProvider(m_communicator, CompletionType::FunctionHint)
, m_followSymbol(new ClangFollowSymbol)
, m_refactoringEngine(new RefactoringEngine)
{
QTC_CHECK(!m_instance);
@@ -188,9 +188,37 @@ TextEditor::BaseHoverHandler *ClangModelManagerSupport::createHoverHandler()
return new Internal::ClangHoverHandler;
}
CppEditor::FollowSymbolInterface &ClangModelManagerSupport::followSymbolInterface()
void ClangModelManagerSupport::followSymbol(const CppEditor::CursorInEditor &data,
Utils::ProcessLinkCallback &&processLinkCallback, bool resolveTarget,
bool inNextSplit)
{
return *m_followSymbol;
if (ClangdClient * const client = clientForFile(data.filePath());
client && client->isFullyIndexed()) {
client->followSymbol(data.textDocument(), data.cursor(), data.editorWidget(),
std::move(processLinkCallback), resolveTarget, inNextSplit);
return;
}
SymbolFinder finder;
CppModelManager::builtinFollowSymbol().findLink(data, std::move(processLinkCallback),
resolveTarget, CppModelManager::instance()->snapshot(),
data.editorWidget()->semanticInfo().doc, &finder, inNextSplit);
}
void ClangModelManagerSupport::switchDeclDef(const CppEditor::CursorInEditor &data,
Utils::ProcessLinkCallback &&processLinkCallback)
{
if (ClangdClient * const client = clientForFile(data.filePath());
client && client->isFullyIndexed()) {
client->switchDeclDef(data.textDocument(), data.cursor(), data.editorWidget(),
std::move(processLinkCallback));
return;
}
SymbolFinder finder;
CppModelManager::builtinFollowSymbol().switchDeclDef(data, std::move(processLinkCallback),
CppModelManager::instance()->snapshot(), data.editorWidget()->semanticInfo().doc,
&finder);
}
CppEditor::RefactoringEngineInterface &ClangModelManagerSupport::refactoringEngineInterface()

View File

@@ -45,10 +45,7 @@ class QWidget;
QT_END_NAMESPACE
namespace TextEditor { class TextEditorWidget; }
namespace CppEditor {
class FollowSymbolInterface;
class RefactoringEngineInterface;
} // namespace CppEditor
namespace CppEditor { class RefactoringEngineInterface; }
namespace ClangCodeModel {
namespace Internal {
@@ -71,7 +68,6 @@ public:
TextEditor::BaseHoverHandler *createHoverHandler() override;
CppEditor::BaseEditorDocumentProcessor *createEditorDocumentProcessor(
TextEditor::TextDocument *baseTextDocument) override;
CppEditor::FollowSymbolInterface &followSymbolInterface() override;
CppEditor::RefactoringEngineInterface &refactoringEngineInterface() override;
std::unique_ptr<CppEditor::AbstractOverviewModel> createOverviewModel() override;
bool supportsOutline(const TextEditor::TextDocument *document) const override;
@@ -92,6 +88,12 @@ signals:
void createdClient(ClangdClient *client);
private:
void followSymbol(const CppEditor::CursorInEditor &data,
Utils::ProcessLinkCallback &&processLinkCallback, bool resolveTarget,
bool inNextSplit) override;
void switchDeclDef(const CppEditor::CursorInEditor &data,
Utils::ProcessLinkCallback &&processLinkCallback) override;
void onEditorOpened(Core::IEditor *editor);
void onEditorClosed(const QList<Core::IEditor *> &editors);
void onCurrentEditorChanged(Core::IEditor *newCurrent);
@@ -142,7 +144,6 @@ private:
BackendCommunicator m_communicator;
ClangCompletionAssistProvider m_completionAssistProvider;
ClangCompletionAssistProvider m_functionHintAssistProvider;
std::unique_ptr<CppEditor::FollowSymbolInterface> m_followSymbol;
std::unique_ptr<CppEditor::RefactoringEngineInterface> m_refactoringEngine;
QHash<ProjectExplorer::Project *, ClangProjectSettings *> m_projectSettings;

View File

@@ -124,25 +124,5 @@ void RefactoringEngine::findUsages(const CppEditor::CursorInEditor &cursor,
client->findUsages(cursor.textDocument(), cursor.cursor(), {});
}
void RefactoringEngine::globalFollowSymbol(
const CppEditor::CursorInEditor &cursor,
Utils::ProcessLinkCallback &&callback,
const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &doc,
CppEditor::SymbolFinder *symbolFinder,
bool inNextSplit) const
{
ClangdClient * const client
= ClangModelManagerSupport::instance()->clientForFile(cursor.filePath());
if (!client || !client->isFullyIndexed()) {
CppEditor::CppModelManager::builtinRefactoringEngine()
->globalFollowSymbol(cursor, std::move(callback), snapshot, doc, symbolFinder,
inNextSplit);
return;
}
client->followSymbol(cursor.textDocument(), cursor.cursor(), cursor.editorWidget(),
std::move(callback), true, inNextSplit);
}
} // namespace Internal
} // namespace ClangCodeModel

View File

@@ -48,12 +48,6 @@ public:
const QString &replacement) override;
void findUsages(const CppEditor::CursorInEditor &cursor,
CppEditor::UsagesCallback &&callback) const override;
void globalFollowSymbol(const CppEditor::CursorInEditor &cursor,
::Utils::ProcessLinkCallback &&callback,
const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &doc,
CppEditor::SymbolFinder *symbolFinder,
bool inNextSplit) const override;
private:
using FutureCursorWatcher = QFutureWatcher<CppEditor::CursorInfo>;