ClangCodeModel: Allow users to set a file size threshold for clangd

With huge source files it might not be so useful to continuously
recompile them while editing, which is basically what clangd does.
Let users opt out.

Change-Id: If3e95c1e286090606a84961d071179f8b40f9180
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-03-29 15:47:10 +02:00
parent f91a9cb891
commit 1f0ea9ef7f
4 changed files with 51 additions and 2 deletions

View File

@@ -367,8 +367,11 @@ void ClangModelManagerSupport::updateLanguageClient(
// Acquaint the client with all open C++ documents for this project.
bool hasDocuments = false;
const ClangdSettings settings(ClangdProjectSettings(project).settings());
for (TextEditor::TextDocument * const doc : allCppDocuments()) {
const Client * const currentClient = LanguageClientManager::clientForDocument(doc);
if (!settings.sizeIsOkay(doc->filePath()))
continue;
if (!currentClient || !currentClient->project()
|| currentClient->state() != Client::Initialized
|| project->isKnownFile(doc->filePath())) {
@@ -466,6 +469,8 @@ void ClangModelManagerSupport::claimNonProjectSources(ClangdClient *client)
continue;
}
ClangEditorDocumentProcessor::clearTextMarks(doc->filePath());
if (!ClangdSettings::instance().sizeIsOkay(doc->filePath()))
continue;
client->openDocument(doc);
}
}
@@ -568,6 +573,9 @@ void ClangModelManagerSupport::onEditorOpened(Core::IEditor *editor)
ProjectExplorer::Project * project
= ProjectExplorer::SessionManager::projectForFile(document->filePath());
const ClangdSettings settings(ClangdProjectSettings(project).settings());
if (!settings.sizeIsOkay(textDocument->filePath()))
return;
if (!project)
project = fallbackProject();
if (ClangdClient * const client = clientForProject(project))

View File

@@ -78,6 +78,8 @@ static QString clangdIndexingKey() { return QLatin1String("ClangdIndexing"); }
static QString clangdHeaderInsertionKey() { return QLatin1String("ClangdHeaderInsertion"); }
static QString clangdThreadLimitKey() { return QLatin1String("ClangdThreadLimit"); }
static QString clangdDocumentThresholdKey() { return QLatin1String("ClangdDocumentThreshold"); }
static QString clangdSizeThresholdEnabledKey() { return QLatin1String("ClangdSizeThresholdEnabled"); }
static QString clangdSizeThresholdKey() { return QLatin1String("ClangdSizeThreshold"); }
static QString clangdUseGlobalSettingsKey() { return QLatin1String("useGlobalSettings"); }
static QString sessionsWithOneClangdKey() { return QLatin1String("SessionsWithOneClangd"); }
@@ -350,6 +352,11 @@ FilePath ClangdSettings::clangdFilePath() const
return fallbackClangdFilePath();
}
bool ClangdSettings::sizeIsOkay(const Utils::FilePath &fp) const
{
return !sizeThresholdEnabled() || sizeThresholdInKb() * 1024 >= fp.fileSize();
}
ClangdSettings::Granularity ClangdSettings::granularity() const
{
if (m_data.sessionsWithOneClangd.contains(ProjectExplorer::SessionManager::activeSession()))
@@ -479,6 +486,8 @@ QVariantMap ClangdSettings::Data::toMap() const
map.insert(clangdHeaderInsertionKey(), autoIncludeHeaders);
map.insert(clangdThreadLimitKey(), workerThreadLimit);
map.insert(clangdDocumentThresholdKey(), documentUpdateThreshold);
map.insert(clangdSizeThresholdEnabledKey(), sizeThresholdEnabled);
map.insert(clangdSizeThresholdKey(), sizeThresholdInKb);
map.insert(sessionsWithOneClangdKey(), sessionsWithOneClangd);
return map;
}
@@ -491,6 +500,8 @@ void ClangdSettings::Data::fromMap(const QVariantMap &map)
autoIncludeHeaders = map.value(clangdHeaderInsertionKey(), false).toBool();
workerThreadLimit = map.value(clangdThreadLimitKey(), 0).toInt();
documentUpdateThreshold = map.value(clangdDocumentThresholdKey(), 500).toInt();
sizeThresholdEnabled = map.value(clangdSizeThresholdEnabledKey(), false).toBool();
sizeThresholdInKb = map.value(clangdSizeThresholdKey(), 1024).toLongLong();
sessionsWithOneClangd = map.value(sessionsWithOneClangdKey()).toStringList();
}

View File

