Clangd: Allow to switch off background indexing

It is conceivable that users don't want to spend the extra CPU time on
this.

Change-Id: Ic3611c8d17d201ae986fad08b344369a8728ce1b
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2021-06-25 17:40:20 +02:00
parent ba138a1855
commit 876cd8e975
4 changed files with 23 additions and 2 deletions

View File

@@ -382,8 +382,11 @@ public:
static BaseClientInterface *clientInterface(const Utils::FilePath &jsonDbDir) static BaseClientInterface *clientInterface(const Utils::FilePath &jsonDbDir)
{ {
QString indexingOption = "--background-index";
if (!CppTools::ClangdSettings::indexingEnabled())
indexingOption += "=0";
Utils::CommandLine cmd{CppTools::ClangdSettings::clangdFilePath(), Utils::CommandLine cmd{CppTools::ClangdSettings::clangdFilePath(),
{"--background-index", "--limit-results=0"}}; {indexingOption, "--limit-results=0"}};
if (!jsonDbDir.isEmpty()) if (!jsonDbDir.isEmpty())
cmd.addArg("--compile-commands-dir=" + jsonDbDir.toString()); cmd.addArg("--compile-commands-dir=" + jsonDbDir.toString());
if (clangdLog().isDebugEnabled()) if (clangdLog().isDebugEnabled())

View File

@@ -65,6 +65,7 @@ static QString indexerFileSizeLimitKey()
static QString useClangdKey() { return QLatin1String("UseClangd"); } static QString useClangdKey() { return QLatin1String("UseClangd"); }
static QString clangdPathKey() { return QLatin1String("ClangdPath"); } static QString clangdPathKey() { return QLatin1String("ClangdPath"); }
static QString clangdIndexingKey() { return QLatin1String("ClangdIndexing"); }
static FilePath g_defaultClangdFilePath; static FilePath g_defaultClangdFilePath;
static FilePath fallbackClangdFilePath() static FilePath fallbackClangdFilePath()
@@ -300,7 +301,9 @@ void CppCodeModelSettings::setEnableLowerClazyLevels(bool yesno)
static bool operator==(const ClangdSettings::Data &s1, const ClangdSettings::Data &s2) static bool operator==(const ClangdSettings::Data &s1, const ClangdSettings::Data &s2)
{ {
return s1.useClangd == s2.useClangd && s1.executableFilePath == s2.executableFilePath; return s1.useClangd == s2.useClangd
&& s1.executableFilePath == s2.executableFilePath
&& s1.enableIndexing == s2.enableIndexing;
} }
static bool operator!=(const ClangdSettings::Data &s1, const ClangdSettings::Data &s2) static bool operator!=(const ClangdSettings::Data &s1, const ClangdSettings::Data &s2)
{ {
@@ -338,6 +341,7 @@ void ClangdSettings::loadSettings()
QSettings * const s = Core::ICore::settings(); QSettings * const s = Core::ICore::settings();
m_data.useClangd = s->value(useClangdKey(), false).toBool(); m_data.useClangd = s->value(useClangdKey(), false).toBool();
m_data.executableFilePath = FilePath::fromString(s->value(clangdPathKey()).toString()); m_data.executableFilePath = FilePath::fromString(s->value(clangdPathKey()).toString());
m_data.enableIndexing = s->value(clangdIndexingKey(), true).toBool();
} }
void ClangdSettings::saveSettings() void ClangdSettings::saveSettings()
@@ -345,6 +349,7 @@ void ClangdSettings::saveSettings()
QSettings * const s = Core::ICore::settings(); QSettings * const s = Core::ICore::settings();
s->setValue(useClangdKey(), useClangd()); s->setValue(useClangdKey(), useClangd());
s->setValue(clangdPathKey(), m_data.executableFilePath.toString()); s->setValue(clangdPathKey(), m_data.executableFilePath.toString());
s->setValue(clangdIndexingKey(), m_data.enableIndexing);
} }
#ifdef WITH_TESTS #ifdef WITH_TESTS

