ClangCodeModel: Implement per-session mode for clangd

Fixes: QTCREATORBUG-26526
Change-Id: If9e018475b4e2f0557d9bf64ad9a7921c9dd6046
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-08-24 11:16:20 +02:00
parent eae5de0fa3
commit 2fc5dba1c3
14 changed files with 226 additions and 114 deletions

View File

@@ -59,7 +59,7 @@ void ClangCodeModelPlugin::generateCompilationDB()
baseDir = TemporaryDirectory::masterDirectoryFilePath(); baseDir = TemporaryDirectory::masterDirectoryFilePath();
QFuture<GenerateCompilationDbResult> task QFuture<GenerateCompilationDbResult> task
= Utils::runAsync(&Internal::generateCompilationDB, projectInfo, = Utils::runAsync(&Internal::generateCompilationDB, ProjectInfoList{projectInfo},
baseDir, CompilationDbPurpose::Project, baseDir, CompilationDbPurpose::Project,
warningsConfigForProject(target->project()), warningsConfigForProject(target->project()),
globalClangOptions(), globalClangOptions(),

View File

@@ -40,6 +40,7 @@
#include <languageserverprotocol/progresssupport.h> #include <languageserverprotocol/progresssupport.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/projecttree.h> #include <projectexplorer/projecttree.h>
#include <projectexplorer/session.h>
#include <projectexplorer/taskhub.h> #include <projectexplorer/taskhub.h>
#include <texteditor/codeassist/assistinterface.h> #include <texteditor/codeassist/assistinterface.h>
#include <texteditor/codeassist/iassistprocessor.h> #include <texteditor/codeassist/iassistprocessor.h>
@@ -372,10 +373,9 @@ ClangdClient::ClangdClient(Project *project, const Utils::FilePath &jsonDbDir)
setClientCapabilities(caps); setClientCapabilities(caps);
setLocatorsEnabled(false); setLocatorsEnabled(false);
setAutoRequestCodeActions(false); // clangd sends code actions inside diagnostics setAutoRequestCodeActions(false); // clangd sends code actions inside diagnostics
if (project) { setProgressTitleForToken(indexingToken(),
setProgressTitleForToken(indexingToken(), project ? tr("Indexing %1 with clangd").arg(project->displayName())
tr("Indexing %1 with clangd").arg(project->displayName())); : tr("Indexing session with clangd"));
}
setCurrentProject(project); setCurrentProject(project);
setDocumentChangeUpdateThreshold(d->settings.documentUpdateThreshold); setDocumentChangeUpdateThreshold(d->settings.documentUpdateThreshold);
setSymbolStringifier(displayNameFromDocumentSymbol); setSymbolStringifier(displayNameFromDocumentSymbol);
@@ -587,6 +587,15 @@ bool ClangdClient::referencesShadowFile(const TextEditor::TextDocument *doc,
return !includePos.isNull(); return !includePos.isNull();
} }
bool ClangdClient::fileBelongsToProject(const Utils::FilePath &filePath) const
{
if (CppEditor::ClangdSettings::instance().granularity()
== CppEditor::ClangdSettings::Granularity::Session) {
return SessionManager::projectForFile(filePath);
}
return Client::fileBelongsToProject(filePath);
}
RefactoringChangesData *ClangdClient::createRefactoringChangesBackend() const RefactoringChangesData *ClangdClient::createRefactoringChangesBackend() const
{ {
return new CppEditor::CppRefactoringChangesData( return new CppEditor::CppRefactoringChangesData(

View File

@@ -126,6 +126,7 @@ private:
LanguageClient::DiagnosticManager *createDiagnosticManager() override; LanguageClient::DiagnosticManager *createDiagnosticManager() override;
bool referencesShadowFile(const TextEditor::TextDocument *doc, bool referencesShadowFile(const TextEditor::TextDocument *doc,
const Utils::FilePath &candidate) override; const Utils::FilePath &candidate) override;
bool fileBelongsToProject(const Utils::FilePath &filePath) const override;
class Private; class Private;
class VirtualFunctionAssistProcessor; class VirtualFunctionAssistProcessor;

View File

@@ -68,6 +68,11 @@ static ProjectExplorer::Project *fallbackProject()
return ProjectExplorer::SessionManager::startupProject(); return ProjectExplorer::SessionManager::startupProject();
} }
static bool sessionModeEnabled()
{
return ClangdSettings::instance().granularity() == ClangdSettings::Granularity::Session;
}
static const QList<TextEditor::TextDocument *> allCppDocuments() static const QList<TextEditor::TextDocument *> allCppDocuments()
{ {
const auto isCppDocument = Utils::equal(&Core::IDocument::id, const auto isCppDocument = Utils::equal(&Core::IDocument::id,
@@ -77,9 +82,23 @@ static const QList<TextEditor::TextDocument *> allCppDocuments()
return Utils::qobject_container_cast<TextEditor::TextDocument *>(documents); return Utils::qobject_container_cast<TextEditor::TextDocument *>(documents);
} }
static const QList<ProjectExplorer::Project *> projectsForClient(const Client *client)
{
QList<ProjectExplorer::Project *> projects;
if (sessionModeEnabled()) {
for (ProjectExplorer::Project * const p : ProjectExplorer::SessionManager::projects()) {
if (ClangdProjectSettings(p).settings().useClangd)
projects << p;
}
} else if (client->project()) {
projects << client->project();
}
return projects;
}
static bool fileIsProjectBuildArtifact(const Client *client, const Utils::FilePath &filePath) static bool fileIsProjectBuildArtifact(const Client *client, const Utils::FilePath &filePath)
{ {
if (const auto p = client->project()) { for (const ProjectExplorer::Project * const p : projectsForClient(client)) {
if (const auto t = p->activeTarget()) { if (const auto t = p->activeTarget()) {
if (const auto bc = t->activeBuildConfiguration()) { if (const auto bc = t->activeBuildConfiguration()) {
if (filePath.isChildOf(bc->buildDirectory())) if (filePath.isChildOf(bc->buildDirectory()))
@@ -176,6 +195,8 @@ ClangModelManagerSupport::ClangModelManagerSupport()
connect(modelManager, &CppEditor::CppModelManager::projectPartsRemoved, connect(modelManager, &CppEditor::CppModelManager::projectPartsRemoved,
this, &ClangModelManagerSupport::onProjectPartsRemoved); this, &ClangModelManagerSupport::onProjectPartsRemoved);
connect(modelManager, &CppModelManager::fallbackProjectPartUpdated, this, [this] { connect(modelManager, &CppModelManager::fallbackProjectPartUpdated, this, [this] {
if (sessionModeEnabled())
return;
if (ClangdClient * const fallbackClient = clientForProject(nullptr)) { if (ClangdClient * const fallbackClient = clientForProject(nullptr)) {
LanguageClientManager::shutdownClient(fallbackClient); LanguageClientManager::shutdownClient(fallbackClient);
claimNonProjectSources(new ClangdClient(nullptr, {})); claimNonProjectSources(new ClangdClient(nullptr, {}));
@@ -184,7 +205,15 @@ ClangModelManagerSupport::ClangModelManagerSupport()
auto *sessionManager = ProjectExplorer::SessionManager::instance(); auto *sessionManager = ProjectExplorer::SessionManager::instance();
connect(sessionManager, &ProjectExplorer::SessionManager::projectRemoved, connect(sessionManager, &ProjectExplorer::SessionManager::projectRemoved,
this, [this] { claimNonProjectSources(clientForProject(fallbackProject())); }); this, [this] {
if (!sessionModeEnabled())
claimNonProjectSources(clientForProject(fallbackProject()));
});
connect(sessionManager, &ProjectExplorer::SessionManager::sessionLoaded,
this, [this] {
if (sessionModeEnabled())
onClangdSettingsChanged();
});
CppEditor::ClangdSettings::setDefaultClangdPath(Core::ICore::clangdExecutable(CLANG_BINDIR)); CppEditor::ClangdSettings::setDefaultClangdPath(Core::ICore::clangdExecutable(CLANG_BINDIR));
connect(&CppEditor::ClangdSettings::instance(), &CppEditor::ClangdSettings::changed, connect(&CppEditor::ClangdSettings::instance(), &CppEditor::ClangdSettings::changed,
@@ -331,38 +360,71 @@ void ClangModelManagerSupport::connectToWidgetsMarkContextMenuRequested(QWidget
} }
} }
void ClangModelManagerSupport::updateLanguageClient( static Utils::FilePath getJsonDbDir(const ProjectExplorer::Project *project)
ProjectExplorer::Project *project, const CppEditor::ProjectInfo::ConstPtr &projectInfo) {
static const QString dirName(".qtc_clangd");
if (!project) {
const QString sessionDirName = Utils::FileUtils::fileSystemFriendlyName(
ProjectExplorer::SessionManager::activeSession());
return Core::ICore::userResourcePath() / dirName / sessionDirName; // TODO: Make configurable?
}
if (const ProjectExplorer::Target * const target = project->activeTarget()) {
if (const ProjectExplorer::BuildConfiguration * const bc
= target->activeBuildConfiguration()) {
return bc->buildDirectory() / dirName;
}
}
return Utils::FilePath();
}
static bool isProjectDataUpToDate(
ProjectExplorer::Project *project, ProjectInfoList projectInfo,
const Utils::FilePath &jsonDbDir)
{
if (project && !ProjectExplorer::SessionManager::hasProject(project))
return false;
const ClangdSettings settings(ClangdProjectSettings(project).settings());
if (!settings.useClangd())
return false;
if (!sessionModeEnabled() && !project)
return false;
if (sessionModeEnabled() && project)
return false;
const ProjectInfoList newProjectInfo = project
? ProjectInfoList{CppModelManager::instance()->projectInfo(project)}
: CppModelManager::instance()->projectInfos();
if (newProjectInfo.size() != projectInfo.size())
return false;
for (int i = 0; i < projectInfo.size(); ++i) {
if (*projectInfo[i] != *newProjectInfo[i])
return false;
}
if (getJsonDbDir(project) != jsonDbDir)
return false;
return true;
}
void ClangModelManagerSupport::updateLanguageClient(ProjectExplorer::Project *project)
{ {
const ClangdSettings settings(ClangdProjectSettings(project).settings()); const ClangdSettings settings(ClangdProjectSettings(project).settings());
if (!settings.useClangd()) if (!settings.useClangd())
return; return;
const auto getJsonDbDir = [project] { ProjectInfoList projectInfo;
if (const ProjectExplorer::Target * const target = project->activeTarget()) { if (sessionModeEnabled()) {
if (const ProjectExplorer::BuildConfiguration * const bc project = nullptr;
= target->activeBuildConfiguration()) { projectInfo = CppModelManager::instance()->projectInfos();
return bc->buildDirectory() / ".qtc_clangd"; } else {
} projectInfo.append(CppModelManager::instance()->projectInfo(project));
} }
return Utils::FilePath();
};
const Utils::FilePath jsonDbDir = getJsonDbDir(); const Utils::FilePath jsonDbDir = getJsonDbDir(project);
if (jsonDbDir.isEmpty()) if (jsonDbDir.isEmpty())
return; return;
const auto generatorWatcher = new QFutureWatcher<GenerateCompilationDbResult>; const auto generatorWatcher = new QFutureWatcher<GenerateCompilationDbResult>;
connect(generatorWatcher, &QFutureWatcher<GenerateCompilationDbResult>::finished, connect(generatorWatcher, &QFutureWatcher<GenerateCompilationDbResult>::finished,
[this, project, projectInfo, getJsonDbDir, jsonDbDir, generatorWatcher] { [this, project, projectInfo, jsonDbDir, generatorWatcher] {
generatorWatcher->deleteLater(); generatorWatcher->deleteLater();
if (!ProjectExplorer::SessionManager::hasProject(project)) if (!isProjectDataUpToDate(project, projectInfo, jsonDbDir))
return;
if (!ClangdSettings(ClangdProjectSettings(project).settings()).useClangd())
return;
const CppEditor::ProjectInfo::ConstPtr newProjectInfo
= cppModelManager()->projectInfo(project);
if (!newProjectInfo || *newProjectInfo != *projectInfo)
return;
if (getJsonDbDir() != jsonDbDir)
return; return;
const GenerateCompilationDbResult result = generatorWatcher->result(); const GenerateCompilationDbResult result = generatorWatcher->result();
if (!result.error.isEmpty()) { if (!result.error.isEmpty()) {
@@ -382,19 +444,13 @@ void ClangModelManagerSupport::updateLanguageClient(
client, client,
[client] { updateParserConfig(client); }); [client] { updateParserConfig(client); });
connect(client, &Client::initialized, this, [this, client, project, projectInfo, jsonDbDir] { connect(client, &Client::initialized, this, [this, client, project, projectInfo, jsonDbDir] {
if (!isProjectDataUpToDate(project, projectInfo, jsonDbDir))
return;
using namespace ProjectExplorer; using namespace ProjectExplorer;
if (!SessionManager::hasProject(project))
return;
if (!CppEditor::ClangdProjectSettings(project).settings().useClangd)
return;
const CppEditor::ProjectInfo::ConstPtr newProjectInfo
= cppModelManager()->projectInfo(project);
if (!newProjectInfo || *newProjectInfo != *projectInfo)
return;
// Acquaint the client with all open C++ documents for this project. // Acquaint the client with all open C++ documents for this project or session.
bool hasDocuments = false;
const ClangdSettings settings(ClangdProjectSettings(project).settings()); const ClangdSettings settings(ClangdProjectSettings(project).settings());
bool hasDocuments = false;
for (TextEditor::TextDocument * const doc : allCppDocuments()) { for (TextEditor::TextDocument * const doc : allCppDocuments()) {
Client * const currentClient = LanguageClientManager::clientForDocument(doc); Client * const currentClient = LanguageClientManager::clientForDocument(doc);
if (currentClient == client) { if (currentClient == client) {
@@ -403,6 +459,13 @@ void ClangModelManagerSupport::updateLanguageClient(
} }
if (!settings.sizeIsOkay(doc->filePath())) if (!settings.sizeIsOkay(doc->filePath()))
continue; continue;
if (!project) {
if (currentClient)
currentClient->closeDocument(doc);
LanguageClientManager::openDocumentWithClient(doc, client);
hasDocuments = true;
continue;
}
const Project * const docProject = SessionManager::projectForFile(doc->filePath()); const Project * const docProject = SessionManager::projectForFile(doc->filePath());
if (currentClient && currentClient->project() if (currentClient && currentClient->project()
&& currentClient->project() != project && currentClient->project() != project
@@ -438,7 +501,11 @@ void ClangModelManagerSupport::updateLanguageClient(
// clangd oddity: Background indexing only starts after opening a random file. // clangd oddity: Background indexing only starts after opening a random file.
// TODO: changes to the compilation db do not seem to trigger re-indexing. // TODO: changes to the compilation db do not seem to trigger re-indexing.
// How to force it? // How to force it?
ProjectNode * const rootNode = project->rootProjectNode(); ProjectNode *rootNode = nullptr;
if (project)
rootNode = project->rootProjectNode();
else if (SessionManager::startupProject())
rootNode = SessionManager::startupProject()->rootProjectNode();
if (!rootNode) if (!rootNode)
return; return;
const Node * const cxxNode = rootNode->findNode([](Node *n) { const Node * const cxxNode = rootNode->findNode([](Node *n) {
@@ -456,8 +523,8 @@ void ClangModelManagerSupport::updateLanguageClient(
}); });
const Utils::FilePath includeDir = settings.clangdIncludePath(); const Utils::FilePath includeDir = settings.clangdIncludePath();
auto future = Utils::runAsync(&Internal::generateCompilationDB, projectInfo, jsonDbDir, auto future = Utils::runAsync(&Internal::generateCompilationDB, projectInfo,
CompilationDbPurpose::CodeModel, jsonDbDir, CompilationDbPurpose::CodeModel,
warningsConfigForProject(project), warningsConfigForProject(project),
globalClangOptions(), includeDir); globalClangOptions(), includeDir);
generatorWatcher->setFuture(future); generatorWatcher->setFuture(future);
@@ -465,6 +532,13 @@ void ClangModelManagerSupport::updateLanguageClient(
} }
ClangdClient *ClangModelManagerSupport::clientForProject(const ProjectExplorer::Project *project) ClangdClient *ClangModelManagerSupport::clientForProject(const ProjectExplorer::Project *project)
{
if (sessionModeEnabled())
project = nullptr;
return clientWithProject(project);
}
ClangdClient *ClangModelManagerSupport::clientWithProject(const ProjectExplorer::Project *project)
{ {
const QList<Client *> clients = Utils::filtered( const QList<Client *> clients = Utils::filtered(
LanguageClientManager::clientsForProject(project), LanguageClientManager::clientsForProject(project),
@@ -520,48 +594,52 @@ void ClangModelManagerSupport::claimNonProjectSources(ClangdClient *client)
// workflow, e.g. a git branch switch will hit at least one open file. // workflow, e.g. a git branch switch will hit at least one open file.
void ClangModelManagerSupport::watchForExternalChanges() void ClangModelManagerSupport::watchForExternalChanges()
{ {
const auto projectIsParsing = [](const ProjectExplorer::Project *project) { static const auto projectIsParsing = [](const ClangdClient *client) {
const ProjectExplorer::BuildSystem * const bs = project && project->activeTarget() for (const ProjectExplorer::Project * const p : projectsForClient(client)) {
? project->activeTarget()->buildSystem() : nullptr; const ProjectExplorer::BuildSystem * const bs = p && p->activeTarget()
return bs && (bs->isParsing() || bs->isWaitingForParse()); ? p->activeTarget()->buildSystem() : nullptr;
if (bs && (bs->isParsing() || bs->isWaitingForParse()))
return true;
}
return false;
}; };
const auto timer = new QTimer(this); const auto timer = new QTimer(this);
timer->setInterval(3000); timer->setInterval(3000);
connect(timer, &QTimer::timeout, this, [this, projectIsParsing] { connect(timer, &QTimer::timeout, this, [this] {
const auto clients = m_clientsToRestart; const auto clients = m_clientsToRestart;
m_clientsToRestart.clear(); m_clientsToRestart.clear();
for (ClangdClient * const client : clients) { for (ClangdClient * const client : clients) {
if (client && client->state() != Client::Shutdown if (client && client->state() != Client::Shutdown
&& client->state() != Client::ShutdownRequested && client->state() != Client::ShutdownRequested
&& !projectIsParsing(client->project())) { && !projectIsParsing(client)) {
ProjectExplorer::Project * const project = client->project(); updateLanguageClient(client->project());
updateLanguageClient(project, CppModelManager::instance()->projectInfo(project));
} }
} }
}); });
connect(Core::DocumentManager::instance(), &Core::DocumentManager::filesChangedExternally, connect(Core::DocumentManager::instance(), &Core::DocumentManager::filesChangedExternally,
this, [this, timer, projectIsParsing](const QSet<Utils::FilePath> &files) { this, [this, timer](const QSet<Utils::FilePath> &files) {
if (!LanguageClientManager::hasClients<ClangdClient>()) if (!LanguageClientManager::hasClients<ClangdClient>())
return; return;
for (const Utils::FilePath &file : files) { for (const Utils::FilePath &file : files) {
const ProjectFile::Kind kind = ProjectFile::classify(file.toString()); const ProjectFile::Kind kind = ProjectFile::classify(file.toString());
if (!ProjectFile::isSource(kind) && !ProjectFile::isHeader(kind)) if (!ProjectFile::isSource(kind) && !ProjectFile::isHeader(kind))
continue; continue;
const ProjectExplorer::Project * const project ProjectExplorer::Project * const project
= ProjectExplorer::SessionManager::projectForFile(file); = ProjectExplorer::SessionManager::projectForFile(file);
if (!project) if (!project)
continue; 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); ClangdClient * const client = clientForProject(project);
if (client && !m_clientsToRestart.contains(client)) { if (client && !m_clientsToRestart.contains(client)) {
// 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(client))
return;
m_clientsToRestart.append(client); m_clientsToRestart.append(client);
timer->start(); timer->start();
} }
@@ -607,14 +685,14 @@ void ClangModelManagerSupport::onEditorOpened(Core::IEditor *editor)
if (textDocument && cppModelManager()->isCppEditor(editor)) { if (textDocument && cppModelManager()->isCppEditor(editor)) {
connectToWidgetsMarkContextMenuRequested(editor->widget()); connectToWidgetsMarkContextMenuRequested(editor->widget());
// TODO: Ensure that not fully loaded documents are updated?
ProjectExplorer::Project * project ProjectExplorer::Project * project
= ProjectExplorer::SessionManager::projectForFile(document->filePath()); = ProjectExplorer::SessionManager::projectForFile(document->filePath());
const ClangdSettings settings(ClangdProjectSettings(project).settings()); const ClangdSettings settings(ClangdProjectSettings(project).settings());
if (!settings.sizeIsOkay(textDocument->filePath())) if (!settings.sizeIsOkay(textDocument->filePath()))
return; return;
if (!project) if (sessionModeEnabled())
project = nullptr;
else if (!project)
project = fallbackProject(); project = fallbackProject();
if (ClangdClient * const client = clientForProject(project)) if (ClangdClient * const client = clientForProject(project))
LanguageClientManager::openDocumentWithClient(textDocument, client); LanguageClientManager::openDocumentWithClient(textDocument, client);
@@ -706,12 +784,13 @@ static ClangEditorDocumentProcessors clangProcessors()
void ClangModelManagerSupport::onProjectPartsUpdated(ProjectExplorer::Project *project) void ClangModelManagerSupport::onProjectPartsUpdated(ProjectExplorer::Project *project)
{ {
QTC_ASSERT(project, return); QTC_ASSERT(project, return);
updateLanguageClient(project);
QStringList projectPartIds;
const CppEditor::ProjectInfo::ConstPtr projectInfo = cppModelManager()->projectInfo(project); const CppEditor::ProjectInfo::ConstPtr projectInfo = cppModelManager()->projectInfo(project);
QTC_ASSERT(projectInfo, return); QTC_ASSERT(projectInfo, return);
updateLanguageClient(project, projectInfo);
QStringList projectPartIds;
for (const CppEditor::ProjectPart::ConstPtr &projectPart : projectInfo->projectParts()) for (const CppEditor::ProjectPart::ConstPtr &projectPart : projectInfo->projectParts())
projectPartIds.append(projectPart->id()); projectPartIds.append(projectPart->id());
onProjectPartsRemoved(projectPartIds); onProjectPartsRemoved(projectPartIds);
@@ -725,13 +804,20 @@ void ClangModelManagerSupport::onProjectPartsRemoved(const QStringList &projectP
void ClangModelManagerSupport::onClangdSettingsChanged() void ClangModelManagerSupport::onClangdSettingsChanged()
{ {
const bool sessionMode = sessionModeEnabled();
for (ProjectExplorer::Project * const project : ProjectExplorer::SessionManager::projects()) { for (ProjectExplorer::Project * const project : ProjectExplorer::SessionManager::projects()) {
const CppEditor::ClangdSettings settings( const CppEditor::ClangdSettings settings(
CppEditor::ClangdProjectSettings(project).settings()); CppEditor::ClangdProjectSettings(project).settings());
ClangdClient * const client = clientForProject(project); ClangdClient * const client = clientWithProject(project);
if (sessionMode) {
if (client && client->project())
LanguageClientManager::shutdownClient(client);
continue;
}
if (!client) { if (!client) {
if (settings.useClangd()) if (settings.useClangd())
updateLanguageClient(project, cppModelManager()->projectInfo(project)); updateLanguageClient(project);
continue; continue;
} }
if (!settings.useClangd()) { if (!settings.useClangd()) {
@@ -739,26 +825,29 @@ void ClangModelManagerSupport::onClangdSettingsChanged()
continue; continue;
} }
if (client->settingsData() != settings.data()) if (client->settingsData() != settings.data())
updateLanguageClient(project, cppModelManager()->projectInfo(project)); updateLanguageClient(project);
} }
ClangdClient * const fallbackClient = clientForProject(nullptr); ClangdClient * const fallbackOrSessionClient = clientForProject(nullptr);
const ClangdSettings &settings = ClangdSettings::instance(); const auto startNewFallbackOrSessionClient = [this, sessionMode] {
const auto startNewFallbackClient = [this] { if (sessionMode)
claimNonProjectSources(new ClangdClient(nullptr, {})); updateLanguageClient(nullptr);
else
claimNonProjectSources(new ClangdClient(nullptr, {}));
}; };
if (!fallbackClient) { const ClangdSettings &settings = ClangdSettings::instance();
if (!fallbackOrSessionClient) {
if (settings.useClangd()) if (settings.useClangd())
startNewFallbackClient(); startNewFallbackOrSessionClient();
return; return;
} }
if (!settings.useClangd()) { if (!settings.useClangd()) {
LanguageClientManager::shutdownClient(fallbackClient); LanguageClientManager::shutdownClient(fallbackOrSessionClient);
return; return;
} }
if (fallbackClient->settingsData() != settings.data()) { if (fallbackOrSessionClient->settingsData() != settings.data()) {
LanguageClientManager::shutdownClient(fallbackClient); LanguageClientManager::shutdownClient(fallbackOrSessionClient);
startNewFallbackClient(); startNewFallbackOrSessionClient();
} }
} }

View File

@@ -81,11 +81,11 @@ private:
void connectTextDocumentToTranslationUnit(TextEditor::TextDocument *textDocument); void connectTextDocumentToTranslationUnit(TextEditor::TextDocument *textDocument);
void connectToWidgetsMarkContextMenuRequested(QWidget *editorWidget); void connectToWidgetsMarkContextMenuRequested(QWidget *editorWidget);
void updateLanguageClient(ProjectExplorer::Project *project, void updateLanguageClient(ProjectExplorer::Project *project);
const CppEditor::ProjectInfo::ConstPtr &projectInfo);
void claimNonProjectSources(ClangdClient *client); void claimNonProjectSources(ClangdClient *client);
void watchForExternalChanges(); void watchForExternalChanges();
void watchForInternalChanges(); void watchForInternalChanges();
static ClangdClient *clientWithProject(const ProjectExplorer::Project *project);
Utils::FutureSynchronizer m_generatorSynchronizer; Utils::FutureSynchronizer m_generatorSynchronizer;
QList<QPointer<ClangdClient>> m_clientsToRestart; QList<QPointer<ClangdClient>> m_clientsToRestart;

View File

@@ -143,17 +143,17 @@ static QJsonObject createFileObject(const FilePath &buildDir,
return fileObject; return fileObject;
} }
GenerateCompilationDbResult generateCompilationDB(const CppEditor::ProjectInfo::ConstPtr projectInfo, GenerateCompilationDbResult generateCompilationDB(QList<ProjectInfo::ConstPtr> projectInfoList,
const Utils::FilePath &baseDir, FilePath baseDir,
CompilationDbPurpose purpose, CompilationDbPurpose purpose,
const ClangDiagnosticConfig &warningsConfig, ClangDiagnosticConfig warningsConfig,
const QStringList &projectOptions, QStringList projectOptions,
const FilePath &clangIncludeDir) FilePath clangIncludeDir)
{ {
QTC_ASSERT(!baseDir.isEmpty(), return GenerateCompilationDbResult(QString(), QTC_ASSERT(!baseDir.isEmpty(), return GenerateCompilationDbResult(QString(),
QCoreApplication::translate("ClangUtils", "Could not retrieve build directory."))); QCoreApplication::translate("ClangUtils", "Could not retrieve build directory.")));
QTC_ASSERT(projectInfo, return GenerateCompilationDbResult(QString(), QTC_ASSERT(!projectInfoList.isEmpty(),
"Could not retrieve project info.")); return GenerateCompilationDbResult(QString(), "Could not retrieve project info."));
QTC_CHECK(baseDir.ensureWritableDir()); QTC_CHECK(baseDir.ensureWritableDir());
QFile compileCommandsFile(baseDir.toString() + "/compile_commands.json"); QFile compileCommandsFile(baseDir.toString() + "/compile_commands.json");
const bool fileOpened = compileCommandsFile.open(QIODevice::WriteOnly | QIODevice::Truncate); const bool fileOpened = compileCommandsFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
@@ -166,23 +166,27 @@ GenerateCompilationDbResult generateCompilationDB(const CppEditor::ProjectInfo::
const UsePrecompiledHeaders usePch = getPchUsage(); const UsePrecompiledHeaders usePch = getPchUsage();
const QJsonArray jsonProjectOptions = QJsonArray::fromStringList(projectOptions); const QJsonArray jsonProjectOptions = QJsonArray::fromStringList(projectOptions);
for (ProjectPart::ConstPtr projectPart : projectInfo->projectParts()) { for (const ProjectInfo::ConstPtr &projectInfo : qAsConst(projectInfoList)) {
QStringList args; for (ProjectPart::ConstPtr projectPart : projectInfo->projectParts()) {
const CompilerOptionsBuilder optionsBuilder = clangOptionsBuilder( QTC_ASSERT(projectInfo, continue);
*projectPart, warningsConfig, clangIncludeDir); QStringList args;
QJsonArray ppOptions; const CompilerOptionsBuilder optionsBuilder = clangOptionsBuilder(
if (purpose == CompilationDbPurpose::Project) { *projectPart, warningsConfig, clangIncludeDir);
args = projectPartArguments(*projectPart); QJsonArray ppOptions;
} else { if (purpose == CompilationDbPurpose::Project) {
ppOptions = fullProjectPartOptions(projectPartOptions(optionsBuilder), jsonProjectOptions); args = projectPartArguments(*projectPart);
} } else {
for (const ProjectFile &projFile : projectPart->files) { ppOptions = fullProjectPartOptions(projectPartOptions(optionsBuilder),
const QJsonObject json = createFileObject(baseDir, args, *projectPart, projFile, jsonProjectOptions);
purpose, ppOptions, usePch, }
optionsBuilder.isClStyle()); for (const ProjectFile &projFile : projectPart->files) {
if (compileCommandsFile.size() > 1) const QJsonObject json = createFileObject(baseDir, args, *projectPart, projFile,
compileCommandsFile.write(","); purpose, ppOptions, usePch,
compileCommandsFile.write('\n' + QJsonDocument(json).toJson().trimmed()); optionsBuilder.isClStyle());
if (compileCommandsFile.size() > 1)
compileCommandsFile.write(",");
compileCommandsFile.write('\n' + QJsonDocument(json).toJson().trimmed());
}
} }
} }

View File

@@ -65,10 +65,10 @@ public:
}; };
enum class CompilationDbPurpose { Project, CodeModel }; enum class CompilationDbPurpose { Project, CodeModel };
GenerateCompilationDbResult generateCompilationDB(const CppEditor::ProjectInfo::ConstPtr projectInfo, GenerateCompilationDbResult generateCompilationDB(QList<CppEditor::ProjectInfo::ConstPtr> projectInfo,
const Utils::FilePath &baseDir, CompilationDbPurpose purpose, Utils::FilePath baseDir, CompilationDbPurpose purpose,
const CppEditor::ClangDiagnosticConfig &warningsConfig, const QStringList &projectOptions, CppEditor::ClangDiagnosticConfig warningsConfig, QStringList projectOptions,
const Utils::FilePath &clangIncludeDir); Utils::FilePath clangIncludeDir);
class DiagnosticTextInfo class DiagnosticTextInfo
{ {

View File

@@ -272,6 +272,10 @@ ClangdSettingsWidget::ClangdSettingsWidget(const ClangdSettings::Data &settingsD
innerSessionsLayout->addLayout(buttonsLayout); innerSessionsLayout->addLayout(buttonsLayout);
outerSessionsLayout->addWidget(d->sessionsGroupBox); outerSessionsLayout->addWidget(d->sessionsGroupBox);
outerSessionsLayout->addStretch(1); outerSessionsLayout->addStretch(1);
const auto separator = new QFrame;
separator->setFrameShape(QFrame::HLine);
layout->addWidget(separator);
layout->addLayout(outerSessionsLayout); layout->addLayout(outerSessionsLayout);
const auto updateRemoveButtonState = [removeButton, sessionsView] { const auto updateRemoveButtonState = [removeButton, sessionsView] {
@@ -303,9 +307,6 @@ ClangdSettingsWidget::ClangdSettingsWidget(const ClangdSettings::Data &settingsD
d->sessionsModel.sort(0); d->sessionsModel.sort(0);
} }
}); });
// TODO: Remove once the concept is functional.
d->sessionsGroupBox->hide();
} }
const auto configFilesHelpLabel = new QLabel; const auto configFilesHelpLabel = new QLabel;

View File

@@ -946,7 +946,7 @@ QFuture<void> CppModelManager::updateSourceFiles(const QSet<QString> &sourceFile
return d->m_internalIndexingSupport->refreshSourceFiles(filteredFiles, mode); return d->m_internalIndexingSupport->refreshSourceFiles(filteredFiles, mode);
} }
QList<ProjectInfo::ConstPtr> CppModelManager::projectInfos() const ProjectInfoList CppModelManager::projectInfos() const
{ {
QReadLocker locker(&d->m_projectLock); QReadLocker locker(&d->m_projectLock);
return Utils::transform<QList<ProjectInfo::ConstPtr>>(d->m_projectData, return Utils::transform<QList<ProjectInfo::ConstPtr>>(d->m_projectData,

View File

@@ -99,7 +99,7 @@ public:
const QList<Document::DiagnosticMessage> diagnosticMessages(); const QList<Document::DiagnosticMessage> diagnosticMessages();
QList<ProjectInfo::ConstPtr> projectInfos() const; ProjectInfoList projectInfos() const;
ProjectInfo::ConstPtr projectInfo(ProjectExplorer::Project *project) const; ProjectInfo::ConstPtr projectInfo(ProjectExplorer::Project *project) const;
QFuture<void> updateProjectInfo(const ProjectInfo::ConstPtr &newProjectInfo, QFuture<void> updateProjectInfo(const ProjectInfo::ConstPtr &newProjectInfo,
const QSet<QString> &additionalFiles = {}); const QSet<QString> &additionalFiles = {});

View File

@@ -13,6 +13,7 @@
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <QHash> #include <QHash>
#include <QList>
#include <QSet> #include <QSet>
#include <QVector> #include <QVector>
@@ -54,4 +55,6 @@ private:
const ProjectExplorer::Macros m_defines; const ProjectExplorer::Macros m_defines;
}; };
using ProjectInfoList = QList<ProjectInfo::ConstPtr>;
} // namespace CppEditor } // namespace CppEditor

View File

@@ -2051,6 +2051,11 @@ bool Client::referencesShadowFile(const TextEditor::TextDocument *doc,
return false; return false;
} }
bool Client::fileBelongsToProject(const Utils::FilePath &filePath) const
{
return project() && project()->isKnownFile(filePath);
}
} // namespace LanguageClient } // namespace LanguageClient
#include <client.moc> #include <client.moc>

View File

@@ -163,6 +163,7 @@ public:
void setCompletionAssistProvider(LanguageClientCompletionAssistProvider *provider); void setCompletionAssistProvider(LanguageClientCompletionAssistProvider *provider);
void setQuickFixAssistProvider(LanguageClientQuickFixProvider *provider); void setQuickFixAssistProvider(LanguageClientQuickFixProvider *provider);
virtual bool supportsDocumentSymbols(const TextEditor::TextDocument *doc) const; virtual bool supportsDocumentSymbols(const TextEditor::TextDocument *doc) const;
virtual bool fileBelongsToProject(const Utils::FilePath &filePath) const;
// logging // logging
enum class LogTarget { Console, Ui }; enum class LogTarget { Console, Ui };

View File

@@ -98,8 +98,7 @@ void DiagnosticManager::showDiagnostics(const DocumentUri &uri, int version)
if (versionedDiagnostics.version.value_or(version) == version if (versionedDiagnostics.version.value_or(version) == version
&& !versionedDiagnostics.diagnostics.isEmpty()) { && !versionedDiagnostics.diagnostics.isEmpty()) {
Marks &marks = m_marks[filePath]; Marks &marks = m_marks[filePath];
const bool isProjectFile = m_client->project() const bool isProjectFile = m_client->fileBelongsToProject(filePath);
&& m_client->project()->isKnownFile(filePath);
for (const Diagnostic &diagnostic : versionedDiagnostics.diagnostics) { for (const Diagnostic &diagnostic : versionedDiagnostics.diagnostics) {
const QTextEdit::ExtraSelection selection const QTextEdit::ExtraSelection selection
= createDiagnosticSelection(diagnostic, doc->document()); = createDiagnosticSelection(diagnostic, doc->document());