From eb5e258d964394c2d17d2db0f2bac43faf0929a2 Mon Sep 17 00:00:00 2001 From: hjk Date: Fri, 14 Jul 2023 10:23:29 +0200 Subject: [PATCH] Beautifier: Delay-construct main settings Even though it doesn't help yet as it's immediately accessed. Change-Id: I7e3a40a05517130a9f747c82a3d4928378143947 Reviewed-by: Marcus Tillmanns --- src/plugins/beautifier/beautifierplugin.cpp | 20 ++++---------- src/plugins/beautifier/generalsettings.cpp | 29 +++++++++++++++++---- src/plugins/beautifier/generalsettings.h | 7 ++--- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp index ade354b62c1..d4389b00c54 100644 --- a/src/plugins/beautifier/beautifierplugin.cpp +++ b/src/plugins/beautifier/beautifierplugin.cpp @@ -26,22 +26,14 @@ #include #include -#include #include #include #include #include #include -#include -#include -#include -#include #include -#include -#include -#include using namespace TextEditor; @@ -71,8 +63,6 @@ public: void autoFormatOnSave(Core::IDocument *document); - GeneralSettings generalSettings; - ArtisticStyle artisticStyleBeautifier; ClangFormat clangFormatBeautifier; Uncrustify uncrustifyBeautifier; @@ -103,7 +93,7 @@ ExtensionSystem::IPlugin::ShutdownFlag BeautifierPlugin::aboutToShutdown() BeautifierPluginPrivate::BeautifierPluginPrivate() { for (BeautifierTool *tool : BeautifierTool::allTools()) - generalSettings.autoFormatTools.addOption(tool->id()); + generalSettings().autoFormatTools.addOption(tool->id()); updateActions(); @@ -122,14 +112,14 @@ void BeautifierPluginPrivate::updateActions(Core::IEditor *editor) void BeautifierPluginPrivate::autoFormatOnSave(Core::IDocument *document) { - if (!generalSettings.autoFormatOnSave.value()) + if (!generalSettings().autoFormatOnSave()) return; - if (!isAutoFormatApplicable(document, generalSettings.allowedMimeTypes())) + if (!isAutoFormatApplicable(document, generalSettings().allowedMimeTypes())) return; // Check if file is contained in the current project (if wished) - if (generalSettings.autoFormatOnlyCurrentProject.value()) { + if (generalSettings().autoFormatOnlyCurrentProject()) { const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject(); if (!pro || pro->files([document](const ProjectExplorer::Node *n) { @@ -142,7 +132,7 @@ void BeautifierPluginPrivate::autoFormatOnSave(Core::IDocument *document) } // Find tool to use by id and format file! - const QString id = generalSettings.autoFormatTools.stringValue(); + const QString id = generalSettings().autoFormatTools.stringValue(); const QList &tools = BeautifierTool::allTools(); auto tool = std::find_if(std::begin(tools), std::end(tools), [&id](const BeautifierTool *t){return t->id() == id;}); diff --git a/src/plugins/beautifier/generalsettings.cpp b/src/plugins/beautifier/generalsettings.cpp index b54e2cc93cd..7b8d37416ac 100644 --- a/src/plugins/beautifier/generalsettings.cpp +++ b/src/plugins/beautifier/generalsettings.cpp @@ -6,6 +6,8 @@ #include "beautifierconstants.h" #include "beautifiertr.h" +#include + #include #include #include @@ -14,13 +16,14 @@ using namespace Utils; namespace Beautifier::Internal { +GeneralSettings &generalSettings() +{ + static GeneralSettings theSettings; + return theSettings; +} + GeneralSettings::GeneralSettings() { - setId(Constants::OPTION_GENERAL_ID); - setDisplayName(Tr::tr("General")); - setCategory(Constants::OPTION_CATEGORY); - setDisplayCategory(Tr::tr("Beautifier")); - setCategoryIconPath(":/beautifier/images/settingscategory_beautifier.png"); setSettingsGroups("Beautifier", "General"); autoFormatOnSave.setSettingsKey(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE); @@ -73,4 +76,20 @@ QList GeneralSettings::allowedMimeTypes() const return types; } +class GeneralSettingsPage final : public Core::IOptionsPage +{ +public: + GeneralSettingsPage() + { + setId(Constants::OPTION_GENERAL_ID); + setDisplayName(Tr::tr("General")); + setCategory(Constants::OPTION_CATEGORY); + setDisplayCategory(Tr::tr("Beautifier")); + setCategoryIconPath(":/beautifier/images/settingscategory_beautifier.png"); + setSettingsProvider([] { return &generalSettings(); }); + } +}; + +const GeneralSettingsPage settingsPage; + } // Beautifier::Internal diff --git a/src/plugins/beautifier/generalsettings.h b/src/plugins/beautifier/generalsettings.h index 0be68b98e35..23c55e1765d 100644 --- a/src/plugins/beautifier/generalsettings.h +++ b/src/plugins/beautifier/generalsettings.h @@ -3,13 +3,12 @@ #pragma once +#include #include -#include - namespace Beautifier::Internal { -class GeneralSettings : public Core::PagedSettings +class GeneralSettings final : public Utils::AspectContainer { public: GeneralSettings(); @@ -22,4 +21,6 @@ public: Utils::StringAspect autoFormatMime{this}; }; +GeneralSettings &generalSettings(); + } // Beautifier::Internal