@@ -115,17 +115,21 @@ public:
&& s1.workerThreadLimit == s2.workerThreadLimit
&& s1.enableIndexing == s2.enableIndexing
&& s1.autoIncludeHeaders == s2.autoIncludeHeaders
&& s1.documentUpdateThreshold == s2.documentUpdateThreshold;
&& s1.documentUpdateThreshold == s2.documentUpdateThreshold
&& s1.sizeThresholdEnabled == s2.sizeThresholdEnabled
&& s1.sizeThresholdInKb == s2.sizeThresholdInKb;
}
friend bool operator!=(const Data &s1, const Data &s2) { return !(s1 == s2); }
Utils::FilePath executableFilePath;
QStringList sessionsWithOneClangd;
int workerThreadLimit = 0;
int documentUpdateThreshold = 500;
qint64 sizeThresholdInKb = 1024;
bool useClangd = true;
bool enableIndexing = true;
bool autoIncludeHeaders = false;
int documentUpdateThreshold = 500;
bool sizeThresholdEnabled = false;
};
ClangdSettings(const Data &data) : m_data(data) {}
@@ -139,6 +143,9 @@ public:
bool autoIncludeHeaders() const { return m_data.autoIncludeHeaders; }
int workerThreadLimit() const { return m_data.workerThreadLimit; }
int documentUpdateThreshold() const { return m_data.documentUpdateThreshold; }
qint64 sizeThresholdInKb() const { return m_data.sizeThresholdInKb; }
bool sizeThresholdEnabled() const { return m_data.sizeThresholdEnabled; }
bool sizeIsOkay(const Utils::FilePath &fp) const;
enum class Granularity { Project, Session };
Granularity granularity() const;

View File

@@ -50,6 +50,8 @@
#include <QVBoxLayout>
#include <QVersionNumber>
#include <limits>
namespace CppEditor::Internal {
class CppCodeModelSettingsWidget final : public Core::IOptionsPageWidget
@@ -206,8 +208,10 @@ public:
QCheckBox useClangdCheckBox;
QCheckBox indexingCheckBox;
QCheckBox autoIncludeHeadersCheckBox;
QCheckBox sizeThresholdCheckBox;
QSpinBox threadLimitSpinBox;
QSpinBox documentUpdateThreshold;
QSpinBox sizeThresholdSpinBox;
Utils::PathChooser clangdChooser;
Utils::InfoLabel versionWarningLabel;
QGroupBox *sessionsGroupBox = nullptr;
@@ -243,6 +247,15 @@ ClangdSettingsWidget::ClangdSettingsWidget(const ClangdSettings::Data &settingsD
tr("Defines the amount of time Qt Creator waits before sending document changes to the "
"server.\n"
"If the document changes again while waiting, this timeout resets.\n"));
d->sizeThresholdCheckBox.setText(tr("Ignore files greater than"));
d->sizeThresholdCheckBox.setChecked(settings.sizeThresholdEnabled());
d->sizeThresholdSpinBox.setMinimum(1);
d->sizeThresholdSpinBox.setMaximum(std::numeric_limits<int>::max());
d->sizeThresholdSpinBox.setSuffix(" KB");
d->sizeThresholdSpinBox.setValue(settings.sizeThresholdInKb());
d->sizeThresholdSpinBox.setToolTip(tr(
"Files greater than this will not be opened as documents in clangd.\n"
"The built-in code model will handle highlighting, completion and so on."));
const auto layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
@@ -265,6 +278,10 @@ ClangdSettingsWidget::ClangdSettingsWidget(const ClangdSettings::Data &settingsD
documentUpdateThresholdLayout->addStretch(1);
const auto documentUpdateThresholdLabel = new QLabel(tr("Document update threshold:"));
formLayout->addRow(documentUpdateThresholdLabel, documentUpdateThresholdLayout);
const auto sizeThresholdLayout = new QHBoxLayout;
sizeThresholdLayout->addWidget(&d->sizeThresholdSpinBox);
sizeThresholdLayout->addStretch(1);
formLayout->addRow(&d->sizeThresholdCheckBox, sizeThresholdLayout);
layout->addLayout(formLayout);
if (!isForProject) {
@@ -381,6 +398,10 @@ ClangdSettingsWidget::ClangdSettingsWidget(const ClangdSettings::Data &settingsD
this, &ClangdSettingsWidget::settingsDataChanged);
connect(&d->threadLimitSpinBox, qOverload<int>(&QSpinBox::valueChanged),
this, &ClangdSettingsWidget::settingsDataChanged);
connect(&d->sizeThresholdCheckBox, &QCheckBox::toggled,
this, &ClangdSettingsWidget::settingsDataChanged);
connect(&d->sizeThresholdSpinBox, qOverload<int>(&QSpinBox::valueChanged),
this, &ClangdSettingsWidget::settingsDataChanged);
connect(&d->clangdChooser, &Utils::PathChooser::pathChanged,
this, &ClangdSettingsWidget::settingsDataChanged);
}
@@ -399,6 +420,8 @@ ClangdSettings::Data ClangdSettingsWidget::settingsData() const
data.autoIncludeHeaders = d->autoIncludeHeadersCheckBox.isChecked();
data.workerThreadLimit = d->threadLimitSpinBox.value();
data.documentUpdateThreshold = d->documentUpdateThreshold.value();
data.sizeThresholdEnabled = d->sizeThresholdCheckBox.isChecked();
data.sizeThresholdInKb = d->sizeThresholdSpinBox.value();
data.sessionsWithOneClangd = d->sessionsModel.stringList();
return data;
}