ClangCodeModel: Allow users to choose between completion ranking models

... in clangd.

Task-number: QTCREATORBUG-29013
Change-Id: Idd80a195709e9813f1713a048f6229a7dd6493ba
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2023-10-13 13:50:21 +02:00
parent 43cfe48fed
commit 0bfa8fd718
4 changed files with 66 additions and 0 deletions

View File

@@ -217,6 +217,10 @@ static BaseClientInterface *clientInterface(Project *project, const Utils::FileP
if (clangdLogServer().isDebugEnabled())
cmd.addArgs({"--log=verbose", "--pretty", "--hidden-features=1"});
cmd.addArg("--use-dirty-headers");
if (settings.completionRankingModel() != ClangdSettings::CompletionRankingModel::Default) {
cmd.addArg("--ranking-model=" + ClangdSettings::rankingModelToCmdLineString(
settings.completionRankingModel()));
}
const auto interface = new StdIOClientInterface;
interface->setCommandLine(cmd);
return interface;

View File

@@ -47,6 +47,7 @@ static Key clangdPathKey() { return "ClangdPath"; }
static Key clangdIndexingKey() { return "ClangdIndexing"; }
static Key clangdIndexingPriorityKey() { return "ClangdIndexingPriority"; }
static Key clangdHeaderSourceSwitchModeKey() { return "ClangdHeaderSourceSwitchMode"; }
static Key clangdCompletionRankingModelKey() { return "ClangdCompletionRankingModel"; }
static Key clangdHeaderInsertionKey() { return "ClangdHeaderInsertion"; }
static Key clangdThreadLimitKey() { return "ClangdThreadLimit"; }
static Key clangdDocumentThresholdKey() { return "ClangdDocumentThreshold"; }
@@ -222,6 +223,26 @@ QString ClangdSettings::headerSourceSwitchModeToDisplayString(HeaderSourceSwitch
return {};
}
QString ClangdSettings::rankingModelToCmdLineString(CompletionRankingModel model)
{
switch (model) {
case CompletionRankingModel::Default: break;
case CompletionRankingModel::DecisionForest: return "decision_forest";
case CompletionRankingModel::Heuristics: return "heuristics";
}
QTC_ASSERT(false, return {});
}
QString ClangdSettings::rankingModelToDisplayString(CompletionRankingModel model)
{
switch (model) {
case CompletionRankingModel::Default: return Tr::tr("Default");
case CompletionRankingModel::DecisionForest: return Tr::tr("Decision Forest");
case CompletionRankingModel::Heuristics: return Tr::tr("Heuristics");
}
QTC_ASSERT(false, return {});
}
ClangdSettings &ClangdSettings::instance()
{
static ClangdSettings settings;
@@ -527,6 +548,7 @@ Store ClangdSettings::Data::toMap() const
map.insert(clangdIndexingKey(), indexingPriority != IndexingPriority::Off);
map.insert(clangdIndexingPriorityKey(), int(indexingPriority));
map.insert(clangdHeaderSourceSwitchModeKey(), int(headerSourceSwitchMode));
map.insert(clangdCompletionRankingModelKey(), int(completionRankingModel));
map.insert(clangdHeaderInsertionKey(), autoIncludeHeaders);
map.insert(clangdThreadLimitKey(), workerThreadLimit);
map.insert(clangdDocumentThresholdKey(), documentUpdateThreshold);
@@ -550,6 +572,8 @@ void ClangdSettings::Data::fromMap(const Store &map)
indexingPriority = IndexingPriority::Off;
headerSourceSwitchMode = HeaderSourceSwitchMode(map.value(clangdHeaderSourceSwitchModeKey(),
int(headerSourceSwitchMode)).toInt());
completionRankingModel = CompletionRankingModel(map.value(clangdCompletionRankingModelKey(),
int(completionRankingModel)).toInt());
autoIncludeHeaders = map.value(clangdHeaderInsertionKey(), false).toBool();
workerThreadLimit = map.value(clangdThreadLimitKey(), 0).toInt();
documentUpdateThreshold = map.value(clangdDocumentThresholdKey(), 500).toInt();

View File

@@ -83,10 +83,13 @@ class CPPEDITOR_EXPORT ClangdSettings : public QObject
public:
enum class IndexingPriority { Off, Background, Normal, Low, };
enum class HeaderSourceSwitchMode { BuiltinOnly, ClangdOnly, Both };
enum class CompletionRankingModel { Default, DecisionForest, Heuristics };
static QString priorityToString(const IndexingPriority &priority);
static QString priorityToDisplayString(const IndexingPriority &priority);
static QString headerSourceSwitchModeToDisplayString(HeaderSourceSwitchMode mode);
static QString rankingModelToCmdLineString(CompletionRankingModel model);
static QString rankingModelToDisplayString(CompletionRankingModel model);
class CPPEDITOR_EXPORT Data
{
@@ -104,6 +107,7 @@ public:
&& s1.workerThreadLimit == s2.workerThreadLimit
&& s1.indexingPriority == s2.indexingPriority
&& s1.headerSourceSwitchMode == s2.headerSourceSwitchMode
&& s1.completionRankingModel == s2.completionRankingModel
&& s1.autoIncludeHeaders == s2.autoIncludeHeaders
&& s1.documentUpdateThreshold == s2.documentUpdateThreshold
&& s1.sizeThresholdEnabled == s2.sizeThresholdEnabled
@@ -125,6 +129,7 @@ public:
bool useClangd = true;
IndexingPriority indexingPriority = IndexingPriority::Low;
HeaderSourceSwitchMode headerSourceSwitchMode = HeaderSourceSwitchMode::Both;
CompletionRankingModel completionRankingModel = CompletionRankingModel::Default;
bool autoIncludeHeaders = false;
bool sizeThresholdEnabled = false;
bool haveCheckedHardwareReqirements = false;
@@ -146,6 +151,7 @@ public:
Utils::FilePath clangdFilePath() const;
IndexingPriority indexingPriority() const { return m_data.indexingPriority; }
HeaderSourceSwitchMode headerSourceSwitchMode() const { return m_data.headerSourceSwitchMode; }
CompletionRankingModel completionRankingModel() const { return m_data.completionRankingModel; }
bool autoIncludeHeaders() const { return m_data.autoIncludeHeaders; }
int workerThreadLimit() const { return m_data.workerThreadLimit; }
int documentUpdateThreshold() const { return m_data.documentUpdateThreshold; }

View File

@@ -194,6 +194,7 @@ public:
QCheckBox useClangdCheckBox;
QComboBox indexingComboBox;
QComboBox headerSourceSwitchComboBox;
QComboBox completionRankingModelComboBox;
QCheckBox autoIncludeHeadersCheckBox;
QCheckBox sizeThresholdCheckBox;
QSpinBox threadLimitSpinBox;
@@ -228,6 +229,16 @@ ClangdSettingsWidget::ClangdSettingsWidget(const ClangdSettings::Data &settingsD
"in the built-in variant."
"<p>When \"Try Both\" is selected, clangd will be employed only if the built-in variant "
"does not find anything.");
using RankingModel = ClangdSettings::CompletionRankingModel;
const QString completionRankingModelToolTip = Tr::tr(
"<p>Which model clangd should use to rank possible completions."
"<p>This determines the order of candidates in the combo box when doing code completion."
"<p>The \"%1\" model used by default results from (pre-trained) machine learning and "
"provides superior results on average."
"<p>If you feel that its suggestions stray too much from your expectations for your "
"code base, you can try switching to the hand-crafted \"%2\" model.").arg(
ClangdSettings::rankingModelToDisplayString(RankingModel::DecisionForest),
ClangdSettings::rankingModelToDisplayString(RankingModel::Heuristics));
const QString workerThreadsToolTip = Tr::tr(
"Number of worker threads used by clangd. Background indexing also uses this many "
"worker threads.");
@@ -266,6 +277,16 @@ ClangdSettingsWidget::ClangdSettingsWidget(const ClangdSettings::Data &settingsD
d->headerSourceSwitchComboBox.count() - 1);
}
d->headerSourceSwitchComboBox.setToolTip(headerSourceSwitchToolTip);
for (RankingModel model : {RankingModel::Default, RankingModel::DecisionForest,
RankingModel::Heuristics}) {
d->completionRankingModelComboBox.addItem(
ClangdSettings::rankingModelToDisplayString(model), int(model));
if (model == settings.completionRankingModel())
d->completionRankingModelComboBox.setCurrentIndex(
d->completionRankingModelComboBox.count() - 1);
}
d->completionRankingModelComboBox.setToolTip(completionRankingModelToolTip);
d->autoIncludeHeadersCheckBox.setText(Tr::tr("Insert header files on completion"));
d->autoIncludeHeadersCheckBox.setChecked(settings.autoIncludeHeaders());
d->autoIncludeHeadersCheckBox.setToolTip(autoIncludeToolTip);
@@ -331,6 +352,13 @@ ClangdSettingsWidget::ClangdSettingsWidget(const ClangdSettings::Data &settingsD
limitResultsLayout->addStretch(1);
formLayout->addRow(completionResultsLabel, limitResultsLayout);
const auto completionRankingModelLayout = new QHBoxLayout;
completionRankingModelLayout->addWidget(&d->completionRankingModelComboBox);
completionRankingModelLayout->addStretch(1);
const auto completionRankingModelLabel = new QLabel(Tr::tr("Completion ranking model:"));
completionRankingModelLabel->setToolTip(completionRankingModelToolTip);
formLayout->addRow(completionRankingModelLabel, completionRankingModelLayout);
const auto documentUpdateThresholdLayout = new QHBoxLayout;
documentUpdateThresholdLayout->addWidget(&d->documentUpdateThreshold);
documentUpdateThresholdLayout->addStretch(1);
@@ -476,6 +504,8 @@ ClangdSettingsWidget::ClangdSettingsWidget(const ClangdSettings::Data &settingsD
this, &ClangdSettingsWidget::settingsDataChanged);
connect(&d->headerSourceSwitchComboBox, &QComboBox::currentIndexChanged,
this, &ClangdSettingsWidget::settingsDataChanged);
connect(&d->completionRankingModelComboBox, &QComboBox::currentIndexChanged,
this, &ClangdSettingsWidget::settingsDataChanged);
connect(&d->autoIncludeHeadersCheckBox, &QCheckBox::toggled,
this, &ClangdSettingsWidget::settingsDataChanged);
connect(&d->threadLimitSpinBox, &QSpinBox::valueChanged,
@@ -508,6 +538,8 @@ ClangdSettings::Data ClangdSettingsWidget::settingsData() const
d->indexingComboBox.currentData().toInt());
data.headerSourceSwitchMode = ClangdSettings::HeaderSourceSwitchMode(
d->headerSourceSwitchComboBox.currentData().toInt());
data.completionRankingModel = ClangdSettings::CompletionRankingModel(
d->completionRankingModelComboBox.currentData().toInt());
data.autoIncludeHeaders = d->autoIncludeHeadersCheckBox.isChecked();
data.workerThreadLimit = d->threadLimitSpinBox.value();
data.documentUpdateThreshold = d->documentUpdateThreshold.value();