Beautifier: Let tools self-register centrally

Change-Id: Id9140747e7c7f7c7ef3a0a957570863de7a628d1
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2023-07-07 09:26:22 +02:00
parent 47ac604aea
commit c92f4bcbdf
3 changed files with 24 additions and 11 deletions

View File

@@ -76,12 +76,6 @@ public:
ArtisticStyle artisticStyleBeautifier;
ClangFormat clangFormatBeautifier;
Uncrustify uncrustifyBeautifier;
BeautifierTool *m_tools[3] {
&artisticStyleBeautifier,
&uncrustifyBeautifier,
&clangFormatBeautifier
};
};
static BeautifierPluginPrivate *dd = nullptr;
@@ -108,7 +102,7 @@ ExtensionSystem::IPlugin::ShutdownFlag BeautifierPlugin::aboutToShutdown()
BeautifierPluginPrivate::BeautifierPluginPrivate()
{
for (BeautifierTool *tool : m_tools)
for (BeautifierTool *tool : BeautifierTool::allTools())
generalSettings.autoFormatTools.addOption(tool->id());
updateActions();
@@ -122,7 +116,7 @@ BeautifierPluginPrivate::BeautifierPluginPrivate()
void BeautifierPluginPrivate::updateActions(Core::IEditor *editor)
{
for (BeautifierTool *tool : m_tools)
for (BeautifierTool *tool : BeautifierTool::allTools())
tool->updateActions(editor);
}
@@ -149,9 +143,10 @@ void BeautifierPluginPrivate::autoFormatOnSave(Core::IDocument *document)
// Find tool to use by id and format file!
const QString id = generalSettings.autoFormatTools.stringValue();
auto tool = std::find_if(std::begin(m_tools), std::end(m_tools),
const QList<BeautifierTool *> &tools = BeautifierTool::allTools();
auto tool = std::find_if(std::begin(tools), std::end(tools),
[&id](const BeautifierTool *t){return t->id() == id;});
if (tool != std::end(m_tools)) {
if (tool != std::end(tools)) {
if (!(*tool)->isApplicable(document))
return;
const TextEditor::Command command = (*tool)->command();

View File

@@ -25,6 +25,22 @@ using namespace Utils;
namespace Beautifier::Internal {
static QList<BeautifierTool *> &theTools()
{
static QList<BeautifierTool *> tools;
return tools;
}
BeautifierTool::BeautifierTool()
{
theTools().append(this);
}
const QList<BeautifierTool *> &BeautifierTool::allTools()
{
return theTools();
}
void BeautifierTool::showError(const QString &error)
{
Core::MessageManager::writeFlashing(Tr::tr("Error in Beautifier: %1").arg(error.trimmed()));

View File

@@ -29,7 +29,9 @@ namespace Beautifier::Internal {
class BeautifierTool : public QObject
{
public:
BeautifierTool() = default;
BeautifierTool();
static const QList<BeautifierTool *> &allTools();
virtual QString id() const = 0;
virtual void updateActions(Core::IEditor *editor) = 0;