diff --git a/src/plugins/clangformat/clangformatglobalconfigwidget.cpp b/src/plugins/clangformat/clangformatglobalconfigwidget.cpp index ac1cae84742..f6e04576919 100644 --- a/src/plugins/clangformat/clangformatglobalconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatglobalconfigwidget.cpp @@ -3,14 +3,21 @@ #include "clangformatglobalconfigwidget.h" +#include "clangformatconfigwidget.h" #include "clangformatconstants.h" +#include "clangformatindenter.h" #include "clangformatsettings.h" #include "clangformattr.h" #include "clangformatutils.h" +#include +#include + #include #include + #include +#include #include @@ -20,7 +27,9 @@ #include #include +using namespace CppEditor; using namespace ProjectExplorer; +using namespace TextEditor; using namespace Utils; namespace ClangFormat { @@ -299,4 +308,38 @@ void ClangFormatGlobalConfigWidget::finish() !ClangFormatSettings::instance().useCustomSettings()); } +class ClangFormatStyleFactory final : public CppCodeStylePreferencesFactory +{ +public: + Indenter *createIndenter(QTextDocument *doc) const final + { + return new ClangFormatForwardingIndenter(doc); + } + + std::pair additionalTab( + ICodeStylePreferences *codeStyle, Project *project, QWidget *parent) const final + { + return {createClangFormatConfigWidget(codeStyle, project, parent), Tr::tr("ClangFormat")}; + } + + CodeStyleEditorWidget *createAdditionalGlobalSettings( + ICodeStylePreferences *codeStyle, Project *project, QWidget *parent) final + { + return new ClangFormatGlobalConfigWidget(codeStyle, project, parent); + } +}; + +void setupClangFormatStyleFactory(QObject *guard) +{ + static ClangFormatStyleFactory theClangFormatStyleFactory; + + // Replace the default one. + TextEditorSettings::unregisterCodeStyleFactory(CppEditor::Constants::CPP_SETTINGS_ID); + TextEditorSettings::registerCodeStyleFactory(&theClangFormatStyleFactory); + + QObject::connect(guard, &QObject::destroyed, [] { + TextEditorSettings::unregisterCodeStyleFactory(CppEditor::Constants::CPP_SETTINGS_ID); + }); +} + } // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatglobalconfigwidget.h b/src/plugins/clangformat/clangformatglobalconfigwidget.h index e4ba4653349..d262f27f950 100644 --- a/src/plugins/clangformat/clangformatglobalconfigwidget.h +++ b/src/plugins/clangformat/clangformatglobalconfigwidget.h @@ -61,4 +61,6 @@ private: QLabel *m_currentProjectLabel; }; +void setupClangFormatStyleFactory(QObject *guard); + } // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatplugin.cpp b/src/plugins/clangformat/clangformatplugin.cpp index 7d43a1e23a9..56f50207939 100644 --- a/src/plugins/clangformat/clangformatplugin.cpp +++ b/src/plugins/clangformat/clangformatplugin.cpp @@ -1,61 +1,27 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 -#include "clangformatconfigwidget.h" #include "clangformatconstants.h" #include "clangformatglobalconfigwidget.h" -#include "clangformatindenter.h" #include "clangformattr.h" -#include "clangformatutils.h" #include "tests/clangformat-test.h" #include #include -#include #include #include #include -#include #include #include -#include - -#include -#include - -#include - using namespace Core; -using namespace CppEditor; -using namespace ProjectExplorer; -using namespace TextEditor; using namespace Utils; namespace ClangFormat { -class ClangFormatStyleFactory final : public CppCodeStylePreferencesFactory -{ -public: - Indenter *createIndenter(QTextDocument *doc) const override - { - return new ClangFormatForwardingIndenter(doc); - } - - std::pair additionalTab( - ICodeStylePreferences *codeStyle, Project *project, QWidget *parent) const override - { - return {createClangFormatConfigWidget(codeStyle, project, parent), Tr::tr("ClangFormat")}; - } - - CodeStyleEditorWidget *createAdditionalGlobalSettings( - ICodeStylePreferences *codeStyle, Project *project, QWidget *parent) override - { - return new ClangFormatGlobalConfigWidget(codeStyle, project, parent); - } -}; +FilePath configForFile(const FilePath &fileName); class ClangFormatPlugin final : public ExtensionSystem::IPlugin { @@ -64,15 +30,11 @@ class ClangFormatPlugin final : public ExtensionSystem::IPlugin ~ClangFormatPlugin() final { - TextEditorSettings::unregisterCodeStyleFactory(CppEditor::Constants::CPP_SETTINGS_ID); - delete m_factory; } void initialize() final { - TextEditorSettings::unregisterCodeStyleFactory(CppEditor::Constants::CPP_SETTINGS_ID); - m_factory = new ClangFormatStyleFactory; - TextEditorSettings::registerCodeStyleFactory(m_factory); + setupClangFormatStyleFactory(this); // This overrides the default, see implementation. ActionContainer *contextMenu = ActionManager::actionContainer(CppEditor::Constants::M_CONTEXT); if (contextMenu) { @@ -94,8 +56,6 @@ class ClangFormatPlugin final : public ExtensionSystem::IPlugin addTestCreator(Internal::createClangFormatTest); #endif } - - TextEditor::ICodeStylePreferencesFactory *m_factory = nullptr; }; } // ClangFormat