CMake: Prepare postponing formatter settings creation

This is still an overall quirky setup.

Change-Id: I7c1d22267bb76d434fe2f9b77ed8d0bfef5ac952
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
hjk
2023-07-14 14:41:41 +02:00
parent 31cf0dfb74
commit c326f049dc
3 changed files with 30 additions and 31 deletions

View File

@@ -37,19 +37,15 @@ using namespace Utils;
namespace CMakeProjectManager::Internal { namespace CMakeProjectManager::Internal {
class CMakeFormatterPrivate : public PagedSettings class CMakeFormatterSettings : public AspectContainer
{ {
public: public:
CMakeFormatterPrivate() CMakeFormatterSettings()
{ {
setAutoApply(false);
setSettingsGroups(Constants::CMAKEFORMATTER_SETTINGS_GROUP, setSettingsGroups(Constants::CMAKEFORMATTER_SETTINGS_GROUP,
Constants::CMAKEFORMATTER_GENERAL_GROUP); Constants::CMAKEFORMATTER_GENERAL_GROUP);
setId(Constants::Settings::FORMATTER_ID);
setDisplayName(Tr::tr("Formatter"));
setDisplayCategory("CMake");
setCategory(Constants::Settings::CATEGORY);
command.setSettingsKey("autoFormatCommand"); command.setSettingsKey("autoFormatCommand");
command.setDefaultValue("cmake-format"); command.setDefaultValue("cmake-format");
command.setExpectedKind(PathChooser::ExistingCommand); command.setExpectedKind(PathChooser::ExistingCommand);
@@ -109,7 +105,7 @@ public:
connect(EditorManager::instance(), &EditorManager::currentEditorChanged, connect(EditorManager::instance(), &EditorManager::currentEditorChanged,
this, updateActions); this, updateActions);
connect(EditorManager::instance(), &EditorManager::aboutToSave, connect(EditorManager::instance(), &EditorManager::aboutToSave,
this, &CMakeFormatterPrivate::applyIfNecessary); this, &CMakeFormatterSettings::applyIfNecessary);
readSettings(); readSettings();
} }
@@ -136,15 +132,15 @@ public:
QAction formatFile{Tr::tr("Format &Current File")}; QAction formatFile{Tr::tr("Format &Current File")};
}; };
bool CMakeFormatterPrivate::isApplicable(const IDocument *document) const bool CMakeFormatterSettings::isApplicable(const IDocument *document) const
{ {
if (!document) if (!document)
return false; return false;
if (autoFormatMime.value().isEmpty()) if (autoFormatMime().isEmpty())
return true; return true;
const QStringList allowedMimeTypes = autoFormatMime.value().split(';'); const QStringList allowedMimeTypes = autoFormatMime().split(';');
const MimeType documentMimeType = Utils::mimeTypeForName(document->mimeType()); const MimeType documentMimeType = Utils::mimeTypeForName(document->mimeType());
return anyOf(allowedMimeTypes, [&documentMimeType](const QString &mime) { return anyOf(allowedMimeTypes, [&documentMimeType](const QString &mime) {
@@ -152,9 +148,9 @@ bool CMakeFormatterPrivate::isApplicable(const IDocument *document) const
}); });
} }
void CMakeFormatterPrivate::applyIfNecessary(IDocument *document) const void CMakeFormatterSettings::applyIfNecessary(IDocument *document) const
{ {
if (!autoFormatOnSave.value()) if (!autoFormatOnSave())
return; return;
if (!document) if (!document)
@@ -164,7 +160,7 @@ void CMakeFormatterPrivate::applyIfNecessary(IDocument *document) const
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 (autoFormatOnlyCurrentProject.value()) { if (autoFormatOnlyCurrentProject()) {
const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject(); const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject();
if (!pro || pro->files([document](const ProjectExplorer::Node *n) { if (!pro || pro->files([document](const ProjectExplorer::Node *n) {
return ProjectExplorer::Project::SourceFiles(n) return ProjectExplorer::Project::SourceFiles(n)
@@ -188,20 +184,30 @@ void CMakeFormatterPrivate::applyIfNecessary(IDocument *document) const
TextEditor::formatEditor(widget, command); TextEditor::formatEditor(widget, command);
} }
// CMakeFormatter CMakeFormatterSettings &formatterSettings()
CMakeFormatter::CMakeFormatter()
: d(new CMakeFormatterPrivate)
{}
CMakeFormatter::~CMakeFormatter()
{ {
delete d; static CMakeFormatterSettings theSettings;
return theSettings;
} }
void CMakeFormatter::applyIfNecessary(IDocument *document) const class CMakeFormatterSettingsPage final : public Core::IOptionsPage
{ {
d->applyIfNecessary(document); public:
CMakeFormatterSettingsPage()
{
setId(Constants::Settings::FORMATTER_ID);
setDisplayName(Tr::tr("Formatter"));
setDisplayCategory("CMake");
setCategory(Constants::Settings::CATEGORY);
setSettingsProvider([] { return &formatterSettings(); });
}
};
const CMakeFormatterSettingsPage settingsPage;
CMakeFormatter::CMakeFormatter()
{
formatterSettings();
} }
} // CMakeProjectManager::Internal } // CMakeProjectManager::Internal

View File

@@ -12,12 +12,6 @@ class CMakeFormatter
{ {
public: public:
CMakeFormatter(); CMakeFormatter();
~CMakeFormatter();
void applyIfNecessary(Core::IDocument *document) const;
private:
class CMakeFormatterPrivate *d = nullptr;
}; };
} // CMakeProjectManager::Internal } // CMakeProjectManager::Internal

View File

@@ -65,7 +65,6 @@ public:
CMakeKitAspect cmakeKitAspect; CMakeKitAspect cmakeKitAspect;
CMakeGeneratorKitAspect cmakeGeneratorKitAspect; CMakeGeneratorKitAspect cmakeGeneratorKitAspect;
CMakeConfigurationKitAspect cmakeConfigurationKitAspect; CMakeConfigurationKitAspect cmakeConfigurationKitAspect;
CMakeFormatter cmakeFormatter; CMakeFormatter cmakeFormatter;
}; };