2013-12-10 14:37:32 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2013-12-10 14:37:32 +01:00
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2013-12-10 14:37:32 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2013-12-10 14:37:32 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
#include "clangmodelmanagersupport.h"
|
|
|
|
|
|
2015-11-30 09:43:50 +01:00
|
|
|
#include "clangconstants.h"
|
2021-04-20 14:42:29 +02:00
|
|
|
#include "clangdclient.h"
|
2022-07-07 15:21:31 +02:00
|
|
|
#include "clangdquickfixes.h"
|
2014-08-19 15:59:29 +02:00
|
|
|
#include "clangeditordocumentprocessor.h"
|
2021-09-30 13:19:50 +02:00
|
|
|
#include "clangdlocatorfilters.h"
|
2021-06-22 14:40:19 +02:00
|
|
|
#include "clangutils.h"
|
2015-05-08 15:48:17 +02:00
|
|
|
|
2021-09-03 17:14:54 +02:00
|
|
|
#include <coreplugin/documentmanager.h>
|
2021-04-29 16:09:07 +02:00
|
|
|
#include <coreplugin/editormanager/documentmodel.h>
|
2015-05-08 15:48:17 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2021-02-23 13:51:41 +01:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/messagemanager.h>
|
2018-02-02 13:01:07 +01:00
|
|
|
|
2022-04-27 17:21:30 +02:00
|
|
|
#include <cppeditor/abstractoverviewmodel.h>
|
2021-08-30 10:58:08 +02:00
|
|
|
#include <cppeditor/cppcodemodelsettings.h>
|
2022-02-18 13:19:07 +01:00
|
|
|
#include <cppeditor/cppeditorconstants.h>
|
2022-04-25 13:37:58 +02:00
|
|
|
#include <cppeditor/cppeditorwidget.h>
|
2021-08-30 10:58:08 +02:00
|
|
|
#include <cppeditor/cppfollowsymbolundercursor.h>
|
|
|
|
|
#include <cppeditor/cppmodelmanager.h>
|
2021-09-03 17:14:54 +02:00
|
|
|
#include <cppeditor/cppprojectfile.h>
|
2021-08-30 10:58:08 +02:00
|
|
|
#include <cppeditor/cpptoolsreuse.h>
|
|
|
|
|
#include <cppeditor/editordocumenthandle.h>
|
2016-01-12 12:51:33 +01:00
|
|
|
|
2021-02-23 13:51:41 +01:00
|
|
|
#include <languageclient/languageclientmanager.h>
|
|
|
|
|
|
2015-12-15 11:59:48 +01:00
|
|
|
#include <texteditor/quickfix.h>
|
2016-01-12 12:51:33 +01:00
|
|
|
|
2021-02-23 13:51:41 +01:00
|
|
|
#include <projectexplorer/buildconfiguration.h>
|
2021-09-03 17:14:54 +02:00
|
|
|
#include <projectexplorer/buildsystem.h>
|
2015-05-08 15:48:17 +02:00
|
|
|
#include <projectexplorer/project.h>
|
2021-02-23 13:51:41 +01:00
|
|
|
#include <projectexplorer/projectnodes.h>
|
2022-02-17 11:46:00 +01:00
|
|
|
#include <projectexplorer/projecttree.h>
|
2018-01-22 16:31:05 +01:00
|
|
|
#include <projectexplorer/session.h>
|
2021-02-23 13:51:41 +01:00
|
|
|
#include <projectexplorer/target.h>
|
2022-04-28 12:10:53 +02:00
|
|
|
#include <projectexplorer/taskhub.h>
|
2015-05-08 15:48:17 +02:00
|
|
|
|
2018-01-19 16:57:18 +01:00
|
|
|
#include <utils/algorithm.h>
|
2022-06-17 11:58:14 +02:00
|
|
|
#include <utils/infobar.h>
|
2015-05-08 15:48:17 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2021-02-23 13:51:41 +01:00
|
|
|
#include <utils/runextensions.h>
|
2013-12-10 14:37:32 +01:00
|
|
|
|
2018-10-10 14:32:00 +02:00
|
|
|
#include <QApplication>
|
2022-06-17 11:58:14 +02:00
|
|
|
#include <QLabel>
|
2015-08-24 18:26:09 +02:00
|
|
|
#include <QMenu>
|
|
|
|
|
#include <QTextBlock>
|
2018-10-19 10:20:27 +02:00
|
|
|
#include <QTimer>
|
2021-09-01 16:01:46 +02:00
|
|
|
#include <QtDebug>
|
2013-12-10 14:37:32 +01:00
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
using namespace CppEditor;
|
2021-02-23 13:51:41 +01:00
|
|
|
using namespace LanguageClient;
|
2013-12-10 14:37:32 +01:00
|
|
|
|
2021-08-12 09:39:02 +02:00
|
|
|
namespace ClangCodeModel {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2018-11-04 21:44:21 +01:00
|
|
|
static ClangModelManagerSupport *m_instance = nullptr;
|
2013-12-10 14:37:32 +01:00
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
static CppEditor::CppModelManager *cppModelManager()
|
2013-12-10 14:37:32 +01:00
|
|
|
{
|
2021-08-30 10:58:08 +02:00
|
|
|
return CppEditor::CppModelManager::instance();
|
2013-12-10 14:37:32 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-17 11:46:00 +01:00
|
|
|
static ProjectExplorer::Project *fallbackProject()
|
2021-07-14 17:07:34 +02:00
|
|
|
{
|
2022-02-17 11:46:00 +01:00
|
|
|
if (ProjectExplorer::Project * const p = ProjectExplorer::ProjectTree::currentProject())
|
|
|
|
|
return p;
|
|
|
|
|
return ProjectExplorer::SessionManager::startupProject();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 13:19:07 +01:00
|
|
|
static const QList<TextEditor::TextDocument *> allCppDocuments()
|
2021-07-14 17:07:34 +02:00
|
|
|
{
|
2022-02-18 13:19:07 +01:00
|
|
|
const auto isCppDocument = Utils::equal(&Core::IDocument::id,
|
|
|
|
|
Utils::Id(CppEditor::Constants::CPPEDITOR_ID));
|
|
|
|
|
const QList<Core::IDocument *> documents
|
|
|
|
|
= Utils::filtered(Core::DocumentModel::openedDocuments(), isCppDocument);
|
|
|
|
|
return Utils::qobject_container_cast<TextEditor::TextDocument *>(documents);
|
2021-07-14 17:07:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-31 16:35:10 +02:00
|
|
|
static bool fileIsProjectBuildArtifact(const Client *client, const Utils::FilePath &filePath)
|
|
|
|
|
{
|
|
|
|
|
if (const auto p = client->project()) {
|
|
|
|
|
if (const auto t = p->activeTarget()) {
|
|
|
|
|
if (const auto bc = t->activeBuildConfiguration()) {
|
|
|
|
|
if (filePath.isChildOf(bc->buildDirectory()))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Client *clientForGeneratedFile(const Utils::FilePath &filePath)
|
|
|
|
|
{
|
|
|
|
|
for (Client * const client : LanguageClientManager::clients()) {
|
|
|
|
|
if (qobject_cast<ClangdClient *>(client) && client->reachable()
|
|
|
|
|
&& fileIsProjectBuildArtifact(client, filePath)) {
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 11:58:14 +02:00
|
|
|
static void checkSystemForClangdSuitability()
|
|
|
|
|
{
|
|
|
|
|
if (ClangdSettings::haveCheckedHardwareRequirements())
|
|
|
|
|
return;
|
|
|
|
|
if (ClangdSettings::hardwareFulfillsRequirements())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ClangdSettings::setUseClangd(false);
|
|
|
|
|
const QString warnStr = ClangModelManagerSupport::tr("The use of clangd for the C/C++ "
|
|
|
|
|
"code model was disabled, because it is likely that its memory requirements "
|
|
|
|
|
"would be higher than what your system can handle.");
|
|
|
|
|
const Utils::Id clangdWarningSetting("WarnAboutClangd");
|
|
|
|
|
Utils::InfoBarEntry info(clangdWarningSetting, warnStr);
|
|
|
|
|
info.setDetailsWidgetCreator([] {
|
|
|
|
|
const auto label = new QLabel(ClangModelManagerSupport::tr(
|
|
|
|
|
"With clangd enabled, Qt Creator fully supports modern C++ "
|
|
|
|
|
"when highlighting code, completing symbols and so on.<br>"
|
|
|
|
|
"This comes at a higher cost in terms of CPU load and memory usage compared "
|
|
|
|
|
"to the built-in code model, which therefore might be the better choice "
|
|
|
|
|
"on older machines and/or with legacy code.<br>"
|
|
|
|
|
"You can enable/disable and fine-tune clangd <a href=\"dummy\">here</a>."));
|
|
|
|
|
label->setWordWrap(true);
|
|
|
|
|
QObject::connect(label, &QLabel::linkActivated, [] {
|
|
|
|
|
Core::ICore::showOptionsDialog(CppEditor::Constants::CPP_CLANGD_SETTINGS_ID);
|
|
|
|
|
});
|
|
|
|
|
return label;
|
|
|
|
|
});
|
|
|
|
|
info.addCustomButton(ClangModelManagerSupport::tr("Enable Anyway"), [clangdWarningSetting] {
|
|
|
|
|
ClangdSettings::setUseClangd(true);
|
|
|
|
|
Core::ICore::infoBar()->removeInfo(clangdWarningSetting);
|
|
|
|
|
});
|
|
|
|
|
Core::ICore::infoBar()->addInfo(info);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 16:52:48 +02:00
|
|
|
ClangModelManagerSupport::ClangModelManagerSupport()
|
2013-12-10 14:37:32 +01:00
|
|
|
{
|
2016-11-21 13:30:26 +01:00
|
|
|
QTC_CHECK(!m_instance);
|
|
|
|
|
m_instance = this;
|
2015-05-08 15:48:17 +02:00
|
|
|
|
2021-09-03 17:14:54 +02:00
|
|
|
watchForExternalChanges();
|
2021-11-05 15:35:54 +01:00
|
|
|
watchForInternalChanges();
|
2022-02-01 17:39:00 +01:00
|
|
|
setupClangdConfigFile();
|
2022-06-17 11:58:14 +02:00
|
|
|
checkSystemForClangdSuitability();
|
2021-09-30 13:19:50 +02:00
|
|
|
cppModelManager()->setCurrentDocumentFilter(std::make_unique<ClangdCurrentDocumentFilter>());
|
2021-02-23 13:51:41 +01:00
|
|
|
cppModelManager()->setLocatorFilter(std::make_unique<ClangGlobalSymbolFilter>());
|
|
|
|
|
cppModelManager()->setClassesFilter(std::make_unique<ClangClassesFilter>());
|
|
|
|
|
cppModelManager()->setFunctionsFilter(std::make_unique<ClangFunctionsFilter>());
|
2017-11-27 15:51:18 +01:00
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
2016-01-11 13:05:04 +01:00
|
|
|
connect(editorManager, &Core::EditorManager::editorOpened,
|
2018-10-18 09:21:35 +02:00
|
|
|
this, &ClangModelManagerSupport::onEditorOpened);
|
2015-05-08 15:48:17 +02:00
|
|
|
connect(editorManager, &Core::EditorManager::currentEditorChanged,
|
2018-10-18 09:21:35 +02:00
|
|
|
this, &ClangModelManagerSupport::onCurrentEditorChanged);
|
2015-05-08 15:48:17 +02:00
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
CppEditor::CppModelManager *modelManager = cppModelManager();
|
|
|
|
|
connect(modelManager, &CppEditor::CppModelManager::abstractEditorSupportContentsUpdated,
|
2018-10-18 09:21:35 +02:00
|
|
|
this, &ClangModelManagerSupport::onAbstractEditorSupportContentsUpdated);
|
2021-08-30 10:58:08 +02:00
|
|
|
connect(modelManager, &CppEditor::CppModelManager::abstractEditorSupportRemoved,
|
2018-10-18 09:21:35 +02:00
|
|
|
this, &ClangModelManagerSupport::onAbstractEditorSupportRemoved);
|
2021-08-30 10:58:08 +02:00
|
|
|
connect(modelManager, &CppEditor::CppModelManager::projectPartsUpdated,
|
2018-10-18 09:21:35 +02:00
|
|
|
this, &ClangModelManagerSupport::onProjectPartsUpdated);
|
2021-08-30 10:58:08 +02:00
|
|
|
connect(modelManager, &CppEditor::CppModelManager::projectPartsRemoved,
|
2018-10-18 09:21:35 +02:00
|
|
|
this, &ClangModelManagerSupport::onProjectPartsRemoved);
|
2021-10-20 11:41:38 +02:00
|
|
|
connect(modelManager, &CppModelManager::fallbackProjectPartUpdated, this, [this] {
|
|
|
|
|
if (ClangdClient * const fallbackClient = clientForProject(nullptr)) {
|
|
|
|
|
LanguageClientManager::shutdownClient(fallbackClient);
|
|
|
|
|
claimNonProjectSources(createClient(nullptr, {}));
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-07-11 14:57:25 +02:00
|
|
|
|
2018-01-22 16:31:05 +01:00
|
|
|
auto *sessionManager = ProjectExplorer::SessionManager::instance();
|
2021-07-14 17:07:34 +02:00
|
|
|
connect(sessionManager, &ProjectExplorer::SessionManager::projectRemoved,
|
2022-02-17 11:46:00 +01:00
|
|
|
this, [this] { claimNonProjectSources(clientForProject(fallbackProject())); });
|
2018-01-22 16:31:05 +01:00
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
CppEditor::ClangdSettings::setDefaultClangdPath(Core::ICore::clangdExecutable(CLANG_BINDIR));
|
|
|
|
|
connect(&CppEditor::ClangdSettings::instance(), &CppEditor::ClangdSettings::changed,
|
2021-07-01 04:17:54 +02:00
|
|
|
this, &ClangModelManagerSupport::onClangdSettingsChanged);
|
2021-02-23 13:51:41 +01:00
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
if (CppEditor::ClangdSettings::instance().useClangd())
|
2021-07-14 17:07:34 +02:00
|
|
|
createClient(nullptr, {});
|
|
|
|
|
|
2021-05-05 17:59:33 +02:00
|
|
|
m_generatorSynchronizer.setCancelOnWait(true);
|
2021-06-22 14:40:19 +02:00
|
|
|
new ClangdQuickFixFactory(); // memory managed by CppEditor::g_cppQuickFixFactories
|
2013-12-10 14:37:32 +01:00
|
|
|
}
|
|
|
|
|
|
2018-10-18 09:21:35 +02:00
|
|
|
ClangModelManagerSupport::~ClangModelManagerSupport()
|
2013-12-10 14:37:32 +01:00
|
|
|
{
|
2021-05-05 17:59:33 +02:00
|
|
|
m_generatorSynchronizer.waitForFinished();
|
2018-11-04 21:44:21 +01:00
|
|
|
m_instance = nullptr;
|
2013-12-10 14:37:32 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
CppEditor::CppCompletionAssistProvider *ClangModelManagerSupport::completionAssistProvider()
|
2013-12-10 14:37:32 +01:00
|
|
|
{
|
2022-04-27 16:02:08 +02:00
|
|
|
return nullptr;
|
2013-12-10 14:37:32 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-25 13:37:58 +02:00
|
|
|
void ClangModelManagerSupport::followSymbol(const CppEditor::CursorInEditor &data,
|
2022-06-03 15:17:33 +02:00
|
|
|
const Utils::LinkHandler &processLinkCallback, bool resolveTarget,
|
2022-04-25 13:37:58 +02:00
|
|
|
bool inNextSplit)
|
2017-08-03 16:43:38 +02:00
|
|
|
{
|
2022-04-25 13:37:58 +02:00
|
|
|
if (ClangdClient * const client = clientForFile(data.filePath());
|
|
|
|
|
client && client->isFullyIndexed()) {
|
|
|
|
|
client->followSymbol(data.textDocument(), data.cursor(), data.editorWidget(),
|
2022-06-03 15:17:33 +02:00
|
|
|
processLinkCallback, resolveTarget, inNextSplit);
|
2022-04-25 13:37:58 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-03 15:17:33 +02:00
|
|
|
CppModelManager::followSymbol(data, processLinkCallback, resolveTarget, inNextSplit,
|
2022-04-29 16:52:48 +02:00
|
|
|
CppModelManager::Backend::Builtin);
|
2022-04-25 13:37:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangModelManagerSupport::switchDeclDef(const CppEditor::CursorInEditor &data,
|
2022-06-03 15:17:33 +02:00
|
|
|
const Utils::LinkHandler &processLinkCallback)
|
2022-04-25 13:37:58 +02:00
|
|
|
{
|
|
|
|
|
if (ClangdClient * const client = clientForFile(data.filePath());
|
|
|
|
|
client && client->isFullyIndexed()) {
|
|
|
|
|
client->switchDeclDef(data.textDocument(), data.cursor(), data.editorWidget(),
|
2022-06-03 15:17:33 +02:00
|
|
|
processLinkCallback);
|
2022-04-25 13:37:58 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-03 15:17:33 +02:00
|
|
|
CppModelManager::switchDeclDef(data, processLinkCallback, CppModelManager::Backend::Builtin);
|
2017-08-03 16:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-29 16:52:48 +02:00
|
|
|
void ClangModelManagerSupport::startLocalRenaming(const CppEditor::CursorInEditor &data,
|
|
|
|
|
const CppEditor::ProjectPart *projectPart,
|
|
|
|
|
RenameCallback &&renameSymbolsCallback)
|
2017-09-26 16:00:30 +02:00
|
|
|
{
|
2022-04-29 16:52:48 +02:00
|
|
|
if (ClangdClient * const client = clientForFile(data.filePath());
|
|
|
|
|
client && client->reachable()) {
|
|
|
|
|
client->findLocalUsages(data.textDocument(), data.cursor(),
|
|
|
|
|
std::move(renameSymbolsCallback));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CppModelManager::startLocalRenaming(data, projectPart,
|
|
|
|
|
std::move(renameSymbolsCallback), CppModelManager::Backend::Builtin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangModelManagerSupport::globalRename(const CppEditor::CursorInEditor &cursor,
|
2022-06-02 00:23:11 +02:00
|
|
|
const QString &replacement)
|
2022-04-29 16:52:48 +02:00
|
|
|
{
|
|
|
|
|
if (ClangdClient * const client = clientForFile(cursor.filePath());
|
|
|
|
|
client && client->isFullyIndexed()) {
|
|
|
|
|
QTC_ASSERT(client->documentOpen(cursor.textDocument()),
|
|
|
|
|
client->openDocument(cursor.textDocument()));
|
|
|
|
|
client->findUsages(cursor.textDocument(), cursor.cursor(), replacement);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-02 00:23:11 +02:00
|
|
|
CppModelManager::globalRename(cursor, replacement, CppModelManager::Backend::Builtin);
|
2022-04-29 16:52:48 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-02 00:42:28 +02:00
|
|
|
void ClangModelManagerSupport::findUsages(const CppEditor::CursorInEditor &cursor) const
|
2022-04-29 16:52:48 +02:00
|
|
|
{
|
|
|
|
|
if (ClangdClient * const client = clientForFile(cursor.filePath());
|
|
|
|
|
client && client->isFullyIndexed()) {
|
|
|
|
|
QTC_ASSERT(client->documentOpen(cursor.textDocument()),
|
|
|
|
|
client->openDocument(cursor.textDocument()));
|
|
|
|
|
client->findUsages(cursor.textDocument(), cursor.cursor(), {});
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-02 00:42:28 +02:00
|
|
|
CppModelManager::findUsages(cursor, CppModelManager::Backend::Builtin);
|
2017-09-26 16:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-18 15:16:40 +02:00
|
|
|
void ClangModelManagerSupport::switchHeaderSource(const Utils::FilePath &filePath, bool inNextSplit)
|
|
|
|
|
{
|
|
|
|
|
if (ClangdClient * const client = clientForFile(filePath)) {
|
|
|
|
|
// The fast, synchronous approach works most of the time, so let's try that one first.
|
|
|
|
|
const auto otherFile = Utils::FilePath::fromString(
|
|
|
|
|
correspondingHeaderOrSource(filePath.toString()));
|
|
|
|
|
if (!otherFile.isEmpty())
|
|
|
|
|
openEditor(otherFile, inNextSplit);
|
|
|
|
|
else
|
|
|
|
|
client->switchHeaderSource(filePath, inNextSplit);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
CppModelManager::switchHeaderSource(inNextSplit, CppModelManager::Backend::Builtin);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
std::unique_ptr<CppEditor::AbstractOverviewModel> ClangModelManagerSupport::createOverviewModel()
|
2018-02-02 13:01:07 +01:00
|
|
|
{
|
2022-04-27 17:21:30 +02:00
|
|
|
return {};
|
2018-02-02 13:01:07 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-10 17:21:59 +02:00
|
|
|
bool ClangModelManagerSupport::usesClangd(const TextEditor::TextDocument *document) const
|
2022-04-27 16:36:22 +02:00
|
|
|
{
|
|
|
|
|
return clientForFile(document->filePath());
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
CppEditor::BaseEditorDocumentProcessor *ClangModelManagerSupport::createEditorDocumentProcessor(
|
2014-09-22 18:43:31 +02:00
|
|
|
TextEditor::TextDocument *baseTextDocument)
|
2013-12-10 14:37:32 +01:00
|
|
|
{
|
2022-04-28 17:15:39 +02:00
|
|
|
const auto processor = new ClangEditorDocumentProcessor(baseTextDocument);
|
2022-02-08 11:49:37 +01:00
|
|
|
const auto handleConfigChange = [this](const Utils::FilePath &fp,
|
|
|
|
|
const BaseEditorDocumentParser::Configuration &config) {
|
|
|
|
|
if (const auto client = clientForFile(fp))
|
|
|
|
|
client->updateParserConfig(fp, config);
|
|
|
|
|
};
|
|
|
|
|
connect(processor, &ClangEditorDocumentProcessor::parserConfigChanged,
|
|
|
|
|
this, handleConfigChange);
|
|
|
|
|
return processor;
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-18 09:21:35 +02:00
|
|
|
void ClangModelManagerSupport::onCurrentEditorChanged(Core::IEditor *editor)
|
2015-05-08 15:48:17 +02:00
|
|
|
{
|
2018-04-12 14:49:07 +02:00
|
|
|
// Update task hub issues for current CppEditorDocument
|
2022-04-28 12:10:53 +02:00
|
|
|
ProjectExplorer::TaskHub::clearTasks(Constants::TASK_CATEGORY_DIAGNOSTICS);
|
2018-04-20 13:55:49 +02:00
|
|
|
if (!editor || !editor->document() || !cppModelManager()->isCppEditor(editor))
|
|
|
|
|
return;
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
const ::Utils::FilePath filePath = editor->document()->filePath();
|
2020-07-15 16:11:26 +02:00
|
|
|
if (auto processor = ClangEditorDocumentProcessor::get(filePath.toString())) {
|
|
|
|
|
processor->semanticRehighlight();
|
2022-05-11 12:30:57 +02:00
|
|
|
if (const auto client = clientForFile(filePath)) {
|
2022-02-08 11:49:37 +01:00
|
|
|
client->updateParserConfig(filePath, processor->parserConfig());
|
2022-05-11 12:30:57 +02:00
|
|
|
client->switchIssuePaneEntries(filePath);
|
|
|
|
|
}
|
2020-07-15 16:11:26 +02:00
|
|
|
}
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-18 09:21:35 +02:00
|
|
|
void ClangModelManagerSupport::connectToWidgetsMarkContextMenuRequested(QWidget *editorWidget)
|
2015-08-24 18:26:09 +02:00
|
|
|
{
|
|
|
|
|
const auto widget = qobject_cast<TextEditor::TextEditorWidget *>(editorWidget);
|
|
|
|
|
if (widget) {
|
|
|
|
|
connect(widget, &TextEditor::TextEditorWidget::markContextMenuRequested,
|
2018-10-18 09:21:35 +02:00
|
|
|
this, &ClangModelManagerSupport::onTextMarkContextMenuRequested);
|
2015-08-24 18:26:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 11:21:06 +02:00
|
|
|
void ClangModelManagerSupport::updateLanguageClient(
|
2021-08-30 10:58:08 +02:00
|
|
|
ProjectExplorer::Project *project, const CppEditor::ProjectInfo::ConstPtr &projectInfo)
|
2021-02-23 13:51:41 +01:00
|
|
|
{
|
2022-05-11 15:27:09 +02:00
|
|
|
const ClangdSettings settings(ClangdProjectSettings(project).settings());
|
|
|
|
|
if (!settings.useClangd())
|
2021-02-23 13:51:41 +01:00
|
|
|
return;
|
|
|
|
|
const auto getJsonDbDir = [project] {
|
|
|
|
|
if (const ProjectExplorer::Target * const target = project->activeTarget()) {
|
|
|
|
|
if (const ProjectExplorer::BuildConfiguration * const bc
|
|
|
|
|
= target->activeBuildConfiguration()) {
|
2021-10-15 16:24:25 +02:00
|
|
|
return bc->buildDirectory() / ".qtc_clangd";
|
2021-02-23 13:51:41 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Utils::FilePath();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const Utils::FilePath jsonDbDir = getJsonDbDir();
|
|
|
|
|
if (jsonDbDir.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
const auto generatorWatcher = new QFutureWatcher<GenerateCompilationDbResult>;
|
|
|
|
|
connect(generatorWatcher, &QFutureWatcher<GenerateCompilationDbResult>::finished,
|
|
|
|
|
[this, project, projectInfo, getJsonDbDir, jsonDbDir, generatorWatcher] {
|
|
|
|
|
generatorWatcher->deleteLater();
|
|
|
|
|
if (!ProjectExplorer::SessionManager::hasProject(project))
|
|
|
|
|
return;
|
2022-05-11 15:27:09 +02:00
|
|
|
if (!ClangdSettings(ClangdProjectSettings(project).settings()).useClangd())
|
2021-06-28 14:55:54 +02:00
|
|
|
return;
|
2021-08-30 10:58:08 +02:00
|
|
|
const CppEditor::ProjectInfo::ConstPtr newProjectInfo
|
2021-08-20 11:21:06 +02:00
|
|
|
= cppModelManager()->projectInfo(project);
|
2021-05-07 16:10:07 +02:00
|
|
|
if (!newProjectInfo || *newProjectInfo != *projectInfo)
|
2021-02-23 13:51:41 +01:00
|
|
|
return;
|
|
|
|
|
if (getJsonDbDir() != jsonDbDir)
|
|
|
|
|
return;
|
|
|
|
|
const GenerateCompilationDbResult result = generatorWatcher->result();
|
|
|
|
|
if (!result.error.isEmpty()) {
|
|
|
|
|
Core::MessageManager::writeDisrupting(
|
|
|
|
|
tr("Cannot use clangd: Failed to generate compilation database:\n%1")
|
|
|
|
|
.arg(result.error));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (Client * const oldClient = clientForProject(project))
|
|
|
|
|
LanguageClientManager::shutdownClient(oldClient);
|
2021-04-21 14:29:49 +02:00
|
|
|
ClangdClient * const client = createClient(project, jsonDbDir);
|
2022-05-31 16:35:10 +02:00
|
|
|
connect(client, &Client::initialized, this, [this, client, project, projectInfo, jsonDbDir] {
|
2021-02-23 13:51:41 +01:00
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
if (!SessionManager::hasProject(project))
|
|
|
|
|
return;
|
2021-08-30 10:58:08 +02:00
|
|
|
if (!CppEditor::ClangdProjectSettings(project).settings().useClangd)
|
2021-06-28 14:55:54 +02:00
|
|
|
return;
|
2021-08-30 10:58:08 +02:00
|
|
|
const CppEditor::ProjectInfo::ConstPtr newProjectInfo
|
2021-05-07 16:10:07 +02:00
|
|
|
= cppModelManager()->projectInfo(project);
|
|
|
|
|
if (!newProjectInfo || *newProjectInfo != *projectInfo)
|
2021-02-23 13:51:41 +01:00
|
|
|
return;
|
|
|
|
|
|
2022-02-08 11:49:37 +01:00
|
|
|
const auto updateParserConfig = [client] {
|
|
|
|
|
if (const auto editor = TextEditor::BaseTextEditor::currentTextEditor()) {
|
|
|
|
|
if (!client->documentOpen(editor->textDocument()))
|
|
|
|
|
return;
|
|
|
|
|
const Utils::FilePath filePath = editor->textDocument()->filePath();
|
|
|
|
|
if (const auto processor = ClangEditorDocumentProcessor::get(
|
|
|
|
|
filePath.toString())) {
|
|
|
|
|
const CppEditor::BaseEditorDocumentParser::Configuration config
|
|
|
|
|
= processor->parserConfig();
|
|
|
|
|
client->updateParserConfig(filePath, config);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-29 16:09:07 +02:00
|
|
|
// Acquaint the client with all open C++ documents for this project.
|
|
|
|
|
bool hasDocuments = false;
|
2022-03-29 15:47:10 +02:00
|
|
|
const ClangdSettings settings(ClangdProjectSettings(project).settings());
|
2022-02-18 13:19:07 +01:00
|
|
|
for (TextEditor::TextDocument * const doc : allCppDocuments()) {
|
2022-02-17 11:46:00 +01:00
|
|
|
const Client * const currentClient = LanguageClientManager::clientForDocument(doc);
|
2022-03-29 15:47:10 +02:00
|
|
|
if (!settings.sizeIsOkay(doc->filePath()))
|
|
|
|
|
continue;
|
2022-07-04 13:39:33 +02:00
|
|
|
if (currentClient && currentClient->project()
|
|
|
|
|
&& currentClient->project() != project) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (const Project * const docProject
|
|
|
|
|
= SessionManager::projectForFile(doc->filePath());
|
|
|
|
|
!docProject || docProject == project) {
|
2022-02-18 13:19:07 +01:00
|
|
|
LanguageClientManager::openDocumentWithClient(doc, client);
|
2022-02-17 11:46:00 +01:00
|
|
|
hasDocuments = true;
|
|
|
|
|
}
|
2021-04-29 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-31 16:35:10 +02:00
|
|
|
for (auto it = m_queuedShadowDocuments.begin(); it != m_queuedShadowDocuments.end();) {
|
|
|
|
|
if (fileIsProjectBuildArtifact(client, it.key())) {
|
|
|
|
|
if (it.value().isEmpty())
|
|
|
|
|
client->removeShadowDocument(it.key());
|
|
|
|
|
else
|
|
|
|
|
client->setShadowDocument(it.key(), it.value());
|
|
|
|
|
ClangdClient::handleUiHeaderChange(it.key().fileName());
|
|
|
|
|
it = m_queuedShadowDocuments.erase(it);
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
connect(client, &Client::shadowDocumentSwitched, this,
|
|
|
|
|
[](const Utils::FilePath &fp) {
|
|
|
|
|
ClangdClient::handleUiHeaderChange(fp.fileName());
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-08 11:49:37 +01:00
|
|
|
if (client->state() == Client::Initialized)
|
|
|
|
|
updateParserConfig();
|
|
|
|
|
else
|
|
|
|
|
connect(client, &Client::initialized, client, updateParserConfig);
|
|
|
|
|
connect(CppModelManager::instance(), &CppModelManager::projectPartsUpdated,
|
|
|
|
|
client, updateParserConfig);
|
|
|
|
|
|
2021-04-29 16:09:07 +02:00
|
|
|
if (hasDocuments)
|
|
|
|
|
return;
|
2021-02-23 13:51:41 +01:00
|
|
|
|
|
|
|
|
// clangd oddity: Background indexing only starts after opening a random file.
|
|
|
|
|
// TODO: changes to the compilation db do not seem to trigger re-indexing.
|
|
|
|
|
// How to force it?
|
|
|
|
|
ProjectNode * const rootNode = project->rootProjectNode();
|
|
|
|
|
if (!rootNode)
|
|
|
|
|
return;
|
|
|
|
|
const Node * const cxxNode = rootNode->findNode([](Node *n) {
|
|
|
|
|
const FileNode * const fileNode = n->asFileNode();
|
|
|
|
|
return fileNode && (fileNode->fileType() == FileType::Source
|
|
|
|
|
|| fileNode->fileType() == FileType::Header)
|
|
|
|
|
&& fileNode->filePath().exists();
|
|
|
|
|
});
|
|
|
|
|
if (!cxxNode)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-04-21 14:29:49 +02:00
|
|
|
client->openExtraFile(cxxNode->filePath());
|
|
|
|
|
client->closeExtraFile(cxxNode->filePath());
|
2021-02-23 13:51:41 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
2022-05-11 15:27:09 +02:00
|
|
|
const Utils::FilePath includeDir = settings.clangdIncludePath();
|
2021-10-15 16:24:25 +02:00
|
|
|
auto future = Utils::runAsync(&Internal::generateCompilationDB, projectInfo, jsonDbDir,
|
2021-05-07 16:10:07 +02:00
|
|
|
CompilationDbPurpose::CodeModel,
|
2022-05-19 14:48:09 +02:00
|
|
|
warningsConfigForProject(project),
|
|
|
|
|
globalClangOptions(), includeDir);
|
2021-05-05 17:59:33 +02:00
|
|
|
generatorWatcher->setFuture(future);
|
2021-05-12 15:12:48 +02:00
|
|
|
m_generatorSynchronizer.addFuture(future);
|
2021-02-23 13:51:41 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-20 15:46:35 +02:00
|
|
|
ClangdClient *ClangModelManagerSupport::clientForProject(
|
2021-06-08 14:28:25 +02:00
|
|
|
const ProjectExplorer::Project *project) const
|
2021-02-23 13:51:41 +01:00
|
|
|
{
|
|
|
|
|
const QList<Client *> clients = Utils::filtered(
|
|
|
|
|
LanguageClientManager::clientsForProject(project),
|
|
|
|
|
[](const LanguageClient::Client *c) {
|
2021-04-20 14:42:29 +02:00
|
|
|
return qobject_cast<const ClangdClient *>(c)
|
2021-02-23 13:51:41 +01:00
|
|
|
&& c->state() != Client::ShutdownRequested
|
|
|
|
|
&& c->state() != Client::Shutdown;
|
|
|
|
|
});
|
2021-09-01 16:01:46 +02:00
|
|
|
QTC_ASSERT(clients.size() <= 1, qDebug() << project << clients.size());
|
2021-10-29 16:39:10 +02:00
|
|
|
if (clients.size() > 1) {
|
|
|
|
|
Client *activeClient = nullptr;
|
|
|
|
|
for (Client * const c : clients) {
|
|
|
|
|
if (!activeClient && (c->state() == Client::Initialized
|
|
|
|
|
|| c->state() == Client::InitializeRequested)) {
|
|
|
|
|
activeClient = c;
|
|
|
|
|
} else {
|
|
|
|
|
LanguageClientManager::shutdownClient(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return qobject_cast<ClangdClient *>(activeClient);
|
|
|
|
|
}
|
2021-04-20 15:46:35 +02:00
|
|
|
return clients.empty() ? nullptr : qobject_cast<ClangdClient *>(clients.first());
|
2021-02-23 13:51:41 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-08 14:28:25 +02:00
|
|
|
ClangdClient *ClangModelManagerSupport::clientForFile(const Utils::FilePath &file) const
|
2021-05-19 13:08:01 +02:00
|
|
|
{
|
2022-02-17 11:46:00 +01:00
|
|
|
return qobject_cast<ClangdClient *>(LanguageClientManager::clientForFilePath(file));
|
2021-05-19 13:08:01 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-20 15:46:35 +02:00
|
|
|
ClangdClient *ClangModelManagerSupport::createClient(ProjectExplorer::Project *project,
|
|
|
|
|
const Utils::FilePath &jsonDbDir)
|
2021-02-23 13:51:41 +01:00
|
|
|
{
|
2021-04-21 14:29:49 +02:00
|
|
|
const auto client = new ClangdClient(project, jsonDbDir);
|
|
|
|
|
emit createdClient(client);
|
|
|
|
|
return client;
|
2021-02-23 13:51:41 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-17 11:46:00 +01:00
|
|
|
void ClangModelManagerSupport::claimNonProjectSources(ClangdClient *client)
|
2021-07-14 17:07:34 +02:00
|
|
|
{
|
2022-02-17 11:46:00 +01:00
|
|
|
if (!client)
|
|
|
|
|
return;
|
2022-02-18 13:19:07 +01:00
|
|
|
for (TextEditor::TextDocument * const doc : allCppDocuments()) {
|
|
|
|
|
if (Client * const currentClient = LanguageClientManager::clientForDocument(doc);
|
2022-02-17 11:46:00 +01:00
|
|
|
currentClient && currentClient->state() == Client::Initialized
|
|
|
|
|
&& (currentClient == client || currentClient->project())) {
|
2021-07-14 17:07:34 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
2022-03-29 15:47:10 +02:00
|
|
|
if (!ClangdSettings::instance().sizeIsOkay(doc->filePath()))
|
|
|
|
|
continue;
|
2022-07-04 13:39:33 +02:00
|
|
|
if (!ProjectExplorer::SessionManager::projectForFile(doc->filePath()))
|
|
|
|
|
LanguageClientManager::openDocumentWithClient(doc, client);
|
2021-07-14 17:07:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 17:14:54 +02:00
|
|
|
// If any open C/C++ source file is changed from outside Qt Creator, we restart the client
|
|
|
|
|
// for the respective project to force re-parsing of open documents and re-indexing.
|
|
|
|
|
// While this is not 100% bullet-proof, chances are good that in a typical session-based
|
|
|
|
|
// workflow, e.g. a git branch switch will hit at least one open file.
|
|
|
|
|
void ClangModelManagerSupport::watchForExternalChanges()
|
|
|
|
|
{
|
|
|
|
|
const auto projectIsParsing = [](const ProjectExplorer::Project *project) {
|
|
|
|
|
const ProjectExplorer::BuildSystem * const bs = project && project->activeTarget()
|
|
|
|
|
? project->activeTarget()->buildSystem() : nullptr;
|
|
|
|
|
return bs && (bs->isParsing() || bs->isWaitingForParse());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto timer = new QTimer(this);
|
|
|
|
|
timer->setInterval(3000);
|
|
|
|
|
connect(timer, &QTimer::timeout, this, [this, projectIsParsing] {
|
|
|
|
|
const auto clients = m_clientsToRestart;
|
|
|
|
|
m_clientsToRestart.clear();
|
|
|
|
|
for (ClangdClient * const client : clients) {
|
|
|
|
|
if (client && client->state() != Client::Shutdown
|
|
|
|
|
&& client->state() != Client::ShutdownRequested
|
|
|
|
|
&& !projectIsParsing(client->project())) {
|
2021-09-23 12:11:31 +02:00
|
|
|
ProjectExplorer::Project * const project = client->project();
|
2021-09-03 17:14:54 +02:00
|
|
|
updateLanguageClient(project, CppModelManager::instance()->projectInfo(project));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connect(Core::DocumentManager::instance(), &Core::DocumentManager::filesChangedExternally,
|
|
|
|
|
this, [this, timer, projectIsParsing](const QSet<Utils::FilePath> &files) {
|
|
|
|
|
if (!LanguageClientManager::hasClients<ClangdClient>())
|
|
|
|
|
return;
|
|
|
|
|
for (const Utils::FilePath &file : files) {
|
|
|
|
|
const ProjectFile::Kind kind = ProjectFile::classify(file.toString());
|
|
|
|
|
if (!ProjectFile::isSource(kind) && !ProjectFile::isHeader(kind))
|
|
|
|
|
continue;
|
|
|
|
|
const ProjectExplorer::Project * const project
|
|
|
|
|
= ProjectExplorer::SessionManager::projectForFile(file);
|
|
|
|
|
if (!project)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// If a project file was changed, it is very likely that we will have to generate
|
|
|
|
|
// a new compilation database, in which case the client will be restarted via
|
|
|
|
|
// a different code path.
|
|
|
|
|
if (projectIsParsing(project))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ClangdClient * const client = clientForProject(project);
|
2021-11-01 13:25:48 +01:00
|
|
|
if (client && !m_clientsToRestart.contains(client)) {
|
2021-09-03 17:14:54 +02:00
|
|
|
m_clientsToRestart.append(client);
|
|
|
|
|
timer->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// It's unlikely that the same signal carries files from different projects,
|
|
|
|
|
// so we exit the loop as soon as we have dealt with one project, as the
|
|
|
|
|
// project look-up is not free.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 15:35:54 +01:00
|
|
|
void ClangModelManagerSupport::watchForInternalChanges()
|
|
|
|
|
{
|
|
|
|
|
connect(Core::DocumentManager::instance(), &Core::DocumentManager::filesChangedInternally,
|
|
|
|
|
this, [this](const Utils::FilePaths &filePaths) {
|
|
|
|
|
for (const Utils::FilePath &fp : filePaths) {
|
|
|
|
|
ClangdClient * const client = clientForFile(fp);
|
|
|
|
|
if (!client || client->documentForFilePath(fp))
|
|
|
|
|
continue;
|
|
|
|
|
client->openExtraFile(fp);
|
|
|
|
|
|
|
|
|
|
// We need to give clangd some time to start re-parsing the file.
|
|
|
|
|
// Closing right away does not work, and neither does doing it queued.
|
|
|
|
|
// If it turns out that this delay is not always enough, we'll need to come up
|
|
|
|
|
// with something more clever.
|
|
|
|
|
// Ideally, clangd would implement workspace/didChangeWatchedFiles; let's keep
|
|
|
|
|
// any eye on that.
|
|
|
|
|
QTimer::singleShot(5000, client, [client, fp] {
|
|
|
|
|
if (!client->documentForFilePath(fp))
|
|
|
|
|
client->closeExtraFile(fp); });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-18 09:21:35 +02:00
|
|
|
void ClangModelManagerSupport::onEditorOpened(Core::IEditor *editor)
|
2015-05-08 15:48:17 +02:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(editor, return);
|
|
|
|
|
Core::IDocument *document = editor->document();
|
|
|
|
|
QTC_ASSERT(document, return);
|
2018-11-04 21:44:21 +01:00
|
|
|
auto textDocument = qobject_cast<TextEditor::TextDocument *>(document);
|
2015-05-08 15:48:17 +02:00
|
|
|
|
2015-07-08 15:08:13 +02:00
|
|
|
if (textDocument && cppModelManager()->isCppEditor(editor)) {
|
2015-11-11 10:47:27 +01:00
|
|
|
connectToWidgetsMarkContextMenuRequested(editor->widget());
|
2015-05-08 15:48:17 +02:00
|
|
|
|
|
|
|
|
// TODO: Ensure that not fully loaded documents are updated?
|
2021-02-23 13:51:41 +01:00
|
|
|
|
2022-02-17 11:46:00 +01:00
|
|
|
ProjectExplorer::Project * project
|
2021-02-23 13:51:41 +01:00
|
|
|
= ProjectExplorer::SessionManager::projectForFile(document->filePath());
|
2022-03-29 15:47:10 +02:00
|
|
|
const ClangdSettings settings(ClangdProjectSettings(project).settings());
|
|
|
|
|
if (!settings.sizeIsOkay(textDocument->filePath()))
|
|
|
|
|
return;
|
2022-02-17 11:46:00 +01:00
|
|
|
if (!project)
|
|
|
|
|
project = fallbackProject();
|
2021-06-18 16:30:03 +02:00
|
|
|
if (ClangdClient * const client = clientForProject(project))
|
2021-09-15 09:42:41 +02:00
|
|
|
LanguageClientManager::openDocumentWithClient(textDocument, client);
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-18 09:21:35 +02:00
|
|
|
void ClangModelManagerSupport::onAbstractEditorSupportContentsUpdated(const QString &filePath,
|
2019-01-23 13:14:50 +01:00
|
|
|
const QString &,
|
2015-05-08 15:48:17 +02:00
|
|
|
const QByteArray &content)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!filePath.isEmpty(), return);
|
2016-11-21 13:30:26 +01:00
|
|
|
|
2018-10-19 10:20:27 +02:00
|
|
|
if (content.size() == 0)
|
|
|
|
|
return; // Generation not yet finished.
|
2022-05-31 16:35:10 +02:00
|
|
|
const auto fp = Utils::FilePath::fromString(filePath);
|
|
|
|
|
const QString stringContent = QString::fromUtf8(content);
|
|
|
|
|
if (Client * const client = clientForGeneratedFile(fp)) {
|
|
|
|
|
client->setShadowDocument(fp, stringContent);
|
|
|
|
|
ClangdClient::handleUiHeaderChange(fp.fileName());
|
|
|
|
|
QTC_CHECK(m_queuedShadowDocuments.remove(fp) == 0);
|
|
|
|
|
} else {
|
|
|
|
|
m_queuedShadowDocuments.insert(fp, stringContent);
|
|
|
|
|
}
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-18 09:21:35 +02:00
|
|
|
void ClangModelManagerSupport::onAbstractEditorSupportRemoved(const QString &filePath)
|
2015-05-08 15:48:17 +02:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!filePath.isEmpty(), return);
|
2016-11-21 13:30:26 +01:00
|
|
|
|
2022-05-31 16:35:10 +02:00
|
|
|
const auto fp = Utils::FilePath::fromString(filePath);
|
|
|
|
|
if (Client * const client = clientForGeneratedFile(fp)) {
|
|
|
|
|
client->removeShadowDocument(fp);
|
|
|
|
|
ClangdClient::handleUiHeaderChange(fp.fileName());
|
|
|
|
|
QTC_CHECK(m_queuedShadowDocuments.remove(fp) == 0);
|
|
|
|
|
} else {
|
|
|
|
|
m_queuedShadowDocuments.insert(fp, {});
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-24 18:26:09 +02:00
|
|
|
void addFixItsActionsToMenu(QMenu *menu, const TextEditor::QuickFixOperations &fixItOperations)
|
|
|
|
|
{
|
2022-05-20 10:54:09 +02:00
|
|
|
for (const TextEditor::QuickFixOperation::Ptr &fixItOperation : fixItOperations) {
|
2015-08-24 18:26:09 +02:00
|
|
|
QAction *action = menu->addAction(fixItOperation->description());
|
|
|
|
|
QObject::connect(action, &QAction::triggered, [fixItOperation]() {
|
|
|
|
|
fixItOperation->perform();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static TextEditor::AssistInterface createAssistInterface(TextEditor::TextEditorWidget *widget,
|
|
|
|
|
int lineNumber)
|
|
|
|
|
{
|
2022-05-13 10:05:45 +02:00
|
|
|
QTextCursor cursor(widget->document()->findBlockByLineNumber(lineNumber));
|
|
|
|
|
if (!cursor.atStart())
|
|
|
|
|
cursor.movePosition(QTextCursor::PreviousCharacter);
|
|
|
|
|
return TextEditor::AssistInterface(cursor,
|
2020-09-02 12:29:23 +02:00
|
|
|
widget->textDocument()->filePath(),
|
2015-08-24 18:26:09 +02:00
|
|
|
TextEditor::IdleEditor);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-18 09:21:35 +02:00
|
|
|
void ClangModelManagerSupport::onTextMarkContextMenuRequested(TextEditor::TextEditorWidget *widget,
|
2015-08-24 18:26:09 +02:00
|
|
|
int lineNumber,
|
|
|
|
|
QMenu *menu)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(widget, return);
|
|
|
|
|
QTC_ASSERT(lineNumber >= 1, return);
|
|
|
|
|
QTC_ASSERT(menu, return);
|
|
|
|
|
|
|
|
|
|
const auto filePath = widget->textDocument()->filePath().toString();
|
|
|
|
|
ClangEditorDocumentProcessor *processor = ClangEditorDocumentProcessor::get(filePath);
|
|
|
|
|
if (processor) {
|
|
|
|
|
const auto assistInterface = createAssistInterface(widget, lineNumber);
|
|
|
|
|
const auto fixItOperations = processor->extraRefactoringOperations(assistInterface);
|
|
|
|
|
|
|
|
|
|
addFixItsActionsToMenu(menu, fixItOperations);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 16:57:18 +01:00
|
|
|
using ClangEditorDocumentProcessors = QVector<ClangEditorDocumentProcessor *>;
|
|
|
|
|
static ClangEditorDocumentProcessors clangProcessors()
|
|
|
|
|
{
|
|
|
|
|
ClangEditorDocumentProcessors result;
|
2022-05-20 10:54:09 +02:00
|
|
|
for (const CppEditorDocumentHandle *editorDocument : cppModelManager()->cppEditorDocuments())
|
2018-01-19 16:57:18 +01:00
|
|
|
result.append(qobject_cast<ClangEditorDocumentProcessor *>(editorDocument->processor()));
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-18 09:21:35 +02:00
|
|
|
void ClangModelManagerSupport::onProjectPartsUpdated(ProjectExplorer::Project *project)
|
2015-05-08 15:48:17 +02:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(project, return);
|
2021-08-30 10:58:08 +02:00
|
|
|
const CppEditor::ProjectInfo::ConstPtr projectInfo = cppModelManager()->projectInfo(project);
|
2021-05-07 16:10:07 +02:00
|
|
|
QTC_ASSERT(projectInfo, return);
|
2015-11-06 16:58:24 +01:00
|
|
|
|
2021-02-23 13:51:41 +01:00
|
|
|
updateLanguageClient(project, projectInfo);
|
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
QStringList projectPartIds;
|
2021-08-30 10:58:08 +02:00
|
|
|
for (const CppEditor::ProjectPart::ConstPtr &projectPart : projectInfo->projectParts())
|
2018-09-25 09:41:32 +02:00
|
|
|
projectPartIds.append(projectPart->id());
|
|
|
|
|
onProjectPartsRemoved(projectPartIds);
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-18 09:21:35 +02:00
|
|
|
void ClangModelManagerSupport::onProjectPartsRemoved(const QStringList &projectPartIds)
|
2015-05-08 15:48:17 +02:00
|
|
|
{
|
2018-09-25 09:41:32 +02:00
|
|
|
if (!projectPartIds.isEmpty())
|
|
|
|
|
reinitializeBackendDocuments(projectPartIds);
|
2015-09-18 12:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-01 04:17:54 +02:00
|
|
|
void ClangModelManagerSupport::onClangdSettingsChanged()
|
|
|
|
|
{
|
|
|
|
|
for (ProjectExplorer::Project * const project : ProjectExplorer::SessionManager::projects()) {
|
2021-08-30 10:58:08 +02:00
|
|
|
const CppEditor::ClangdSettings settings(
|
|
|
|
|
CppEditor::ClangdProjectSettings(project).settings());
|
2021-07-01 04:17:54 +02:00
|
|
|
ClangdClient * const client = clientForProject(project);
|
|
|
|
|
if (!client) {
|
|
|
|
|
if (settings.useClangd())
|
|
|
|
|
updateLanguageClient(project, cppModelManager()->projectInfo(project));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!settings.useClangd()) {
|
|
|
|
|
LanguageClientManager::shutdownClient(client);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (client->settingsData() != settings.data())
|
|
|
|
|
updateLanguageClient(project, cppModelManager()->projectInfo(project));
|
|
|
|
|
}
|
2021-07-14 17:07:34 +02:00
|
|
|
|
|
|
|
|
ClangdClient * const fallbackClient = clientForProject(nullptr);
|
2021-08-12 09:39:02 +02:00
|
|
|
const ClangdSettings &settings = ClangdSettings::instance();
|
2021-07-14 17:07:34 +02:00
|
|
|
const auto startNewFallbackClient = [this] {
|
|
|
|
|
claimNonProjectSources(createClient(nullptr, {}));
|
|
|
|
|
};
|
|
|
|
|
if (!fallbackClient) {
|
|
|
|
|
if (settings.useClangd())
|
|
|
|
|
startNewFallbackClient();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!settings.useClangd()) {
|
|
|
|
|
LanguageClientManager::shutdownClient(fallbackClient);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (fallbackClient->settingsData() != settings.data()) {
|
|
|
|
|
LanguageClientManager::shutdownClient(fallbackClient);
|
|
|
|
|
startNewFallbackClient();
|
|
|
|
|
}
|
2021-07-01 04:17:54 +02:00
|
|
|
}
|
|
|
|
|
|
2018-01-19 16:57:18 +01:00
|
|
|
static ClangEditorDocumentProcessors
|
|
|
|
|
clangProcessorsWithProjectParts(const QStringList &projectPartIds)
|
|
|
|
|
{
|
|
|
|
|
return ::Utils::filtered(clangProcessors(), [projectPartIds](ClangEditorDocumentProcessor *p) {
|
|
|
|
|
return p->hasProjectPart() && projectPartIds.contains(p->projectPart()->id());
|
|
|
|
|
});
|
2015-09-18 12:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-18 09:21:35 +02:00
|
|
|
void ClangModelManagerSupport::reinitializeBackendDocuments(const QStringList &projectPartIds)
|
2015-09-18 12:19:46 +02:00
|
|
|
{
|
2022-05-20 10:54:09 +02:00
|
|
|
const ClangEditorDocumentProcessors processors = clangProcessorsWithProjectParts(projectPartIds);
|
|
|
|
|
for (ClangEditorDocumentProcessor *processor : processors) {
|
2015-09-18 12:19:46 +02:00
|
|
|
processor->clearProjectPart();
|
|
|
|
|
processor->run();
|
|
|
|
|
}
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-18 09:21:35 +02:00
|
|
|
ClangModelManagerSupport *ClangModelManagerSupport::instance()
|
2015-05-08 15:48:17 +02:00
|
|
|
{
|
2016-11-21 13:30:26 +01:00
|
|
|
return m_instance;
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-12 09:39:02 +02:00
|
|
|
} // Internal
|
|
|
|
|
} // ClangCodeModel
|