View File

@@ -103,6 +103,7 @@ public:
{ {
public: public:
bool useClangd = false; bool useClangd = false;
bool enableIndexing = true;
Utils::FilePath executableFilePath; Utils::FilePath executableFilePath;
}; };
@@ -110,6 +111,7 @@ public:
static void setDefaultClangdPath(const Utils::FilePath &filePath); static void setDefaultClangdPath(const Utils::FilePath &filePath);
static Utils::FilePath clangdFilePath(); static Utils::FilePath clangdFilePath();
static bool indexingEnabled() { return instance().m_data.enableIndexing; }
static void setData(const Data &data); static void setData(const Data &data);
static Data data() { return instance().m_data; } static Data data() { return instance().m_data; }

View File

@@ -205,18 +205,27 @@ public:
m_clangdChooser.setExpectedKind(Utils::PathChooser::ExistingCommand); m_clangdChooser.setExpectedKind(Utils::PathChooser::ExistingCommand);
m_clangdChooser.setFilePath(ClangdSettings::clangdFilePath()); m_clangdChooser.setFilePath(ClangdSettings::clangdFilePath());
m_clangdChooser.setEnabled(m_useClangdCheckBox.isChecked()); m_clangdChooser.setEnabled(m_useClangdCheckBox.isChecked());
m_indexingCheckBox.setChecked(ClangdSettings::indexingEnabled());
m_indexingCheckBox.setToolTip(tr(
"If background indexing is enabled, global symbol searches will yield\n"
"more accurate results, at the cost of additional CPU load when\n"
"the project is first opened."));
const auto layout = new QVBoxLayout(this); const auto layout = new QVBoxLayout(this);
layout->addWidget(&m_useClangdCheckBox); layout->addWidget(&m_useClangdCheckBox);
const auto formLayout = new QFormLayout; const auto formLayout = new QFormLayout;
const auto chooserLabel = new QLabel(tr("Path to executable:")); const auto chooserLabel = new QLabel(tr("Path to executable:"));
formLayout->addRow(chooserLabel, &m_clangdChooser); formLayout->addRow(chooserLabel, &m_clangdChooser);
const auto indexingLabel = new QLabel(tr("Enable background indexing:"));
formLayout->addRow(indexingLabel, &m_indexingCheckBox);
layout->addLayout(formLayout); layout->addLayout(formLayout);
layout->addStretch(1); layout->addStretch(1);
const auto toggleEnabled = [=](const bool checked) { const auto toggleEnabled = [=](const bool checked) {
chooserLabel->setEnabled(checked); chooserLabel->setEnabled(checked);
m_clangdChooser.setEnabled(checked); m_clangdChooser.setEnabled(checked);
indexingLabel->setEnabled(checked);
m_indexingCheckBox.setEnabled(checked);
}; };
connect(&m_useClangdCheckBox, &QCheckBox::toggled, toggleEnabled); connect(&m_useClangdCheckBox, &QCheckBox::toggled, toggleEnabled);
toggleEnabled(m_useClangdCheckBox.isChecked()); toggleEnabled(m_useClangdCheckBox.isChecked());
@@ -228,10 +237,12 @@ private:
ClangdSettings::Data data; ClangdSettings::Data data;
data.useClangd = m_useClangdCheckBox.isChecked(); data.useClangd = m_useClangdCheckBox.isChecked();
data.executableFilePath = m_clangdChooser.filePath(); data.executableFilePath = m_clangdChooser.filePath();
data.enableIndexing = m_indexingCheckBox.isChecked();
ClangdSettings::setData(data); ClangdSettings::setData(data);
} }
QCheckBox m_useClangdCheckBox; QCheckBox m_useClangdCheckBox;
QCheckBox m_indexingCheckBox;
Utils::PathChooser m_clangdChooser; Utils::PathChooser m_clangdChooser;
}; };