From a4fbc5f00dddde39871c25bca652de7842baa1dd Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Wed, 31 Jan 2024 10:59:42 +0100 Subject: [PATCH] CppEditor: Don't needlessly store default values Task-number: QTCREATORBUG-24762 Change-Id: I171a7c6bd8f7f62cb4871b3d6c1828ff7edd53ab Reviewed-by: Reviewed-by: Eike Ziller --- src/libs/utils/store.h | 21 ++++- .../cppeditor/cppcodemodelsettings.cpp | 93 +++++++++++++------ src/plugins/cppeditor/cppcodemodelsettings.h | 29 ++++-- 3 files changed, 105 insertions(+), 38 deletions(-) diff --git a/src/libs/utils/store.h b/src/libs/utils/store.h index df45cc8e9ce..97e71b0eecd 100644 --- a/src/libs/utils/store.h +++ b/src/libs/utils/store.h @@ -15,7 +15,26 @@ class QtcSettings; using KeyList = QList; -using Store = QMap; +class Store : public QMap +{ +public: + using QMap::QMap; + + template + void insertValueWithDefault(const Key &key, const T &val, const T &defaultValue) + { + if (val != defaultValue) + insert(key, val); + } + + template + void insertValueWithDefault(const Key &key, const T &val) + { + if (val != T()) + insert(key, val); + } +}; + using OldStore = QMap; QTCREATOR_UTILS_EXPORT KeyList keysFromStrings(const QStringList &list); diff --git a/src/plugins/cppeditor/cppcodemodelsettings.cpp b/src/plugins/cppeditor/cppcodemodelsettings.cpp index 14330796cdc..db6231cfe99 100644 --- a/src/plugins/cppeditor/cppcodemodelsettings.cpp +++ b/src/plugins/cppeditor/cppcodemodelsettings.cpp @@ -546,44 +546,81 @@ void ClangdProjectSettings::saveSettings() Store ClangdSettings::Data::toMap() const { Store map; - map.insert(useClangdKey(), useClangd); - map.insert(clangdPathKey(), - executableFilePath != fallbackClangdFilePath() ? executableFilePath.toString() - : QString()); - 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); - map.insert(clangdSizeThresholdEnabledKey(), sizeThresholdEnabled); - map.insert(clangdSizeThresholdKey(), sizeThresholdInKb); - map.insert(sessionsWithOneClangdKey(), sessionsWithOneClangd); - map.insert(diagnosticConfigIdKey(), diagnosticConfigId.toSetting()); - map.insert(checkedHardwareKey(), true); - map.insert(completionResultsKey(), completionResults); + + map.insertValueWithDefault(useClangdKey(), useClangd, DefaultUseClangd); + + const QString clangdPath = executableFilePath != fallbackClangdFilePath() + ? executableFilePath.toString() + : QString(); + + map.insertValueWithDefault(clangdPathKey(), clangdPath); + + map.insertValueWithDefault(clangdIndexingKey(), indexingPriority != IndexingPriority::Off, true); + map.insertValueWithDefault(clangdIndexingPriorityKey(), + int(indexingPriority), + int(DefaultIndexingPriority)); + + map.insertValueWithDefault(clangdHeaderSourceSwitchModeKey(), + int(headerSourceSwitchMode), + int(DefaultHeaderSourceSwitchMode)); + + map.insertValueWithDefault(clangdCompletionRankingModelKey(), + int(completionRankingModel), + int(DefaultCompletionRankingModel)); + + map.insertValueWithDefault(clangdHeaderInsertionKey(), + autoIncludeHeaders, + DefaultAutoIncludeHeaders); + + map.insertValueWithDefault(clangdThreadLimitKey(), workerThreadLimit, DefaultWorkerThreadLimit); + + map.insertValueWithDefault(clangdDocumentThresholdKey(), + documentUpdateThreshold, + DefaultDocumentUpdateThreshold); + + map.insertValueWithDefault(clangdSizeThresholdEnabledKey(), + sizeThresholdEnabled, + DefaultSizeThresholdEnabled); + + map.insertValueWithDefault(clangdSizeThresholdKey(), + sizeThresholdInKb, + DefaultSizeThresholdInKb); + + map.insertValueWithDefault(sessionsWithOneClangdKey(), sessionsWithOneClangd); + + map.insertValueWithDefault(diagnosticConfigIdKey(), + diagnosticConfigId.toSetting(), + initialClangDiagnosticConfigId().toSetting()); + + if (haveCheckedHardwareReqirements != false) + map.insert(checkedHardwareKey(), true); + + map.insertValueWithDefault(completionResultsKey(), + completionResults, + defaultCompletionResults()); return map; } void ClangdSettings::Data::fromMap(const Store &map) { - useClangd = map.value(useClangdKey(), true).toBool(); + useClangd = map.value(useClangdKey(), DefaultUseClangd).toBool(); executableFilePath = FilePath::fromString(map.value(clangdPathKey()).toString()); indexingPriority = IndexingPriority( - map.value(clangdIndexingPriorityKey(), int(this->indexingPriority)).toInt()); + map.value(clangdIndexingPriorityKey(), int(DefaultIndexingPriority)).toInt()); const auto it = map.find(clangdIndexingKey()); if (it != map.end() && !it->toBool()) 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(); - sizeThresholdEnabled = map.value(clangdSizeThresholdEnabledKey(), false).toBool(); - sizeThresholdInKb = map.value(clangdSizeThresholdKey(), 1024).toLongLong(); + headerSourceSwitchMode = HeaderSourceSwitchMode( + map.value(clangdHeaderSourceSwitchModeKey(), int(DefaultHeaderSourceSwitchMode)).toInt()); + completionRankingModel = CompletionRankingModel( + map.value(clangdCompletionRankingModelKey(), int(DefaultCompletionRankingModel)).toInt()); + autoIncludeHeaders = map.value(clangdHeaderInsertionKey(), DefaultAutoIncludeHeaders).toBool(); + workerThreadLimit = map.value(clangdThreadLimitKey(), DefaultWorkerThreadLimit).toInt(); + documentUpdateThreshold + = map.value(clangdDocumentThresholdKey(), DefaultDocumentUpdateThreshold).toInt(); + sizeThresholdEnabled + = map.value(clangdSizeThresholdEnabledKey(), DefaultSizeThresholdEnabled).toBool(); + sizeThresholdInKb = map.value(clangdSizeThresholdKey(), DefaultSizeThresholdInKb).toLongLong(); sessionsWithOneClangd = map.value(sessionsWithOneClangdKey()).toStringList(); diagnosticConfigId = Id::fromSetting(map.value(diagnosticConfigIdKey(), initialClangDiagnosticConfigId().toSetting())); diff --git a/src/plugins/cppeditor/cppcodemodelsettings.h b/src/plugins/cppeditor/cppcodemodelsettings.h index b6cecdf5b94..b4f8ac7269d 100644 --- a/src/plugins/cppeditor/cppcodemodelsettings.h +++ b/src/plugins/cppeditor/cppcodemodelsettings.h @@ -125,15 +125,26 @@ public: QStringList sessionsWithOneClangd; ClangDiagnosticConfigs customDiagnosticConfigs; Utils::Id diagnosticConfigId; - int workerThreadLimit = 0; - int documentUpdateThreshold = 500; - qint64 sizeThresholdInKb = 1024; - bool useClangd = true; - IndexingPriority indexingPriority = IndexingPriority::Low; - HeaderSourceSwitchMode headerSourceSwitchMode = HeaderSourceSwitchMode::Both; - CompletionRankingModel completionRankingModel = CompletionRankingModel::Default; - bool autoIncludeHeaders = false; - bool sizeThresholdEnabled = false; + + static constexpr auto DefaultWorkerThreadLimit = 0; + static constexpr auto DefaultDocumentUpdateThreshold = 500; + static constexpr auto DefaultSizeThresholdInKb = 1024ll; + static constexpr auto DefaultUseClangd = true; + static constexpr auto DefaultIndexingPriority = ClangdSettings::IndexingPriority::Low; + static constexpr auto DefaultHeaderSourceSwitchMode = HeaderSourceSwitchMode::Both; + static constexpr auto DefaultCompletionRankingModel = CompletionRankingModel::Default; + static constexpr auto DefaultAutoIncludeHeaders = false; + static constexpr auto DefaultSizeThresholdEnabled = false; + + int workerThreadLimit = DefaultWorkerThreadLimit; + int documentUpdateThreshold = DefaultDocumentUpdateThreshold; + qint64 sizeThresholdInKb = DefaultSizeThresholdInKb; + bool useClangd = DefaultUseClangd; + IndexingPriority indexingPriority = DefaultIndexingPriority; + HeaderSourceSwitchMode headerSourceSwitchMode = DefaultHeaderSourceSwitchMode; + CompletionRankingModel completionRankingModel = DefaultCompletionRankingModel; + bool autoIncludeHeaders = DefaultAutoIncludeHeaders; + bool sizeThresholdEnabled = DefaultSizeThresholdEnabled; bool haveCheckedHardwareReqirements = false; int completionResults = defaultCompletionResults(); };