Beautifier: Move functions from settings to tools [3/3]

Change-Id: I579af5bf7db6f8ed50efdadf7037affdda458c74
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2023-07-07 09:00:17 +02:00
parent d672d70732
commit 5545c93ad1
7 changed files with 89 additions and 93 deletions

View File

@@ -6,7 +6,6 @@
#include "artisticstyle.h" #include "artisticstyle.h"
#include "../beautifierconstants.h" #include "../beautifierconstants.h"
#include "../beautifierplugin.h"
#include "../beautifiertool.h" #include "../beautifiertool.h"
#include "../beautifiertr.h" #include "../beautifiertr.h"
#include "../configurationpanel.h" #include "../configurationpanel.h"
@@ -64,7 +63,7 @@ public:
setVersionRegExp(QRegularExpression("([2-9]{1})\\.([0-9]{1,2})(\\.[1-9]{1})?$")); setVersionRegExp(QRegularExpression("([2-9]{1})\\.([0-9]{1,2})(\\.[1-9]{1})?$"));
command.setLabelText(Tr::tr("Artistic Style command:")); command.setLabelText(Tr::tr("Artistic Style command:"));
command.setDefaultValue("astyle"); command.setDefaultValue("astyle");
command.setPromptDialogTitle(BeautifierPlugin::msgCommandPromptDialogTitle(displayName())); command.setPromptDialogTitle(BeautifierTool::msgCommandPromptDialogTitle(displayName()));
useOtherFiles.setSettingsKey("useOtherFiles"); useOtherFiles.setSettingsKey("useOtherFiles");
useOtherFiles.setLabelText(Tr::tr("Use file *.astylerc defined in project files")); 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"); Core::ActionContainer *menu = Core::ActionManager::createMenu("ArtisticStyle.Menu");
menu->menu()->setTitle(Tr::tr("&Artistic Style")); 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")); menu->addAction(Core::ActionManager::registerAction(m_formatFile, "ArtisticStyle.FormatFile"));
connect(m_formatFile, &QAction::triggered, this, &ArtisticStyle::formatFile); connect(m_formatFile, &QAction::triggered, this, &ArtisticStyle::formatFile);
@@ -279,7 +278,7 @@ void ArtisticStyle::formatFile()
{ {
const QString cfgFileName = configurationFile(); const QString cfgFileName = configurationFile();
if (cfgFileName.isEmpty()) if (cfgFileName.isEmpty())
BeautifierPlugin::showError(BeautifierPlugin::msgCannotGetConfigurationFile(displayName())); showError(msgCannotGetConfigurationFile(displayName()));
else else
formatCurrentFile(command(cfgFileName)); formatCurrentFile(command(cfgFileName));
} }

View File

@@ -19,14 +19,17 @@
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h> #include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/messagemanager.h> #include <coreplugin/messagemanager.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/projectnodes.h> #include <projectexplorer/projectnodes.h>
#include <projectexplorer/projecttree.h> #include <projectexplorer/projecttree.h>
#include <texteditor/formattexteditor.h> #include <texteditor/formattexteditor.h>
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <texteditor/textdocumentlayout.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>
@@ -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 } // Beautifier::Internal

View File

@@ -13,17 +13,6 @@ class BeautifierPlugin : public ExtensionSystem::IPlugin
Q_OBJECT Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Beautifier.json") 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 initialize() override;
void extensionsInitialized() override; void extensionsInitialized() override;
ShutdownFlag aboutToShutdown() override; ShutdownFlag aboutToShutdown() override;

View File

