ClangCodeModel: Create a clangd config file, if none exists yet

There are some clangd settings that can only be provided via a config file
(i.e. neither via the command line nor via LSP extensions).
In addition, clangd is very picky about that file's location; in
particular, we cannot put it at the "pseudo project root" where our
generated compilation database resides.
Therefore, we have to go with a user config file (as we don't want to
write into any project source directories). If there already is such a
file, we don't do anything, based on the assumption that the user is
familiar with clangd and has already configured it to their liking.
Otherwise, we create the file, including a special marker that tells us
it was generated by us and can safely be overwritten.
The set of options we set is currently quite small, but we expect it to
grow in the future.

Change-Id: I1605bf7b0edecc844f3e7cdd1d7dddda7af33956
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-02-01 17:39:00 +01:00
parent 0e7de92319
commit cfc7890416
3 changed files with 23 additions and 0 deletions

View File

@@ -70,6 +70,7 @@
#include <texteditor/texteditorsettings.h>
#include <texteditor/texteditor.h>
#include <utils/algorithm.h>
#include <utils/fileutils.h>
#include <utils/itemviews.h>
#include <utils/runextensions.h>
#include <utils/treemodel.h>
@@ -86,6 +87,7 @@
#include <QPair>
#include <QPointer>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QVBoxLayout>
#include <QWidget>
#include <QtConcurrent>
@@ -598,6 +600,24 @@ public:
: Request("textDocument/symbolInfo", params) {}
};
void setupClangdConfigFile()
{
const Utils::FilePath baseDir = Utils::FilePath::fromString(
QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation)) / "clangd";
baseDir.ensureWritableDir();
const Utils::FilePath targetConfigFile = baseDir / "config.yaml";
Utils::FileReader configReader;
const QByteArray firstLine = "# This file was generated by Qt Creator and will be overwritten "
"unless you remove this line.";
if (!configReader.fetch(targetConfigFile) || configReader.data().startsWith(firstLine)) {
Utils::FileSaver saver(targetConfigFile);
saver.write(firstLine + '\n');
saver.write("Hover:\n");
saver.write(" ShowAKA: Yes\n");
QTC_CHECK(saver.finalize());
}
}
static BaseClientInterface *clientInterface(Project *project, const Utils::FilePath &jsonDbDir)
{
QString indexingOption = "--background-index";

View File

@@ -42,6 +42,8 @@ namespace TextEditor { class BaseTextEditor; }
namespace ClangCodeModel {
namespace Internal {
void setupClangdConfigFile();
class ClangdClient : public LanguageClient::Client
{
Q_OBJECT

View File

@@ -113,6 +113,7 @@ ClangModelManagerSupport::ClangModelManagerSupport()
watchForExternalChanges();
watchForInternalChanges();
setupClangdConfigFile();
cppModelManager()->setCurrentDocumentFilter(std::make_unique<ClangdCurrentDocumentFilter>());
cppModelManager()->setLocatorFilter(std::make_unique<ClangGlobalSymbolFilter>());
cppModelManager()->setClassesFilter(std::make_unique<ClangClassesFilter>());