From 5545c93ad10c8ae28ac4279d86f6764a31126a8a Mon Sep 17 00:00:00 2001 From: hjk Date: Fri, 7 Jul 2023 09:00:17 +0200 Subject: [PATCH] Beautifier: Move functions from settings to tools [3/3] Change-Id: I579af5bf7db6f8ed50efdadf7037affdda458c74 Reviewed-by: Christian Stenger --- .../artisticstyle/artisticstyle.cpp | 7 +- src/plugins/beautifier/beautifierplugin.cpp | 49 +----------- src/plugins/beautifier/beautifierplugin.h | 11 --- src/plugins/beautifier/beautifiertool.cpp | 80 +++++++++++++++---- src/plugins/beautifier/beautifiertool.h | 10 ++- .../beautifier/clangformat/clangformat.cpp | 14 ++-- .../beautifier/uncrustify/uncrustify.cpp | 11 ++- 7 files changed, 89 insertions(+), 93 deletions(-) diff --git a/src/plugins/beautifier/artisticstyle/artisticstyle.cpp b/src/plugins/beautifier/artisticstyle/artisticstyle.cpp index 8960f42fbc1..89699253a6a 100644 --- a/src/plugins/beautifier/artisticstyle/artisticstyle.cpp +++ b/src/plugins/beautifier/artisticstyle/artisticstyle.cpp @@ -6,7 +6,6 @@ #include "artisticstyle.h" #include "../beautifierconstants.h" -#include "../beautifierplugin.h" #include "../beautifiertool.h" #include "../beautifiertr.h" #include "../configurationpanel.h" @@ -64,7 +63,7 @@ public: setVersionRegExp(QRegularExpression("([2-9]{1})\\.([0-9]{1,2})(\\.[1-9]{1})?$")); command.setLabelText(Tr::tr("Artistic Style command:")); command.setDefaultValue("astyle"); - command.setPromptDialogTitle(BeautifierPlugin::msgCommandPromptDialogTitle(displayName())); + command.setPromptDialogTitle(BeautifierTool::msgCommandPromptDialogTitle(displayName())); useOtherFiles.setSettingsKey("useOtherFiles"); useOtherFiles.setLabelText(Tr::tr("Use file *.astylerc defined in project files")); @@ -255,7 +254,7 @@ ArtisticStyle::ArtisticStyle() Core::ActionContainer *menu = Core::ActionManager::createMenu("ArtisticStyle.Menu"); menu->menu()->setTitle(Tr::tr("&Artistic Style")); - m_formatFile = new QAction(BeautifierPlugin::msgFormatCurrentFile(), this); + m_formatFile = new QAction(msgFormatCurrentFile(), this); menu->addAction(Core::ActionManager::registerAction(m_formatFile, "ArtisticStyle.FormatFile")); connect(m_formatFile, &QAction::triggered, this, &ArtisticStyle::formatFile); @@ -279,7 +278,7 @@ void ArtisticStyle::formatFile() { const QString cfgFileName = configurationFile(); if (cfgFileName.isEmpty()) - BeautifierPlugin::showError(BeautifierPlugin::msgCannotGetConfigurationFile(displayName())); + showError(msgCannotGetConfigurationFile(displayName())); else formatCurrentFile(command(cfgFileName)); } diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp index fe76fd1de7d..d1505e74987 100644 --- a/src/plugins/beautifier/beautifierplugin.cpp +++ b/src/plugins/beautifier/beautifierplugin.cpp @@ -19,14 +19,17 @@ #include #include #include + #include #include #include + #include #include #include #include #include + #include #include #include @@ -162,50 +165,4 @@ void BeautifierPluginPrivate::autoFormatOnSave(Core::IDocument *document) } } -void BeautifierPlugin::showError(const QString &error) -{ - Core::MessageManager::writeFlashing(Tr::tr("Error in Beautifier: %1").arg(error.trimmed())); -} - -QString BeautifierPlugin::msgCannotGetConfigurationFile(const QString &command) -{ - return Tr::tr("Cannot get configuration file for %1.").arg(command); -} - -QString BeautifierPlugin::msgFormatCurrentFile() -{ - //: Menu entry - return Tr::tr("Format &Current File"); -} - -QString BeautifierPlugin::msgFormatSelectedText() -{ - //: Menu entry - return Tr::tr("Format &Selected Text"); -} - -QString BeautifierPlugin::msgFormatAtCursor() -{ - //: Menu entry - return Tr::tr("&Format at Cursor"); -} - -QString BeautifierPlugin::msgFormatLines() -{ - //: Menu entry - return Tr::tr("Format &Line(s)"); -} - -QString BeautifierPlugin::msgDisableFormattingSelectedText() -{ - //: Menu entry - return Tr::tr("&Disable Formatting for Selected Text"); -} - -QString BeautifierPlugin::msgCommandPromptDialogTitle(const QString &command) -{ - //: File dialog title for path chooser when choosing binary - return Tr::tr("%1 Command").arg(command); -} - } // Beautifier::Internal diff --git a/src/plugins/beautifier/beautifierplugin.h b/src/plugins/beautifier/beautifierplugin.h index 8b3d89ed210..e64403d9a7c 100644 --- a/src/plugins/beautifier/beautifierplugin.h +++ b/src/plugins/beautifier/beautifierplugin.h @@ -13,17 +13,6 @@ class BeautifierPlugin : public ExtensionSystem::IPlugin Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Beautifier.json") -public: - static QString msgCannotGetConfigurationFile(const QString &command); - static QString msgFormatCurrentFile(); - static QString msgFormatSelectedText(); - static QString msgFormatAtCursor(); - static QString msgFormatLines(); - static QString msgDisableFormattingSelectedText(); - static QString msgCommandPromptDialogTitle(const QString &command); - static void showError(const QString &error); - -private: void initialize() override; void extensionsInitialized() override; ShutdownFlag aboutToShutdown() override; diff --git a/src/plugins/beautifier/beautifiertool.cpp b/src/plugins/beautifier/beautifiertool.cpp index 5e5300fd10b..76b6e7dbd09 100644 --- a/src/plugins/beautifier/beautifiertool.cpp +++ b/src/plugins/beautifier/beautifiertool.cpp @@ -4,11 +4,11 @@ #include "beautifiertool.h" #include "beautifierconstants.h" -#include "beautifierplugin.h" #include "beautifiertr.h" #include #include +#include #include #include @@ -26,6 +26,52 @@ using namespace Utils; namespace Beautifier::Internal { +void BeautifierTool::showError(const QString &error) +{ + Core::MessageManager::writeFlashing(Tr::tr("Error in Beautifier: %1").arg(error.trimmed())); +} + +QString BeautifierTool::msgCannotGetConfigurationFile(const QString &command) +{ + return Tr::tr("Cannot get configuration file for %1.").arg(command); +} + +QString BeautifierTool::msgFormatCurrentFile() +{ + //: Menu entry + return Tr::tr("Format &Current File"); +} + +QString BeautifierTool::msgFormatSelectedText() +{ + //: Menu entry + return Tr::tr("Format &Selected Text"); +} + +QString BeautifierTool::msgFormatAtCursor() +{ + //: Menu entry + return Tr::tr("&Format at Cursor"); +} + +QString BeautifierTool::msgFormatLines() +{ + //: Menu entry + return Tr::tr("Format &Line(s)"); +} + +QString BeautifierTool::msgDisableFormattingSelectedText() +{ + //: Menu entry + return Tr::tr("&Disable Formatting for Selected Text"); +} + +QString BeautifierTool::msgCommandPromptDialogTitle(const QString &command) +{ + //: File dialog title for path chooser when choosing binary + return Tr::tr("%1 Command").arg(command); +} + class VersionUpdater { public: @@ -90,7 +136,7 @@ AbstractSettings::AbstractSettings(const QString &name, const QString &ending) command.setSettingsKey("command"); command.setExpectedKind(PathChooser::ExistingCommand); command.setCommandVersionArguments({"--version"}); - command.setPromptDialogTitle(BeautifierPlugin::msgCommandPromptDialogTitle("Clang Format")); + command.setPromptDialogTitle(BeautifierTool::msgCommandPromptDialogTitle("Clang Format")); command.setValidatePlaceHolder(true); supportedMimeTypes.setDisplayStyle(StringAspect::LineEditDisplay); @@ -253,22 +299,22 @@ void AbstractSettings::save() const QFileInfo fi(styleFileName(iStyles.key())); if (!(m_styleDir.mkpath(fi.absolutePath()))) { - BeautifierPlugin::showError(Tr::tr("Cannot save styles. %1 does not exist.") - .arg(fi.absolutePath())); + BeautifierTool::showError(Tr::tr("Cannot save styles. %1 does not exist.") + .arg(fi.absolutePath())); continue; } FileSaver saver(FilePath::fromUserInput(fi.absoluteFilePath())); if (saver.hasError()) { - BeautifierPlugin::showError(Tr::tr("Cannot open file \"%1\": %2.") - .arg(saver.filePath().toUserOutput()) - .arg(saver.errorString())); + BeautifierTool::showError(Tr::tr("Cannot open file \"%1\": %2.") + .arg(saver.filePath().toUserOutput()) + .arg(saver.errorString())); } else { saver.write(iStyles.value().toLocal8Bit()); if (!saver.finalize()) { - BeautifierPlugin::showError(Tr::tr("Cannot save file \"%1\": %2.") - .arg(saver.filePath().toUserOutput()) - .arg(saver.errorString())); + BeautifierTool::showError(Tr::tr("Cannot save file \"%1\": %2.") + .arg(saver.filePath().toUserOutput()) + .arg(saver.errorString())); } } ++iStyles; @@ -297,7 +343,7 @@ void AbstractSettings::readDocumentation() { const FilePath filename = documentationFilePath; if (filename.isEmpty()) { - BeautifierPlugin::showError(Tr::tr("No documentation file specified.")); + BeautifierTool::showError(Tr::tr("No documentation file specified.")); return; } @@ -306,8 +352,8 @@ void AbstractSettings::readDocumentation() createDocumentationFile(); if (!file.open(QIODevice::ReadOnly)) { - BeautifierPlugin::showError(Tr::tr("Cannot open documentation file \"%1\".") - .arg(filename.toUserOutput())); + BeautifierTool::showError(Tr::tr("Cannot open documentation file \"%1\".") + .arg(filename.toUserOutput())); return; } @@ -315,8 +361,8 @@ void AbstractSettings::readDocumentation() if (!xml.readNextStartElement()) return; if (xml.name() != QLatin1String(Constants::DOCUMENTATION_XMLROOT)) { - BeautifierPlugin::showError(Tr::tr("The file \"%1\" is not a valid documentation file.") - .arg(filename.toUserOutput())); + BeautifierTool::showError(Tr::tr("The file \"%1\" is not a valid documentation file.") + .arg(filename.toUserOutput())); return; } @@ -346,8 +392,8 @@ void AbstractSettings::readDocumentation() } if (xml.hasError()) { - BeautifierPlugin::showError(Tr::tr("Cannot read documentation file \"%1\": %2.") - .arg(filename.toUserOutput()).arg(xml.errorString())); + BeautifierTool::showError(Tr::tr("Cannot read documentation file \"%1\": %2.") + .arg(filename.toUserOutput()).arg(xml.errorString())); } } diff --git a/src/plugins/beautifier/beautifiertool.h b/src/plugins/beautifier/beautifiertool.h index 392b460dc70..0eb4faf6418 100644 --- a/src/plugins/beautifier/beautifiertool.h +++ b/src/plugins/beautifier/beautifiertool.h @@ -15,7 +15,6 @@ #include #include #include -#include #include #include @@ -48,6 +47,15 @@ public: virtual TextEditor::Command command() const = 0; virtual bool isApplicable(const Core::IDocument *document) const = 0; + + static QString msgCannotGetConfigurationFile(const QString &command); + static QString msgFormatCurrentFile(); + static QString msgFormatSelectedText(); + static QString msgFormatAtCursor(); + static QString msgFormatLines(); + static QString msgDisableFormattingSelectedText(); + static QString msgCommandPromptDialogTitle(const QString &command); + static void showError(const QString &error); }; class VersionUpdater; diff --git a/src/plugins/beautifier/clangformat/clangformat.cpp b/src/plugins/beautifier/clangformat/clangformat.cpp index d529ae0eb47..345cf870d50 100644 --- a/src/plugins/beautifier/clangformat/clangformat.cpp +++ b/src/plugins/beautifier/clangformat/clangformat.cpp @@ -6,7 +6,6 @@ #include "clangformat.h" #include "../beautifierconstants.h" -#include "../beautifierplugin.h" #include "../beautifiertool.h" #include "../beautifiertr.h" #include "../configurationpanel.h" @@ -51,11 +50,11 @@ const char SETTINGS_NAME[] = "clangformat"; class ClangFormatSettings : public AbstractSettings { public: - explicit ClangFormatSettings() + ClangFormatSettings() : AbstractSettings(SETTINGS_NAME, ".clang-format") { command.setDefaultValue("clang-format"); - command.setPromptDialogTitle(BeautifierPlugin::msgCommandPromptDialogTitle("Clang Format")); + command.setPromptDialogTitle(BeautifierTool::msgCommandPromptDialogTitle("Clang Format")); command.setLabelText(Tr::tr("Clang Format command:")); usePredefinedStyle.setSettingsKey("usePredefinedStyle"); @@ -333,24 +332,23 @@ ClangFormat::ClangFormat() Core::ActionContainer *menu = Core::ActionManager::createMenu("ClangFormat.Menu"); menu->menu()->setTitle(Tr::tr("&ClangFormat")); - m_formatFile = new QAction(BeautifierPlugin::msgFormatCurrentFile(), this); + m_formatFile = new QAction(msgFormatCurrentFile(), this); Core::Command *cmd = Core::ActionManager::registerAction(m_formatFile, "ClangFormat.FormatFile"); menu->addAction(cmd); connect(m_formatFile, &QAction::triggered, this, &ClangFormat::formatFile); - m_formatLines = new QAction(BeautifierPlugin::msgFormatLines(), this); + m_formatLines = new QAction(msgFormatLines(), this); cmd = Core::ActionManager::registerAction(m_formatLines, "ClangFormat.FormatLines"); menu->addAction(cmd); connect(m_formatLines, &QAction::triggered, this, &ClangFormat::formatLines); - m_formatRange = new QAction(BeautifierPlugin::msgFormatAtCursor(), this); + m_formatRange = new QAction(msgFormatAtCursor(), this); cmd = Core::ActionManager::registerAction(m_formatRange, "ClangFormat.FormatAtCursor"); menu->addAction(cmd); connect(m_formatRange, &QAction::triggered, this, &ClangFormat::formatAtCursor); - m_disableFormattingSelectedText - = new QAction(BeautifierPlugin::msgDisableFormattingSelectedText(), this); + m_disableFormattingSelectedText = new QAction(msgDisableFormattingSelectedText(), this); cmd = Core::ActionManager::registerAction( m_disableFormattingSelectedText, "ClangFormat.DisableFormattingSelectedText"); menu->addAction(cmd); diff --git a/src/plugins/beautifier/uncrustify/uncrustify.cpp b/src/plugins/beautifier/uncrustify/uncrustify.cpp index 43082d82dff..553f3875001 100644 --- a/src/plugins/beautifier/uncrustify/uncrustify.cpp +++ b/src/plugins/beautifier/uncrustify/uncrustify.cpp @@ -6,7 +6,6 @@ #include "uncrustify.h" #include "../beautifierconstants.h" -#include "../beautifierplugin.h" #include "../beautifiertool.h" #include "../beautifiertr.h" #include "../configurationpanel.h" @@ -64,7 +63,7 @@ public: command.setDefaultValue("uncrustify"); command.setLabelText(Tr::tr("Uncrustify command:")); - command.setPromptDialogTitle(BeautifierPlugin::msgCommandPromptDialogTitle(displayName())); + command.setPromptDialogTitle(BeautifierTool::msgCommandPromptDialogTitle(displayName())); useOtherFiles.setSettingsKey("useOtherFiles"); useOtherFiles.setDefaultValue(true); @@ -250,13 +249,13 @@ Uncrustify::Uncrustify() Core::ActionContainer *menu = Core::ActionManager::createMenu("Uncrustify.Menu"); menu->menu()->setTitle(Tr::tr("&Uncrustify")); - m_formatFile = new QAction(BeautifierPlugin::msgFormatCurrentFile(), this); + m_formatFile = new QAction(msgFormatCurrentFile(), this); Core::Command *cmd = Core::ActionManager::registerAction(m_formatFile, "Uncrustify.FormatFile"); menu->addAction(cmd); connect(m_formatFile, &QAction::triggered, this, &Uncrustify::formatFile); - m_formatRange = new QAction(BeautifierPlugin::msgFormatSelectedText(), this); + m_formatRange = new QAction(msgFormatSelectedText(), this); cmd = Core::ActionManager::registerAction(m_formatRange, "Uncrustify.FormatSelectedText"); menu->addAction(cmd); connect(m_formatRange, &QAction::triggered, this, &Uncrustify::formatSelectedText); @@ -283,7 +282,7 @@ void Uncrustify::formatFile() { const FilePath cfgFileName = configurationFile(); if (cfgFileName.isEmpty()) - BeautifierPlugin::showError(BeautifierPlugin::msgCannotGetConfigurationFile(displayName())); + showError(msgCannotGetConfigurationFile(displayName())); else formatCurrentFile(command(cfgFileName)); } @@ -292,7 +291,7 @@ void Uncrustify::formatSelectedText() { const FilePath cfgFileName = configurationFile(); if (cfgFileName.isEmpty()) { - BeautifierPlugin::showError(BeautifierPlugin::msgCannotGetConfigurationFile(displayName())); + showError(msgCannotGetConfigurationFile(displayName())); return; }