@@ -4,11 +4,11 @@
#include "beautifiertool.h" #include "beautifiertool.h"
#include "beautifierconstants.h" #include "beautifierconstants.h"
#include "beautifierplugin.h"
#include "beautifiertr.h" #include "beautifiertr.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/idocument.h> #include <coreplugin/idocument.h>
#include <coreplugin/messagemanager.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
@@ -26,6 +26,52 @@ using namespace Utils;
namespace Beautifier::Internal { 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 class VersionUpdater
{ {
public: public:
@@ -90,7 +136,7 @@ AbstractSettings::AbstractSettings(const QString &name, const QString &ending)
command.setSettingsKey("command"); command.setSettingsKey("command");
command.setExpectedKind(PathChooser::ExistingCommand); command.setExpectedKind(PathChooser::ExistingCommand);
command.setCommandVersionArguments({"--version"}); command.setCommandVersionArguments({"--version"});
command.setPromptDialogTitle(BeautifierPlugin::msgCommandPromptDialogTitle("Clang Format")); command.setPromptDialogTitle(BeautifierTool::msgCommandPromptDialogTitle("Clang Format"));
command.setValidatePlaceHolder(true); command.setValidatePlaceHolder(true);
supportedMimeTypes.setDisplayStyle(StringAspect::LineEditDisplay); supportedMimeTypes.setDisplayStyle(StringAspect::LineEditDisplay);
@@ -253,20 +299,20 @@ void AbstractSettings::save()
const QFileInfo fi(styleFileName(iStyles.key())); const QFileInfo fi(styleFileName(iStyles.key()));
if (!(m_styleDir.mkpath(fi.absolutePath()))) { if (!(m_styleDir.mkpath(fi.absolutePath()))) {
BeautifierPlugin::showError(Tr::tr("Cannot save styles. %1 does not exist.") BeautifierTool::showError(Tr::tr("Cannot save styles. %1 does not exist.")
.arg(fi.absolutePath())); .arg(fi.absolutePath()));
continue; continue;
} }
FileSaver saver(FilePath::fromUserInput(fi.absoluteFilePath())); FileSaver saver(FilePath::fromUserInput(fi.absoluteFilePath()));
if (saver.hasError()) { if (saver.hasError()) {
BeautifierPlugin::showError(Tr::tr("Cannot open file \"%1\": %2.") BeautifierTool::showError(Tr::tr("Cannot open file \"%1\": %2.")
.arg(saver.filePath().toUserOutput()) .arg(saver.filePath().toUserOutput())
.arg(saver.errorString())); .arg(saver.errorString()));
} else { } else {
saver.write(iStyles.value().toLocal8Bit()); saver.write(iStyles.value().toLocal8Bit());
if (!saver.finalize()) { if (!saver.finalize()) {
BeautifierPlugin::showError(Tr::tr("Cannot save file \"%1\": %2.") BeautifierTool::showError(Tr::tr("Cannot save file \"%1\": %2.")
.arg(saver.filePath().toUserOutput()) .arg(saver.filePath().toUserOutput())
.arg(saver.errorString())); .arg(saver.errorString()));
} }
@@ -297,7 +343,7 @@ void AbstractSettings::readDocumentation()
{ {
const FilePath filename = documentationFilePath; const FilePath filename = documentationFilePath;
if (filename.isEmpty()) { if (filename.isEmpty()) {
BeautifierPlugin::showError(Tr::tr("No documentation file specified.")); BeautifierTool::showError(Tr::tr("No documentation file specified."));
return; return;
} }
@@ -306,7 +352,7 @@ void AbstractSettings::readDocumentation()
createDocumentationFile(); createDocumentationFile();
if (!file.open(QIODevice::ReadOnly)) { if (!file.open(QIODevice::ReadOnly)) {
BeautifierPlugin::showError(Tr::tr("Cannot open documentation file \"%1\".") BeautifierTool::showError(Tr::tr("Cannot open documentation file \"%1\".")
.arg(filename.toUserOutput())); .arg(filename.toUserOutput()));
return; return;
} }
@@ -315,7 +361,7 @@ void AbstractSettings::readDocumentation()
if (!xml.readNextStartElement()) if (!xml.readNextStartElement())
return; return;
if (xml.name() != QLatin1String(Constants::DOCUMENTATION_XMLROOT)) { if (xml.name() != QLatin1String(Constants::DOCUMENTATION_XMLROOT)) {
BeautifierPlugin::showError(Tr::tr("The file \"%1\" is not a valid documentation file.") BeautifierTool::showError(Tr::tr("The file \"%1\" is not a valid documentation file.")
.arg(filename.toUserOutput())); .arg(filename.toUserOutput()));
return; return;
} }
@@ -346,7 +392,7 @@ void AbstractSettings::readDocumentation()
} }
if (xml.hasError()) { if (xml.hasError()) {
BeautifierPlugin::showError(Tr::tr("Cannot read documentation file \"%1\": %2.") BeautifierTool::showError(Tr::tr("Cannot read documentation file \"%1\": %2.")
.arg(filename.toUserOutput()).arg(xml.errorString())); .arg(filename.toUserOutput()).arg(xml.errorString()));
} }
} }

View File

@@ -15,7 +15,6 @@
#include <QMap> #include <QMap>
#include <QObject> #include <QObject>
#include <QSet> #include <QSet>
#include <QString>
#include <QStringList> #include <QStringList>
#include <memory> #include <memory>
@@ -48,6 +47,15 @@ public:
virtual TextEditor::Command command() const = 0; virtual TextEditor::Command command() const = 0;
virtual bool isApplicable(const Core::IDocument *document) 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; class VersionUpdater;

View File

@@ -6,7 +6,6 @@
#include "clangformat.h" #include "clangformat.h"
#include "../beautifierconstants.h" #include "../beautifierconstants.h"
#include "../beautifierplugin.h"
#include "../beautifiertool.h" #include "../beautifiertool.h"
#include "../beautifiertr.h" #include "../beautifiertr.h"
#include "../configurationpanel.h" #include "../configurationpanel.h"
@@ -51,11 +50,11 @@ const char SETTINGS_NAME[] = "clangformat";
class ClangFormatSettings : public AbstractSettings class ClangFormatSettings : public AbstractSettings
{ {
public: public:
explicit ClangFormatSettings() ClangFormatSettings()
: AbstractSettings(SETTINGS_NAME, ".clang-format") : AbstractSettings(SETTINGS_NAME, ".clang-format")
{ {
command.setDefaultValue("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:")); command.setLabelText(Tr::tr("Clang Format command:"));
usePredefinedStyle.setSettingsKey("usePredefinedStyle"); usePredefinedStyle.setSettingsKey("usePredefinedStyle");
@@ -333,24 +332,23 @@ ClangFormat::ClangFormat()
Core::ActionContainer *menu = Core::ActionManager::createMenu("ClangFormat.Menu"); Core::ActionContainer *menu = Core::ActionManager::createMenu("ClangFormat.Menu");
menu->menu()->setTitle(Tr::tr("&ClangFormat")); menu->menu()->setTitle(Tr::tr("&ClangFormat"));
m_formatFile = new QAction(BeautifierPlugin::msgFormatCurrentFile(), this); m_formatFile = new QAction(msgFormatCurrentFile(), this);
Core::Command *cmd Core::Command *cmd
= Core::ActionManager::registerAction(m_formatFile, "ClangFormat.FormatFile"); = Core::ActionManager::registerAction(m_formatFile, "ClangFormat.FormatFile");
menu->addAction(cmd); menu->addAction(cmd);
connect(m_formatFile, &QAction::triggered, this, &ClangFormat::formatFile); 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"); cmd = Core::ActionManager::registerAction(m_formatLines, "ClangFormat.FormatLines");
menu->addAction(cmd); menu->addAction(cmd);
connect(m_formatLines, &QAction::triggered, this, &ClangFormat::formatLines); 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"); cmd = Core::ActionManager::registerAction(m_formatRange, "ClangFormat.FormatAtCursor");
menu->addAction(cmd); menu->addAction(cmd);
connect(m_formatRange, &QAction::triggered, this, &ClangFormat::formatAtCursor); connect(m_formatRange, &QAction::triggered, this, &ClangFormat::formatAtCursor);
m_disableFormattingSelectedText m_disableFormattingSelectedText = new QAction(msgDisableFormattingSelectedText(), this);
= new QAction(BeautifierPlugin::msgDisableFormattingSelectedText(), this);
cmd = Core::ActionManager::registerAction( cmd = Core::ActionManager::registerAction(
m_disableFormattingSelectedText, "ClangFormat.DisableFormattingSelectedText"); m_disableFormattingSelectedText, "ClangFormat.DisableFormattingSelectedText");
menu->addAction(cmd); menu->addAction(cmd);

View File

@@ -6,7 +6,6 @@
#include "uncrustify.h" #include "uncrustify.h"
#include "../beautifierconstants.h" #include "../beautifierconstants.h"
#include "../beautifierplugin.h"
#include "../beautifiertool.h" #include "../beautifiertool.h"
#include "../beautifiertr.h" #include "../beautifiertr.h"
#include "../configurationpanel.h" #include "../configurationpanel.h"
@@ -64,7 +63,7 @@ public:
command.setDefaultValue("uncrustify"); command.setDefaultValue("uncrustify");
command.setLabelText(Tr::tr("Uncrustify command:")); command.setLabelText(Tr::tr("Uncrustify command:"));
command.setPromptDialogTitle(BeautifierPlugin::msgCommandPromptDialogTitle(displayName())); command.setPromptDialogTitle(BeautifierTool::msgCommandPromptDialogTitle(displayName()));
useOtherFiles.setSettingsKey("useOtherFiles"); useOtherFiles.setSettingsKey("useOtherFiles");
useOtherFiles.setDefaultValue(true); useOtherFiles.setDefaultValue(true);
@@ -250,13 +249,13 @@ Uncrustify::Uncrustify()
Core::ActionContainer *menu = Core::ActionManager::createMenu("Uncrustify.Menu"); Core::ActionContainer *menu = Core::ActionManager::createMenu("Uncrustify.Menu");
menu->menu()->setTitle(Tr::tr("&Uncrustify")); menu->menu()->setTitle(Tr::tr("&Uncrustify"));
m_formatFile = new QAction(BeautifierPlugin::msgFormatCurrentFile(), this); m_formatFile = new QAction(msgFormatCurrentFile(), this);
Core::Command *cmd Core::Command *cmd
= Core::ActionManager::registerAction(m_formatFile, "Uncrustify.FormatFile"); = Core::ActionManager::registerAction(m_formatFile, "Uncrustify.FormatFile");
menu->addAction(cmd); menu->addAction(cmd);
connect(m_formatFile, &QAction::triggered, this, &Uncrustify::formatFile); 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"); cmd = Core::ActionManager::registerAction(m_formatRange, "Uncrustify.FormatSelectedText");
menu->addAction(cmd); menu->addAction(cmd);
connect(m_formatRange, &QAction::triggered, this, &Uncrustify::formatSelectedText); connect(m_formatRange, &QAction::triggered, this, &Uncrustify::formatSelectedText);
@@ -283,7 +282,7 @@ void Uncrustify::formatFile()
{ {
const FilePath cfgFileName = configurationFile(); const FilePath cfgFileName = configurationFile();
if (cfgFileName.isEmpty()) if (cfgFileName.isEmpty())
BeautifierPlugin::showError(BeautifierPlugin::msgCannotGetConfigurationFile(displayName())); showError(msgCannotGetConfigurationFile(displayName()));
else else
formatCurrentFile(command(cfgFileName)); formatCurrentFile(command(cfgFileName));
} }
@@ -292,7 +291,7 @@ void Uncrustify::formatSelectedText()
{ {
const FilePath cfgFileName = configurationFile(); const FilePath cfgFileName = configurationFile();
if (cfgFileName.isEmpty()) { if (cfgFileName.isEmpty()) {
BeautifierPlugin::showError(BeautifierPlugin::msgCannotGetConfigurationFile(displayName())); showError(msgCannotGetConfigurationFile(displayName()));
return; return;
} }