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 "../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));
}

View File

@@ -19,14 +19,17 @@
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/messagemanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/projecttree.h>
#include <texteditor/formattexteditor.h>
#include <texteditor/textdocument.h>
#include <texteditor/textdocumentlayout.h>
#include <texteditor/texteditor.h>
#include <texteditor/texteditorconstants.h>
#include <utils/algorithm.h>
#include <utils/fileutils.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

View File

@@ -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;

View File

@@ -4,11 +4,11 @@
#include "beautifiertool.h"
#include "beautifierconstants.h"
#include "beautifierplugin.h"
#include "beautifiertr.h"
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h>
#include <coreplugin/messagemanager.h>
#include <utils/algorithm.h>
#include <utils/fileutils.h>
@@ -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,20 +299,20 @@ 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.")
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.")
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.")
BeautifierTool::showError(Tr::tr("Cannot save file \"%1\": %2.")
.arg(saver.filePath().toUserOutput())
.arg(saver.errorString()));
}
@@ -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,7 +352,7 @@ void AbstractSettings::readDocumentation()
createDocumentationFile();
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()));
return;
}
@@ -315,7 +361,7 @@ 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.")
BeautifierTool::showError(Tr::tr("The file \"%1\" is not a valid documentation file.")
.arg(filename.toUserOutput()));
return;
}
@@ -346,7 +392,7 @@ void AbstractSettings::readDocumentation()
}
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()));
}
}

View File

@@ -15,7 +15,6 @@
#include <QMap>
#include <QObject>
#include <QSet>
#include <QString>
#include <QStringList>
#include <memory>
@@ -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;

View File

@@ -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);

View File

@@ -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;
}