diff options
author | hjk <hjk@qt.io> | 2023-11-22 14:58:56 +0100 |
---|---|---|
committer | hjk <hjk@qt.io> | 2023-11-22 17:04:33 +0000 |
commit | 8ceaa67ea8b1a168767d4f70d2c542cdc6cd0697 (patch) | |
tree | 818b0e8476f4213032d8af19f4f87f0de77e78b4 | |
parent | 27d265137f882ff3301a1bc4ecb98fa62693289d (diff) |
Beautifier: New setup also for ClangFormat and Uncrustify
Change-Id: I338e8947c09219f6a35d1361f2e10f8264b37ad3
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
-rw-r--r-- | src/plugins/beautifier/beautifierplugin.cpp | 6 | ||||
-rw-r--r-- | src/plugins/beautifier/clangformat/clangformat.cpp | 131 | ||||
-rw-r--r-- | src/plugins/beautifier/clangformat/clangformat.h | 30 | ||||
-rw-r--r-- | src/plugins/beautifier/uncrustify/uncrustify.cpp | 107 | ||||
-rw-r--r-- | src/plugins/beautifier/uncrustify/uncrustify.h | 26 |
5 files changed, 144 insertions, 156 deletions
diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp index e5929392836..7972fc67773 100644 --- a/src/plugins/beautifier/beautifierplugin.cpp +++ b/src/plugins/beautifier/beautifierplugin.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include "beautifierconstants.h" +#include "beautifiertool.h" #include "beautifiertr.h" #include "generalsettings.h" @@ -64,9 +65,6 @@ public: void updateActions(IEditor *editor = nullptr); void autoFormatOnSave(IDocument *document); - - ClangFormat clangFormatBeautifier; - Uncrustify uncrustifyBeautifier; }; BeautifierPluginPrivate::BeautifierPluginPrivate() @@ -142,6 +140,8 @@ class BeautifierPlugin final : public ExtensionSystem::IPlugin ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(menu); setupArtisticStyle(); + setupClangFormat(); + setupUncrustify(); } void extensionsInitialized() final diff --git a/src/plugins/beautifier/clangformat/clangformat.cpp b/src/plugins/beautifier/clangformat/clangformat.cpp index 6a31cbb374a..9e4721da04e 100644 --- a/src/plugins/beautifier/clangformat/clangformat.cpp +++ b/src/plugins/beautifier/clangformat/clangformat.cpp @@ -92,19 +92,19 @@ public: read(); } - void createDocumentationFile() const override; + void createDocumentationFile() const final; - QStringList completerWords() override; + QStringList completerWords() final; BoolAspect usePredefinedStyle{this}; SelectionAspect predefinedStyle{this}; SelectionAspect fallbackStyle{this}; StringAspect customStyle{this}; - Utils::FilePath styleFileName(const QString &key) const override; + Utils::FilePath styleFileName(const QString &key) const final; private: - void readStyles() override; + void readStyles() final; }; void ClangFormatSettings::createDocumentationFile() const @@ -310,53 +310,77 @@ public: // ClangFormat -ClangFormat::ClangFormat() +class ClangFormat final : public BeautifierTool { - const Id menuId = "ClangFormat.Menu"; - Core::ActionContainer *menu = Core::ActionManager::createMenu(menuId); - menu->menu()->setTitle(Tr::tr("&ClangFormat")); - - Core::ActionBuilder formatFile(this, "ClangFormat.FormatFile"); - formatFile.setText(msgFormatCurrentFile()); - formatFile.bindContextAction(&m_formatFile); - formatFile.setContainer(menuId); - formatFile.setOnTriggered(this, [this] { this->formatFile(); }); - - Core::ActionBuilder formatLines(this, "ClangFormat.FormatLines"); - formatLines.setText(msgFormatLines()); - formatLines.bindContextAction(&m_formatLines); - formatLines.setContainer(menuId); - formatLines.setOnTriggered(this, [this] { this->formatLines(); }); - - Core::ActionBuilder formatAtCursor(this, "ClangFormat.FormatAtCursor"); - formatAtCursor.setText(msgFormatAtCursor()); - formatAtCursor.bindContextAction(&m_formatRange); - formatAtCursor.setContainer(menuId); - formatAtCursor.setOnTriggered(this, [this] { this->formatAtCursor(); }); - - Core::ActionBuilder formatDisable(this, "ClangFormat.DisableFormattingSelectedText"); - formatDisable.setText(msgDisableFormattingSelectedText()); - formatDisable.bindContextAction(&m_disableFormattingSelectedText); - formatDisable.setContainer(menuId); - formatDisable.setOnTriggered(this, [this] { disableFormattingSelectedText(); }); - - Core::ActionManager::actionContainer(Constants::MENU_ID)->addMenu(menu); - - connect(&settings().supportedMimeTypes, &BaseAspect::changed, - this, [this] { updateActions(Core::EditorManager::currentEditor()); }); -} +public: + ClangFormat() + { + const Id menuId = "ClangFormat.Menu"; + Core::ActionContainer *menu = Core::ActionManager::createMenu(menuId); + menu->menu()->setTitle(Tr::tr("&ClangFormat")); + + Core::ActionBuilder formatFile(this, "ClangFormat.FormatFile"); + formatFile.setText(msgFormatCurrentFile()); + formatFile.bindContextAction(&m_formatFile); + formatFile.setContainer(menuId); + formatFile.setOnTriggered(this, [this] { this->formatFile(); }); + + Core::ActionBuilder formatLines(this, "ClangFormat.FormatLines"); + formatLines.setText(msgFormatLines()); + formatLines.bindContextAction(&m_formatLines); + formatLines.setContainer(menuId); + formatLines.setOnTriggered(this, [this] { this->formatLines(); }); + + Core::ActionBuilder formatAtCursor(this, "ClangFormat.FormatAtCursor"); + formatAtCursor.setText(msgFormatAtCursor()); + formatAtCursor.bindContextAction(&m_formatRange); + formatAtCursor.setContainer(menuId); + formatAtCursor.setOnTriggered(this, [this] { this->formatAtCursor(); }); + + Core::ActionBuilder formatDisable(this, "ClangFormat.DisableFormattingSelectedText"); + formatDisable.setText(msgDisableFormattingSelectedText()); + formatDisable.bindContextAction(&m_disableFormattingSelectedText); + formatDisable.setContainer(menuId); + formatDisable.setOnTriggered(this, [this] { disableFormattingSelectedText(); }); + + Core::ActionManager::actionContainer(Constants::MENU_ID)->addMenu(menu); + + connect(&settings().supportedMimeTypes, &BaseAspect::changed, + this, [this] { updateActions(Core::EditorManager::currentEditor()); }); + } -QString ClangFormat::id() const -{ - return "ClangFormat"; -} + QString id() const final + { + return "ClangFormat"; + } -void ClangFormat::updateActions(Core::IEditor *editor) -{ - const bool enabled = editor && settings().isApplicable(editor->document()); - m_formatFile->setEnabled(enabled); - m_formatRange->setEnabled(enabled); -} + void updateActions(Core::IEditor *editor) final + { + const bool enabled = editor && settings().isApplicable(editor->document()); + m_formatFile->setEnabled(enabled); + m_formatRange->setEnabled(enabled); + } + + TextEditor::Command textCommand() const final; + + bool isApplicable(const Core::IDocument *document) const final + { + return settings().isApplicable(document); + } + +private: + void formatFile(); + void formatAtPosition(const int pos, const int length); + void formatAtCursor(); + void formatLines(); + void disableFormattingSelectedText(); + TextEditor::Command textCommand(int offset, int length) const; + + QAction *m_formatFile = nullptr; + QAction *m_formatLines = nullptr; + QAction *m_formatRange = nullptr; + QAction *m_disableFormattingSelectedText = nullptr; +}; void ClangFormat::formatFile() { @@ -488,11 +512,6 @@ Command ClangFormat::textCommand() const return cmd; } -bool ClangFormat::isApplicable(const Core::IDocument *document) const -{ - return settings().isApplicable(document); -} - Command ClangFormat::textCommand(int offset, int length) const { Command cmd = textCommand(); @@ -501,7 +520,6 @@ Command ClangFormat::textCommand(int offset, int length) const return cmd; } - // ClangFormatSettingsPage class ClangFormatSettingsPage final : public Core::IOptionsPage @@ -518,4 +536,9 @@ public: const ClangFormatSettingsPage settingsPage; +void setupClangFormat() +{ + static ClangFormat theClangFormat; +} + } // Beautifier::Internal diff --git a/src/plugins/beautifier/clangformat/clangformat.h b/src/plugins/beautifier/clangformat/clangformat.h index 82f6ac160b9..8c54a1939f2 100644 --- a/src/plugins/beautifier/clangformat/clangformat.h +++ b/src/plugins/beautifier/clangformat/clangformat.h @@ -3,36 +3,8 @@ #pragma once -#include "../beautifiertool.h" - -QT_BEGIN_NAMESPACE -class QAction; -QT_END_NAMESPACE - namespace Beautifier::Internal { -class ClangFormat : public BeautifierTool -{ -public: - ClangFormat(); - - QString id() const override; - void updateActions(Core::IEditor *editor) override; - TextEditor::Command textCommand() const override; - bool isApplicable(const Core::IDocument *document) const override; - -private: - void formatFile(); - void formatAtPosition(const int pos, const int length); - void formatAtCursor(); - void formatLines(); - void disableFormattingSelectedText(); - TextEditor::Command textCommand(int offset, int length) const; - - QAction *m_formatFile = nullptr; - QAction *m_formatLines = nullptr; - QAction *m_formatRange = nullptr; - QAction *m_disableFormattingSelectedText = nullptr; -}; +void setupClangFormat(); } // Beautifier::Internal diff --git a/src/plugins/beautifier/uncrustify/uncrustify.cpp b/src/plugins/beautifier/uncrustify/uncrustify.cpp index 340ee416f1c..d943b1daed7 100644 --- a/src/plugins/beautifier/uncrustify/uncrustify.cpp +++ b/src/plugins/beautifier/uncrustify/uncrustify.cpp @@ -97,7 +97,7 @@ public: read(); } - void createDocumentationFile() const override + void createDocumentationFile() const final { Process process; process.setTimeoutS(2); @@ -228,44 +228,67 @@ public: } }; - // Uncrustify -Uncrustify::Uncrustify() +class Uncrustify final : public BeautifierTool { - const Id menuId = "Uncrustify.Menu"; - Core::ActionContainer *menu = Core::ActionManager::createMenu(menuId); - menu->menu()->setTitle(Tr::tr("&Uncrustify")); - - Core::ActionBuilder formatFile(this, "Uncrustify.FormatFile"); - formatFile.setText(msgFormatCurrentFile()); - formatFile.bindContextAction(&m_formatFile); - formatFile.setContainer(menuId); - formatFile.setOnTriggered(this, [this] { this->formatFile(); }); - - Core::ActionBuilder formatRange(this, "Uncrustify.FormatSelectedText"); - formatRange.setText(msgFormatSelectedText()); - formatRange.bindContextAction(&m_formatRange); - formatRange.setContainer(menuId); - formatRange.setOnTriggered(this, [this] { this->formatSelectedText(); }); - - Core::ActionManager::actionContainer(Constants::MENU_ID)->addMenu(menu); - - connect(&settings().supportedMimeTypes, &Utils::BaseAspect::changed, - this, [this] { updateActions(Core::EditorManager::currentEditor()); }); -} +public: + Uncrustify() + { + const Id menuId = "Uncrustify.Menu"; + Core::ActionContainer *menu = Core::ActionManager::createMenu(menuId); + menu->menu()->setTitle(Tr::tr("&Uncrustify")); + + Core::ActionBuilder formatFile(this, "Uncrustify.FormatFile"); + formatFile.setText(msgFormatCurrentFile()); + formatFile.bindContextAction(&m_formatFile); + formatFile.setContainer(menuId); + formatFile.setOnTriggered(this, [this] { this->formatFile(); }); + + Core::ActionBuilder formatRange(this, "Uncrustify.FormatSelectedText"); + formatRange.setText(msgFormatSelectedText()); + formatRange.bindContextAction(&m_formatRange); + formatRange.setContainer(menuId); + formatRange.setOnTriggered(this, [this] { this->formatSelectedText(); }); + + Core::ActionManager::actionContainer(Constants::MENU_ID)->addMenu(menu); + + connect(&settings().supportedMimeTypes, &Utils::BaseAspect::changed, + this, [this] { updateActions(Core::EditorManager::currentEditor()); }); + } -QString Uncrustify::id() const -{ - return "Uncrustify"; -} + QString id() const final + { + return "Uncrustify"; + } -void Uncrustify::updateActions(Core::IEditor *editor) -{ - const bool enabled = editor && settings().isApplicable(editor->document()); - m_formatFile->setEnabled(enabled); - m_formatRange->setEnabled(enabled); -} + void updateActions(Core::IEditor *editor) final + { + const bool enabled = editor && settings().isApplicable(editor->document()); + m_formatFile->setEnabled(enabled); + m_formatRange->setEnabled(enabled); + } + + TextEditor::Command textCommand() const final + { + const FilePath cfgFile = configurationFile(); + return cfgFile.isEmpty() ? Command() : textCommand(cfgFile, false); + } + + bool isApplicable(const Core::IDocument *document) const final + { + return settings().isApplicable(document); + } + +private: + void formatFile(); + void formatSelectedText(); + Utils::FilePath configurationFile() const; + TextEditor::Command textCommand(const Utils::FilePath &cfgFile, bool fragment = false) const; + + QAction *m_formatFile = nullptr; + QAction *m_formatRange = nullptr; +}; void Uncrustify::formatFile() { @@ -338,17 +361,6 @@ FilePath Uncrustify::configurationFile() const return {}; } -Command Uncrustify::textCommand() const -{ - const FilePath cfgFile = configurationFile(); - return cfgFile.isEmpty() ? Command() : textCommand(cfgFile, false); -} - -bool Uncrustify::isApplicable(const Core::IDocument *document) const -{ - return settings().isApplicable(document); -} - Command Uncrustify::textCommand(const FilePath &cfgFile, bool fragment) const { Command cmd; @@ -387,4 +399,9 @@ public: const UncrustifySettingsPage settingsPage; +void setupUncrustify() +{ + static Uncrustify theUncrustify; +} + } // Beautifier::Internal diff --git a/src/plugins/beautifier/uncrustify/uncrustify.h b/src/plugins/beautifier/uncrustify/uncrustify.h index 090b06e0423..05c5ce72289 100644 --- a/src/plugins/beautifier/uncrustify/uncrustify.h +++ b/src/plugins/beautifier/uncrustify/uncrustify.h @@ -3,32 +3,8 @@ #pragma once -#include "../beautifiertool.h" - -QT_BEGIN_NAMESPACE -class QAction; -QT_END_NAMESPACE - namespace Beautifier::Internal { -class Uncrustify : public BeautifierTool -{ -public: - Uncrustify(); - - QString id() const override; - void updateActions(Core::IEditor *editor) override; - TextEditor::Command textCommand() const override; - bool isApplicable(const Core::IDocument *document) const override; - -private: - void formatFile(); - void formatSelectedText(); - Utils::FilePath configurationFile() const; - TextEditor::Command textCommand(const Utils::FilePath &cfgFile, bool fragment = false) const; - - QAction *m_formatFile = nullptr; - QAction *m_formatRange = nullptr; -}; +void setupUncrustify(); } // Beautifier::Internal |