Beautifier: Delay-construct main settings

Even though it doesn't help yet as it's immediately accessed.

Change-Id: I7e3a40a05517130a9f747c82a3d4928378143947
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
hjk
2023-07-14 10:23:29 +02:00
parent ce998a1661
commit eb5e258d96
3 changed files with 33 additions and 23 deletions

View File

@@ -26,22 +26,14 @@
#include <texteditor/formattexteditor.h> #include <texteditor/formattexteditor.h>
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <texteditor/textdocumentlayout.h>
#include <texteditor/texteditor.h> #include <texteditor/texteditor.h>
#include <texteditor/texteditorconstants.h> #include <texteditor/texteditorconstants.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/mimeutils.h> #include <utils/mimeutils.h>
#include <utils/process.h>
#include <utils/qtcassert.h>
#include <utils/temporarydirectory.h>
#include <utils/textutils.h>
#include <QMenu> #include <QMenu>
#include <QPlainTextEdit>
#include <QScrollBar>
#include <QTextBlock>
using namespace TextEditor; using namespace TextEditor;
@@ -71,8 +63,6 @@ public:
void autoFormatOnSave(Core::IDocument *document); void autoFormatOnSave(Core::IDocument *document);
GeneralSettings generalSettings;
ArtisticStyle artisticStyleBeautifier; ArtisticStyle artisticStyleBeautifier;
ClangFormat clangFormatBeautifier; ClangFormat clangFormatBeautifier;
Uncrustify uncrustifyBeautifier; Uncrustify uncrustifyBeautifier;
@@ -103,7 +93,7 @@ ExtensionSystem::IPlugin::ShutdownFlag BeautifierPlugin::aboutToShutdown()
BeautifierPluginPrivate::BeautifierPluginPrivate() BeautifierPluginPrivate::BeautifierPluginPrivate()
{ {
for (BeautifierTool *tool : BeautifierTool::allTools()) for (BeautifierTool *tool : BeautifierTool::allTools())
generalSettings.autoFormatTools.addOption(tool->id()); generalSettings().autoFormatTools.addOption(tool->id());
updateActions(); updateActions();
@@ -122,14 +112,14 @@ void BeautifierPluginPrivate::updateActions(Core::IEditor *editor)
void BeautifierPluginPrivate::autoFormatOnSave(Core::IDocument *document) void BeautifierPluginPrivate::autoFormatOnSave(Core::IDocument *document)
{ {
if (!generalSettings.autoFormatOnSave.value()) if (!generalSettings().autoFormatOnSave())
return; return;
if (!isAutoFormatApplicable(document, generalSettings.allowedMimeTypes())) if (!isAutoFormatApplicable(document, generalSettings().allowedMimeTypes()))
return; return;
// Check if file is contained in the current project (if wished) // 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(); const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject();
if (!pro if (!pro
|| pro->files([document](const ProjectExplorer::Node *n) { || 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! // Find tool to use by id and format file!
const QString id = generalSettings.autoFormatTools.stringValue(); const QString id = generalSettings().autoFormatTools.stringValue();
const QList<BeautifierTool *> &tools = BeautifierTool::allTools(); const QList<BeautifierTool *> &tools = BeautifierTool::allTools();
auto tool = std::find_if(std::begin(tools), std::end(tools), auto tool = std::find_if(std::begin(tools), std::end(tools),
[&id](const BeautifierTool *t){return t->id() == id;}); [&id](const BeautifierTool *t){return t->id() == id;});

View File

@@ -6,6 +6,8 @@
#include "beautifierconstants.h" #include "beautifierconstants.h"
#include "beautifiertr.h" #include "beautifiertr.h"
#include <coreplugin/dialogs/ioptionspage.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/genericconstants.h> #include <utils/genericconstants.h>
#include <utils/layoutbuilder.h> #include <utils/layoutbuilder.h>
@@ -14,13 +16,14 @@ using namespace Utils;
namespace Beautifier::Internal { namespace Beautifier::Internal {
GeneralSettings &generalSettings()
{
static GeneralSettings theSettings;
return theSettings;
}
GeneralSettings::GeneralSettings() 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"); setSettingsGroups("Beautifier", "General");
autoFormatOnSave.setSettingsKey(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE); autoFormatOnSave.setSettingsKey(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE);
@@ -73,4 +76,20 @@ QList<MimeType> GeneralSettings::allowedMimeTypes() const
return types; 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 } // Beautifier::Internal

View File

@@ -3,13 +3,12 @@
#pragma once #pragma once
#include <utils/aspects.h>
#include <utils/mimeutils.h> #include <utils/mimeutils.h>
#include <coreplugin/dialogs/ioptionspage.h>
namespace Beautifier::Internal { namespace Beautifier::Internal {
class GeneralSettings : public Core::PagedSettings class GeneralSettings final : public Utils::AspectContainer
{ {
public: public:
GeneralSettings(); GeneralSettings();
@@ -22,4 +21,6 @@ public:
Utils::StringAspect autoFormatMime{this}; Utils::StringAspect autoFormatMime{this};
}; };
GeneralSettings &generalSettings();
} // Beautifier::Internal } // Beautifier::Internal