From cfc789041636c5de703d9db6757799fafeff3a2d Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 1 Feb 2022 17:39:00 +0100 Subject: [PATCH] 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 --- src/plugins/clangcodemodel/clangdclient.cpp | 20 +++++++++++++++++++ src/plugins/clangcodemodel/clangdclient.h | 2 ++ .../clangmodelmanagersupport.cpp | 1 + 3 files changed, 23 insertions(+) diff --git a/src/plugins/clangcodemodel/clangdclient.cpp b/src/plugins/clangcodemodel/clangdclient.cpp index 36c58a7e21d..89271c75464 100644 --- a/src/plugins/clangcodemodel/clangdclient.cpp +++ b/src/plugins/clangcodemodel/clangdclient.cpp @@ -70,6 +70,7 @@ #include #include #include +#include #include #include #include @@ -86,6 +87,7 @@ #include #include #include +#include #include #include #include @@ -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"; diff --git a/src/plugins/clangcodemodel/clangdclient.h b/src/plugins/clangcodemodel/clangdclient.h index 68a35adbc13..2e4f1517002 100644 --- a/src/plugins/clangcodemodel/clangdclient.h +++ b/src/plugins/clangcodemodel/clangdclient.h @@ -42,6 +42,8 @@ namespace TextEditor { class BaseTextEditor; } namespace ClangCodeModel { namespace Internal { +void setupClangdConfigFile(); + class ClangdClient : public LanguageClient::Client { Q_OBJECT diff --git a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp index 1d8a3f8af2e..1b6a1042321 100644 --- a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp +++ b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp @@ -113,6 +113,7 @@ ClangModelManagerSupport::ClangModelManagerSupport() watchForExternalChanges(); watchForInternalChanges(); + setupClangdConfigFile(); cppModelManager()->setCurrentDocumentFilter(std::make_unique()); cppModelManager()->setLocatorFilter(std::make_unique()); cppModelManager()->setClassesFilter(std::make_unique());