ClangCodeModel: Introduce dedicated class for clangd LSP client

Makes sense for modularization purposes, and it will soon get additional
functionality.

Change-Id: Ie8163d352fc408b4167ee2ce6147aa1fb19528eb
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2021-04-20 14:42:29 +02:00
parent ecafdb7543
commit e27b367bdc
6 changed files with 122 additions and 29 deletions

View File

@@ -19,6 +19,7 @@ add_qtc_plugin(ClangCodeModel
clangcompletionchunkstotextconverter.cpp clangcompletionchunkstotextconverter.h clangcompletionchunkstotextconverter.cpp clangcompletionchunkstotextconverter.h
clangcompletioncontextanalyzer.cpp clangcompletioncontextanalyzer.h clangcompletioncontextanalyzer.cpp clangcompletioncontextanalyzer.h
clangconstants.h clangconstants.h
clangdclient.cpp clangdclient.h
clangcurrentdocumentfilter.cpp clangcurrentdocumentfilter.h clangcurrentdocumentfilter.cpp clangcurrentdocumentfilter.h
clangdiagnosticfilter.cpp clangdiagnosticfilter.h clangdiagnosticfilter.cpp clangdiagnosticfilter.h
clangdiagnosticmanager.cpp clangdiagnosticmanager.h clangdiagnosticmanager.cpp clangdiagnosticmanager.h

View File

@@ -41,6 +41,7 @@ SOURCES += \
clanguiheaderondiskmanager.cpp \ clanguiheaderondiskmanager.cpp \
clangutils.cpp \ clangutils.cpp \
clangoverviewmodel.cpp \ clangoverviewmodel.cpp \
clangdclient.cpp
clanggloballocatorfilters.cpp clanggloballocatorfilters.cpp
HEADERS += \ HEADERS += \
@@ -81,6 +82,7 @@ HEADERS += \
clanguiheaderondiskmanager.h \ clanguiheaderondiskmanager.h \
clangutils.h \ clangutils.h \
clangoverviewmodel.h \ clangoverviewmodel.h \
clangdclient.h
clanggloballocatorfilters.h clanggloballocatorfilters.h
FORMS += clangprojectsettingswidget.ui FORMS += clangprojectsettingswidget.ui

View File

@@ -55,6 +55,8 @@ QtcPlugin {
"clangconstants.h", "clangconstants.h",
"clangcurrentdocumentfilter.cpp", "clangcurrentdocumentfilter.cpp",
"clangcurrentdocumentfilter.h", "clangcurrentdocumentfilter.h",
"clangdclient.cpp",
"clangdclient.h",
"clangdiagnosticfilter.cpp", "clangdiagnosticfilter.cpp",
"clangdiagnosticfilter.h", "clangdiagnosticfilter.h",
"clangdiagnosticmanager.cpp", "clangdiagnosticmanager.cpp",

View File

@@ -0,0 +1,71 @@
/****************************************************************************
**
** Copyright (C) 2021 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 "clangdclient.h"
#include <cpptools/cppcodemodelsettings.h>
#include <cpptools/cpptoolsreuse.h>
#include <languageclient/languageclientinterface.h>
using namespace LanguageClient;
namespace ClangCodeModel {
namespace Internal {
static Q_LOGGING_CATEGORY(clangdLog, "qtc.clangcodemodel.clangd", QtWarningMsg);
static BaseClientInterface *clientInterface(const Utils::FilePath &jsonDbDir)
{
QString clangdArgs = "--index --background-index --limit-results=0";
if (!jsonDbDir.isEmpty())
clangdArgs += " --compile-commands-dir=" + jsonDbDir.toString();
if (clangdLog().isDebugEnabled())
clangdArgs += " --log=verbose --pretty";
const auto interface = new StdIOClientInterface;
interface->setExecutable(CppTools::codeModelSettings()->clangdFilePath().toString());
interface->setArguments(clangdArgs);
return interface;
}
ClangdClient::ClangdClient(ProjectExplorer::Project *project, const Utils::FilePath &jsonDbDir)
: Client(clientInterface(jsonDbDir))
{
setName(tr("clangd"));
LanguageFilter langFilter;
langFilter.mimeTypes = QStringList{"text/x-chdr", "text/x-c++hdr", "text/x-c++src",
"text/x-objc++src", "text/x-objcsrc"};
setSupportedLanguage(langFilter);
LanguageServerProtocol::ClientCapabilities caps = Client::defaultClientCapabilities();
caps.clearExperimental();
caps.clearTextDocument();
setClientCapabilities(caps);
setLocatorsEnabled(false);
setDocumentActionsEnabled(false);
setCurrentProject(project);
start();
}
} // namespace Internal
} // namespace ClangCodeModel

View File

@@ -0,0 +1,43 @@
/****************************************************************************
**
** Copyright (C) 2021 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 <languageclient/client.h>
namespace ProjectExplorer { class Project; }
namespace ClangCodeModel {
namespace Internal {
class ClangdClient : public LanguageClient::Client
{
Q_OBJECT
public:
ClangdClient(ProjectExplorer::Project *project, const Utils::FilePath &jsonDbDir);
};
} // namespace Internal
} // namespace ClangCodeModel

View File

@@ -26,6 +26,7 @@
#include "clangmodelmanagersupport.h" #include "clangmodelmanagersupport.h"
#include "clangconstants.h" #include "clangconstants.h"
#include "clangdclient.h"
#include "clangeditordocumentprocessor.h" #include "clangeditordocumentprocessor.h"
#include "clangutils.h" #include "clangutils.h"
#include "clangfollowsymbol.h" #include "clangfollowsymbol.h"
@@ -47,8 +48,6 @@
#include <cpptools/editordocumenthandle.h> #include <cpptools/editordocumenthandle.h>
#include <cpptools/projectinfo.h> #include <cpptools/projectinfo.h>
#include <languageclient/client.h>
#include <languageclient/languageclientinterface.h>
#include <languageclient/languageclientmanager.h> #include <languageclient/languageclientmanager.h>
#include <texteditor/quickfix.h> #include <texteditor/quickfix.h>
@@ -74,7 +73,6 @@ using namespace ClangCodeModel;
using namespace ClangCodeModel::Internal; using namespace ClangCodeModel::Internal;
using namespace LanguageClient; using namespace LanguageClient;
static Q_LOGGING_CATEGORY(clangdLog, "qtc.clangcodemodel.clangd", QtWarningMsg);
static ClangModelManagerSupport *m_instance = nullptr; static ClangModelManagerSupport *m_instance = nullptr;
static CppTools::CppModelManager *cppModelManager() static CppTools::CppModelManager *cppModelManager()
@@ -242,8 +240,6 @@ void ClangModelManagerSupport::connectToWidgetsMarkContextMenuRequested(QWidget
} }
} }
static QString clientName() { return "ccm-clangd"; }
void ClangModelManagerSupport::updateLanguageClient(ProjectExplorer::Project *project, void ClangModelManagerSupport::updateLanguageClient(ProjectExplorer::Project *project,
const CppTools::ProjectInfo &projectInfo) const CppTools::ProjectInfo &projectInfo)
{ {
@@ -336,7 +332,7 @@ LanguageClient::Client *ClangModelManagerSupport::clientForProject(
const QList<Client *> clients = Utils::filtered( const QList<Client *> clients = Utils::filtered(
LanguageClientManager::clientsForProject(project), LanguageClientManager::clientsForProject(project),
[](const LanguageClient::Client *c) { [](const LanguageClient::Client *c) {
return c->name().startsWith(clientName()) return qobject_cast<const ClangdClient *>(c)
&& c->state() != Client::ShutdownRequested && c->state() != Client::ShutdownRequested
&& c->state() != Client::Shutdown; && c->state() != Client::Shutdown;
}); });
@@ -347,29 +343,7 @@ LanguageClient::Client *ClangModelManagerSupport::clientForProject(
Client *ClangModelManagerSupport::createClient(ProjectExplorer::Project *project, Client *ClangModelManagerSupport::createClient(ProjectExplorer::Project *project,
const Utils::FilePath &jsonDbDir) const Utils::FilePath &jsonDbDir)
{ {
QString clangdArgs = "--index --background-index --limit-results=0"; return new ClangdClient(project, jsonDbDir);
if (!jsonDbDir.isEmpty())
clangdArgs += " --compile-commands-dir=" + jsonDbDir.toString();
if (clangdLog().isDebugEnabled())
clangdArgs += " --log=verbose --pretty";
const auto clientInterface = new StdIOClientInterface;
clientInterface->setExecutable(CppTools::codeModelSettings()->clangdFilePath().toString());
clientInterface->setArguments(clangdArgs);
const auto client = new Client(clientInterface);
client->setName(clientName());
LanguageFilter langFilter;
langFilter.mimeTypes = QStringList{"text/x-chdr", "text/x-c++hdr", "text/x-c++src",
"text/x-objc++src", "text/x-objcsrc"};
client->setSupportedLanguage(langFilter);
LanguageServerProtocol::ClientCapabilities caps = Client::defaultClientCapabilities();
caps.clearExperimental();
caps.clearTextDocument();
client->setClientCapabilities(caps);
client->setLocatorsEnabled(false);
client->setDocumentActionsEnabled(false);
client->setCurrentProject(project);
client->start();
return client;
} }
void ClangModelManagerSupport::onEditorOpened(Core::IEditor *editor) void ClangModelManagerSupport::onEditorOpened(Core::IEditor *editor)