diff options
author | hjk <hjk@qt.io> | 2023-05-11 08:49:06 +0200 |
---|---|---|
committer | hjk <hjk@qt.io> | 2023-05-15 11:21:45 +0000 |
commit | 9dad4ab9d4f3b638ab3df6cd45eca1fbaa51fa96 (patch) | |
tree | d7817f35c8303189a568e40aadf09e9164981079 | |
parent | 72d9decb877c158e79f6b21c7aab5a006985c024 (diff) | |
download | qt-creator-9dad4ab9d4f3b638ab3df6cd45eca1fbaa51fa96.tar.gz |
CMakeProjectManager: Rework CMakeFormatter
Use PagedSettings, make setup more local, remove Q_OBJECT
Change-Id: I9e91f9e63ed8ad15749323bd039d118562dba1a6
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
9 files changed, 175 insertions, 444 deletions
diff --git a/src/plugins/cmakeprojectmanager/CMakeLists.txt b/src/plugins/cmakeprojectmanager/CMakeLists.txt index e12c9656a8..3d9dc4849e 100644 --- a/src/plugins/cmakeprojectmanager/CMakeLists.txt +++ b/src/plugins/cmakeprojectmanager/CMakeLists.txt @@ -16,8 +16,6 @@ add_qtc_plugin(CMakeProjectManager cmakeeditor.cpp cmakeeditor.h cmakefilecompletionassist.cpp cmakefilecompletionassist.h cmakeformatter.cpp cmakeformatter.h - cmakeformatteroptionspage.cpp cmakeformatteroptionspage.h - cmakeformattersettings.cpp cmakeformattersettings.h cmakeindenter.cpp cmakeindenter.h cmakeinstallstep.cpp cmakeinstallstep.h cmakekitinformation.cpp cmakekitinformation.h diff --git a/src/plugins/cmakeprojectmanager/cmakeformatter.cpp b/src/plugins/cmakeprojectmanager/cmakeformatter.cpp index d9aa8b2c84..3996946e47 100644 --- a/src/plugins/cmakeprojectmanager/cmakeformatter.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeformatter.cpp @@ -4,7 +4,6 @@ #include "cmakeformatter.h" -#include "cmakeformattersettings.h" #include "cmakeprojectconstants.h" #include "cmakeprojectmanagertr.h" @@ -12,62 +11,198 @@ #include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/command.h> #include <coreplugin/coreconstants.h> +#include <coreplugin/dialogs/ioptionspage.h> #include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/ieditor.h> +#include <coreplugin/icore.h> #include <coreplugin/idocument.h> #include <projectexplorer/project.h> #include <projectexplorer/projectnodes.h> #include <projectexplorer/projecttree.h> +#include <texteditor/command.h> #include <texteditor/formattexteditor.h> #include <texteditor/texteditor.h> -#include <QAction> -#include <QVersionNumber> +#include <utils/algorithm.h> +#include <utils/genericconstants.h> +#include <utils/layoutbuilder.h> +#include <utils/mimeutils.h> +#include <QMenu> + +using namespace Core; using namespace TextEditor; +using namespace Utils; -namespace CMakeProjectManager { -namespace Internal { +namespace CMakeProjectManager::Internal { -void CMakeFormatter::updateActions(Core::IEditor *editor) +class CMakeFormatterPrivate : public PagedSettings { - const bool enabled = editor && CMakeFormatterSettings::instance()->isApplicable(editor->document()); - m_formatFile->setEnabled(enabled); -} - -void CMakeFormatter::formatFile() +public: + CMakeFormatterPrivate() + { + setSettingsGroups(Constants::CMAKEFORMATTER_SETTINGS_GROUP, + Constants::CMAKEFORMATTER_GENERAL_GROUP); + + setId(Constants::Settings::FORMATTER_ID); + setDisplayName(Tr::tr("Formatter")); + setDisplayCategory("CMake"); + setCategory(Constants::Settings::CATEGORY); + + registerAspect(&command); + command.setSettingsKey("autoFormatCommand"); + command.setDisplayStyle(StringAspect::PathChooserDisplay); + command.setDefaultValue("cmake-format"); + command.setExpectedKind(PathChooser::ExistingCommand); + + registerAspect(&autoFormatOnSave); + autoFormatOnSave.setSettingsKey("autoFormatOnSave"); + autoFormatOnSave.setLabelText(Tr::tr("Enable auto format on file save")); + + registerAspect(&autoFormatOnlyCurrentProject); + autoFormatOnlyCurrentProject.setSettingsKey("autoFormatOnlyCurrentProject"); + autoFormatOnlyCurrentProject.setDefaultValue(true); + autoFormatOnlyCurrentProject.setLabelText(Tr::tr("Restrict to files contained in the current project")); + + registerAspect(&autoFormatMime); + autoFormatMime.setSettingsKey("autoFormatMime"); + autoFormatMime.setDefaultValue("text/x-cmake"); + autoFormatMime.setLabelText(Tr::tr("Restrict to MIME types:")); + + setLayouter([this](QWidget *widget) { + using namespace Layouting; + Column { + Row { Tr::tr("CMakeFormat command:"), command }, + Space(10), + Group { + title(Tr::tr("Automatic Formatting on File Save")), + autoFormatOnSave.groupChecker(), + Form { + autoFormatMime, br, + Span(2, autoFormatOnlyCurrentProject) + } + }, + st + }.attachTo(widget); + }); + + ActionContainer *menu = ActionManager::createMenu(Constants::CMAKEFORMATTER_MENU_ID); + menu->menu()->setTitle(Tr::tr("CMakeFormatter")); + menu->setOnAllDisabledBehavior(ActionContainer::Show); + ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(menu); + + Core::Command *cmd = ActionManager::registerAction(&formatFile, Constants::CMAKEFORMATTER_ACTION_ID); + connect(&formatFile, &QAction::triggered, this, [this] { + TextEditor::formatCurrentFile(formatCommand()); + }); + + ActionManager::actionContainer(Constants::CMAKEFORMATTER_MENU_ID)->addAction(cmd); + + auto updateActions = [this] { + auto editor = EditorManager::currentEditor(); + formatFile.setEnabled(editor && isApplicable(editor->document())); + }; + + connect(&autoFormatMime, &Utils::StringAspect::changed, + this, updateActions); + connect(EditorManager::instance(), &EditorManager::currentEditorChanged, + this, updateActions); + connect(EditorManager::instance(), &EditorManager::aboutToSave, + this, &CMakeFormatterPrivate::applyIfNecessary); + + readSettings(ICore::settings()); + } + + bool isApplicable(const IDocument *document) const; + + void applyIfNecessary(IDocument *document) const; + + TextEditor::Command formatCommand() const + { + TextEditor::Command cmd; + cmd.setExecutable(command.filePath()); + cmd.setProcessing(TextEditor::Command::FileProcessing); + cmd.addOption("--in-place"); + cmd.addOption("%file"); + return cmd; + } + + StringAspect command; + BoolAspect autoFormatOnSave; + BoolAspect autoFormatOnlyCurrentProject; + StringAspect autoFormatMime; + + QAction formatFile{Tr::tr("Format &Current File")}; +}; + +bool CMakeFormatterPrivate::isApplicable(const IDocument *document) const { - formatCurrentFile(command()); + if (!document) + return false; + + if (autoFormatMime.value().isEmpty()) + return true; + + const QStringList allowedMimeTypes = autoFormatMime.value().split(';'); + const MimeType documentMimeType = Utils::mimeTypeForName(document->mimeType()); + + return anyOf(allowedMimeTypes, [&documentMimeType](const QString &mime) { + return documentMimeType.inherits(mime); + }); } -Command CMakeFormatter::command() const +void CMakeFormatterPrivate::applyIfNecessary(IDocument *document) const { - Command command; - command.setExecutable(CMakeFormatterSettings::instance()->command()); - command.setProcessing(Command::FileProcessing); - command.addOption("--in-place"); - command.addOption("%file"); - return command; + if (!autoFormatOnSave.value()) + return; + + if (!document) + return; + + if (!isApplicable(document)) + return; + + // Check if file is contained in the current project (if wished) + if (autoFormatOnlyCurrentProject.value()) { + const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject(); + if (!pro || pro->files([document](const ProjectExplorer::Node *n) { + return ProjectExplorer::Project::SourceFiles(n) + && n->filePath() == document->filePath(); + }).isEmpty()) { + return; + } + } + + TextEditor::Command command = formatCommand(); + if (!command.isValid()) + return; + + const QList<IEditor *> editors = DocumentModel::editorsForDocument(document); + if (editors.isEmpty()) + return; + + IEditor *currentEditor = EditorManager::currentEditor(); + IEditor *editor = editors.contains(currentEditor) ? currentEditor : editors.first(); + if (auto widget = TextEditorWidget::fromEditor(editor)) + TextEditor::formatEditor(widget, command); } -bool CMakeFormatter::isApplicable(const Core::IDocument *document) const +// CMakeFormatter + +CMakeFormatter::CMakeFormatter() + : d(new CMakeFormatterPrivate) +{} + +CMakeFormatter::~CMakeFormatter() { - return CMakeFormatterSettings::instance()->isApplicable(document); + delete d; } -void CMakeFormatter::initialize() +void CMakeFormatter::applyIfNecessary(IDocument *document) const { - m_formatFile = new QAction(Tr::tr("Format &Current File"), this); - Core::Command *cmd = Core::ActionManager::registerAction(m_formatFile, Constants::CMAKEFORMATTER_ACTION_ID); - connect(m_formatFile, &QAction::triggered, this, &CMakeFormatter::formatFile); - - Core::ActionManager::actionContainer(Constants::CMAKEFORMATTER_MENU_ID)->addAction(cmd); - - connect(CMakeFormatterSettings::instance(), &CMakeFormatterSettings::supportedMimeTypesChanged, - [this] { updateActions(Core::EditorManager::currentEditor()); }); + d->applyIfNecessary(document); } -} // namespace Internal -} // namespace CMakeProjectManager +} // CMakeProjectManager::Internal diff --git a/src/plugins/cmakeprojectmanager/cmakeformatter.h b/src/plugins/cmakeprojectmanager/cmakeformatter.h index f3fc3a28c5..9727c245a5 100644 --- a/src/plugins/cmakeprojectmanager/cmakeformatter.h +++ b/src/plugins/cmakeprojectmanager/cmakeformatter.h @@ -4,35 +4,20 @@ #pragma once -#include <texteditor/command.h> +namespace Core { class IDocument; } -#include "cmakeformatteroptionspage.h" +namespace CMakeProjectManager::Internal { -namespace Core { -class IDocument; -class IEditor; -} - -namespace CMakeProjectManager { -namespace Internal { - -class CMakeFormatter : public QObject +class CMakeFormatter { - Q_OBJECT - public: - void updateActions(Core::IEditor *editor); - TextEditor::Command command() const; - bool isApplicable(const Core::IDocument *document) const; + CMakeFormatter(); + ~CMakeFormatter(); - void initialize(); + void applyIfNecessary(Core::IDocument *document) const; private: - void formatFile(); - - QAction *m_formatFile = nullptr; - CMakeFormatterOptionsPage m_page; + class CMakeFormatterPrivate *d = nullptr; }; -} // namespace Internal -} // namespace CMakeProjectManager +} // CMakeProjectManager::Internal diff --git a/src/plugins/cmakeprojectmanager/cmakeformatteroptionspage.cpp b/src/plugins/cmakeprojectmanager/cmakeformatteroptionspage.cpp deleted file mode 100644 index 2e08946991..0000000000 --- a/src/plugins/cmakeprojectmanager/cmakeformatteroptionspage.cpp +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright (C) 2016 Lorenz Haas -// Copyright (C) 2022 Xavier BESSON -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 - -#include "cmakeformatteroptionspage.h" - -#include "cmakeprojectconstants.h" -#include "cmakeprojectmanagertr.h" -#include "cmakeformattersettings.h" - -#include <utils/layoutbuilder.h> -#include <utils/pathchooser.h> - -#include <QApplication> -#include <QCheckBox> -#include <QComboBox> -#include <QLabel> -#include <QLineEdit> - -namespace CMakeProjectManager::Internal { - -class CMakeFormatterOptionsPageWidget : public Core::IOptionsPageWidget -{ -public: - explicit CMakeFormatterOptionsPageWidget(); - -private: - void apply() final; - - Utils::PathChooser *m_command; - QCheckBox *m_autoFormat; - QLineEdit *m_autoFormatMime; - QCheckBox *m_autoFormatOnlyCurrentProject; -}; - -CMakeFormatterOptionsPageWidget::CMakeFormatterOptionsPageWidget() -{ - auto settings = CMakeFormatterSettings::instance(); - - m_autoFormat = new QCheckBox(Tr::tr("Enable auto format on file save")); - m_autoFormat->setChecked(settings->autoFormatOnSave()); - - auto mimeLabel = new QLabel(Tr::tr("Restrict to MIME types:")); - mimeLabel->setEnabled(false); - - m_autoFormatMime = new QLineEdit(settings->autoFormatMimeAsString()); - m_autoFormatMime->setEnabled(m_autoFormat->isChecked()); - - m_autoFormatOnlyCurrentProject = - new QCheckBox(Tr::tr("Restrict to files contained in the current project")); - m_autoFormatOnlyCurrentProject->setEnabled(m_autoFormat->isChecked()); - m_autoFormatOnlyCurrentProject->setChecked(settings->autoFormatOnlyCurrentProject()); - - m_command = new Utils::PathChooser; - m_command->setExpectedKind(Utils::PathChooser::ExistingCommand); - m_command->setCommandVersionArguments({"--version"}); - m_command->setPromptDialogTitle(Tr::tr("%1 Command").arg(Tr::tr("Formatter"))); - m_command->setFilePath(settings->command()); - - using namespace Layouting; - - Column { - Group { - title(Tr::tr("Automatic Formatting on File Save")), - Form { - Tr::tr("CMakeFormat command:"), m_command, br, - Span(2, m_autoFormat), br, - mimeLabel, m_autoFormatMime, br, - Span(2, m_autoFormatOnlyCurrentProject) - } - }, - st - }.attachTo(this); - - connect(m_autoFormat, &QCheckBox::toggled, m_autoFormatMime, &QLineEdit::setEnabled); - connect(m_autoFormat, &QCheckBox::toggled, mimeLabel, &QLabel::setEnabled); - connect(m_autoFormat, &QCheckBox::toggled, m_autoFormatOnlyCurrentProject, &QCheckBox::setEnabled); -} - -void CMakeFormatterOptionsPageWidget::apply() -{ - auto settings = CMakeFormatterSettings::instance(); - settings->setCommand(m_command->filePath().toString()); - settings->setAutoFormatOnSave(m_autoFormat->isChecked()); - settings->setAutoFormatMime(m_autoFormatMime->text()); - settings->setAutoFormatOnlyCurrentProject(m_autoFormatOnlyCurrentProject->isChecked()); - settings->save(); -} - -CMakeFormatterOptionsPage::CMakeFormatterOptionsPage() -{ - setId(Constants::Settings::FORMATTER_ID); - setDisplayName(Tr::tr("Formatter")); - setDisplayCategory("CMake"); - setCategory(Constants::Settings::CATEGORY); - setWidgetCreator([] { return new CMakeFormatterOptionsPageWidget; }); -} - -} // CMakeProjectManager::Internal diff --git a/src/plugins/cmakeprojectmanager/cmakeformatteroptionspage.h b/src/plugins/cmakeprojectmanager/cmakeformatteroptionspage.h deleted file mode 100644 index 08cfac3590..0000000000 --- a/src/plugins/cmakeprojectmanager/cmakeformatteroptionspage.h +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (C) 2016 Lorenz Haas -// Copyright (C) 2022 Xavier BESSON -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 - -#pragma once - -#include <coreplugin/dialogs/ioptionspage.h> - -namespace CMakeProjectManager::Internal { - -class CMakeFormatterOptionsPage final : public Core::IOptionsPage -{ -public: - explicit CMakeFormatterOptionsPage(); -}; - -} // CMakeProjectManager::Internal diff --git a/src/plugins/cmakeprojectmanager/cmakeformattersettings.cpp b/src/plugins/cmakeprojectmanager/cmakeformattersettings.cpp deleted file mode 100644 index 258954fa11..0000000000 --- a/src/plugins/cmakeprojectmanager/cmakeformattersettings.cpp +++ /dev/null @@ -1,135 +0,0 @@ -// Copyright (C) 2016 Lorenz Haas -// Copyright (C) 2022 Xavier BESSON -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 - -#include "cmakeformattersettings.h" -#include "cmakeprojectconstants.h" - -#include <coreplugin/icore.h> -#include <coreplugin/idocument.h> -#include <utils/algorithm.h> -#include <utils/genericconstants.h> -#include <utils/mimeutils.h> -#include <utils/process.h> - -namespace CMakeProjectManager { -namespace Internal { - -namespace { -const char AUTO_FORMAT_COMMAND[] = "autoFormatCommand"; -const char AUTO_FORMAT_MIME[] = "autoFormatMime"; -const char AUTO_FORMAT_ONLY_CURRENT_PROJECT[] = "autoFormatOnlyCurrentProject"; -const char AUTO_FORMAT_ON_SAVE[] = "autoFormatOnSave"; -} - -CMakeFormatterSettings::CMakeFormatterSettings(QObject* parent) - : QObject(parent) -{ - read(); -} - -CMakeFormatterSettings *CMakeFormatterSettings::instance() -{ - static CMakeFormatterSettings m_instance; - return &m_instance; -} - -void CMakeFormatterSettings::read() -{ - QSettings *s = Core::ICore::settings(); - s->beginGroup(Constants::CMAKEFORMATTER_SETTINGS_GROUP); - s->beginGroup(Constants::CMAKEFORMATTER_GENERAL_GROUP); - setCommand(s->value(AUTO_FORMAT_COMMAND, QString("cmake-format")).toString()); - m_autoFormatOnSave = s->value(AUTO_FORMAT_ON_SAVE, false).toBool(); - setAutoFormatMime(s->value(AUTO_FORMAT_MIME, QString("text/x-cmake")).toString()); - m_autoFormatOnlyCurrentProject = s->value(AUTO_FORMAT_ONLY_CURRENT_PROJECT, true).toBool(); - s->endGroup(); - s->endGroup(); -} - -void CMakeFormatterSettings::save() -{ - QSettings *s = Core::ICore::settings(); - s->beginGroup(Constants::CMAKEFORMATTER_SETTINGS_GROUP); - s->beginGroup(Constants::CMAKEFORMATTER_GENERAL_GROUP); - Utils::QtcSettings::setValueWithDefault(s, AUTO_FORMAT_COMMAND, m_command, QString("cmake-format")); - Utils::QtcSettings::setValueWithDefault(s, AUTO_FORMAT_ON_SAVE, m_autoFormatOnSave, false); - Utils::QtcSettings::setValueWithDefault(s, AUTO_FORMAT_MIME, autoFormatMimeAsString(), QString("text/x-cmake")); - Utils::QtcSettings::setValueWithDefault(s, AUTO_FORMAT_ONLY_CURRENT_PROJECT, m_autoFormatOnlyCurrentProject, true); - s->endGroup(); - s->endGroup(); -} - -Utils::FilePath CMakeFormatterSettings::command() const -{ - return Utils::FilePath::fromString(m_command); -} - -void CMakeFormatterSettings::setCommand(const QString &cmd) -{ - if (cmd == m_command) - return; - - m_command = cmd; -} - -bool CMakeFormatterSettings::autoFormatOnSave() const -{ - return m_autoFormatOnSave; -} - -void CMakeFormatterSettings::setAutoFormatOnSave(bool autoFormatOnSave) -{ - m_autoFormatOnSave = autoFormatOnSave; -} - -QStringList CMakeFormatterSettings::autoFormatMime() const -{ - return m_autoFormatMime; -} - -QString CMakeFormatterSettings::autoFormatMimeAsString() const -{ - return m_autoFormatMime.join("; "); -} - -void CMakeFormatterSettings::setAutoFormatMime(const QStringList &autoFormatMime) -{ - if (m_autoFormatMime == autoFormatMime) - return; - - m_autoFormatMime = autoFormatMime; - emit supportedMimeTypesChanged(); -} - -void CMakeFormatterSettings::setAutoFormatMime(const QString &mimeList) -{ - setAutoFormatMime(mimeList.split(';')); -} - -bool CMakeFormatterSettings::autoFormatOnlyCurrentProject() const -{ - return m_autoFormatOnlyCurrentProject; -} - -void CMakeFormatterSettings::setAutoFormatOnlyCurrentProject(bool autoFormatOnlyCurrentProject) -{ - m_autoFormatOnlyCurrentProject = autoFormatOnlyCurrentProject; -} - -bool CMakeFormatterSettings::isApplicable(const Core::IDocument *document) const -{ - if (!document) - return false; - - if (m_autoFormatMime.isEmpty()) - return true; - - const Utils::MimeType documentMimeType = Utils::mimeTypeForName(document->mimeType()); - return Utils::anyOf(m_autoFormatMime, [&documentMimeType](const QString &mime) { - return documentMimeType.inherits(mime); - }); -} - -} // namespace Internal -} // namespace CMakeProjectManager diff --git a/src/plugins/cmakeprojectmanager/cmakeformattersettings.h b/src/plugins/cmakeprojectmanager/cmakeformattersettings.h deleted file mode 100644 index ea8a3ba162..0000000000 --- a/src/plugins/cmakeprojectmanager/cmakeformattersettings.h +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (C) 2016 Lorenz Haas -// Copyright (C) 2022 Xavier BESSON -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 - -#pragma once - -#include <QList> -#include <QObject> -#include <QVersionNumber> - -namespace Core { class IDocument; } -namespace Utils { class FilePath; } - -namespace CMakeProjectManager { -namespace Internal { - -class VersionUpdater; - -class CMakeFormatterSettings : public QObject -{ - Q_OBJECT -public: - explicit CMakeFormatterSettings(QObject* parent = nullptr); - static CMakeFormatterSettings *instance(); - - void read(); - void save(); - - Utils::FilePath command() const; - void setCommand(const QString &cmd); - - bool autoFormatOnSave() const; - void setAutoFormatOnSave(bool autoFormatOnSave); - - QStringList autoFormatMime() const; - QString autoFormatMimeAsString() const; - void setAutoFormatMime(const QStringList &autoFormatMime); - void setAutoFormatMime(const QString &mimeList); - - bool autoFormatOnlyCurrentProject() const; - void setAutoFormatOnlyCurrentProject(bool autoFormatOnlyCurrentProject); - - bool isApplicable(const Core::IDocument *document) const; - -signals: - void supportedMimeTypesChanged(); - -private: - QString m_command; - - bool m_autoFormatOnSave = false; - bool m_autoFormatOnlyCurrentProject = true; - QString m_autoFormatTool; - QStringList m_autoFormatMime; -}; - -} // namespace Internal -} // namespace CMakeProjectManager diff --git a/src/plugins/cmakeprojectmanager/cmakeprojectmanager.qbs b/src/plugins/cmakeprojectmanager/cmakeprojectmanager.qbs index f3eca55129..79d8c219f2 100644 --- a/src/plugins/cmakeprojectmanager/cmakeprojectmanager.qbs +++ b/src/plugins/cmakeprojectmanager/cmakeprojectmanager.qbs @@ -35,10 +35,6 @@ QtcPlugin { "cmakefilecompletionassist.h", "cmakeformatter.cpp", "cmakeformatter.h", - "cmakeformatteroptionspage.cpp", - "cmakeformatteroptionspage.h", - "cmakeformattersettings.cpp", - "cmakeformattersettings.h", "cmakeinstallstep.cpp", "cmakeinstallstep.h", "cmakekitinformation.h", diff --git a/src/plugins/cmakeprojectmanager/cmakeprojectplugin.cpp b/src/plugins/cmakeprojectmanager/cmakeprojectplugin.cpp index 109a44bf9c..39e3ec2926 100644 --- a/src/plugins/cmakeprojectmanager/cmakeprojectplugin.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeprojectplugin.cpp @@ -8,7 +8,6 @@ #include "cmakebuildsystem.h" #include "cmakeeditor.h" #include "cmakeformatter.h" -#include "cmakeformattersettings.h" #include "cmakeinstallstep.h" #include "cmakekitinformation.h" #include "cmakelocatorfilter.h" @@ -43,27 +42,9 @@ using namespace Utils; namespace CMakeProjectManager::Internal { -bool isAutoFormatApplicable(const Core::IDocument *document, const QStringList &allowedMimeTypes) -{ - if (!document) - return false; - - if (allowedMimeTypes.isEmpty()) - return true; - - const Utils::MimeType documentMimeType = Utils::mimeTypeForName(document->mimeType()); - return Utils::anyOf(allowedMimeTypes, [&documentMimeType](const QString &mime) { - return documentMimeType.inherits(mime); - }); -} - class CMakeProjectPluginPrivate : public QObject { public: - CMakeProjectPluginPrivate(); - void updateActions(Core::IEditor *editor = nullptr); - void autoFormatOnSave(Core::IDocument *document); - CMakeToolManager cmakeToolManager; // have that before the first CMakeKitAspect ParameterAction buildTargetContextAction{ @@ -90,53 +71,6 @@ public: CMakeFormatter cmakeFormatter; }; -CMakeProjectPluginPrivate::CMakeProjectPluginPrivate() -{ - const Core::EditorManager *editorManager = Core::EditorManager::instance(); - QObject::connect(editorManager, &Core::EditorManager::currentEditorChanged, - this, &CMakeProjectPluginPrivate::updateActions); - QObject::connect(editorManager, &Core::EditorManager::aboutToSave, - this, &CMakeProjectPluginPrivate::autoFormatOnSave); -} - -void CMakeProjectPluginPrivate::updateActions(Core::IEditor *editor) -{ - cmakeFormatter.updateActions(editor); -} - -void CMakeProjectPluginPrivate::autoFormatOnSave(Core::IDocument *document) -{ - if (!CMakeFormatterSettings::instance()->autoFormatOnSave()) - return; - - if (!isAutoFormatApplicable(document, CMakeFormatterSettings::instance()->autoFormatMime())) - return; - - // Check if file is contained in the current project (if wished) - if (CMakeFormatterSettings::instance()->autoFormatOnlyCurrentProject()) { - const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject(); - if (!pro || pro->files([document](const ProjectExplorer::Node *n) { - return ProjectExplorer::Project::SourceFiles(n) - && n->filePath() == document->filePath(); - }).isEmpty()) { - return; - } - } - - if (!cmakeFormatter.isApplicable(document)) - return; - const TextEditor::Command command = cmakeFormatter.command(); - if (!command.isValid()) - return; - const QList<Core::IEditor *> editors = Core::DocumentModel::editorsForDocument(document); - if (editors.isEmpty()) - return; - IEditor *currentEditor = EditorManager::currentEditor(); - IEditor *editor = editors.contains(currentEditor) ? currentEditor : editors.first(); - if (auto widget = TextEditor::TextEditorWidget::fromEditor(editor)) - TextEditor::formatEditor(widget, command); -} - CMakeProjectPlugin::~CMakeProjectPlugin() { delete d; @@ -177,14 +111,6 @@ void CMakeProjectPlugin::initialize() bs->buildCMakeTarget(targetNode ? targetNode->displayName() : QString()); } }); - - Core::ActionContainer *menu = Core::ActionManager::createMenu(Constants::CMAKEFORMATTER_MENU_ID); - menu->menu()->setTitle(Tr::tr("CMakeFormatter")); - menu->setOnAllDisabledBehavior(Core::ActionContainer::Show); - Core::ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(menu); - - d->cmakeFormatter.initialize(); - d->updateActions(); } void CMakeProjectPlugin::extensionsInitialized() |