ClangFormat: Move ClangFormatStyleFactory creation closer to its code

Change-Id: I2378846e1dd9076b62e5d68cb75b7e0d9e4d65b0
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
hjk
2024-01-19 15:01:50 +01:00
parent 5193ab0d37
commit fddbb0e2d9
3 changed files with 47 additions and 42 deletions

View File

@@ -3,14 +3,21 @@
#include "clangformatglobalconfigwidget.h" #include "clangformatglobalconfigwidget.h"
#include "clangformatconfigwidget.h"
#include "clangformatconstants.h" #include "clangformatconstants.h"
#include "clangformatindenter.h"
#include "clangformatsettings.h" #include "clangformatsettings.h"
#include "clangformattr.h" #include "clangformattr.h"
#include "clangformatutils.h" #include "clangformatutils.h"
#include <cppeditor/cppcodestylepreferencesfactory.h>
#include <cppeditor/cppeditorconstants.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/projecttree.h> #include <projectexplorer/projecttree.h>
#include <texteditor/icodestylepreferences.h> #include <texteditor/icodestylepreferences.h>
#include <texteditor/texteditorsettings.h>
#include <utils/layoutbuilder.h> #include <utils/layoutbuilder.h>
@@ -20,7 +27,9 @@
#include <QSpinBox> #include <QSpinBox>
#include <QWidget> #include <QWidget>
using namespace CppEditor;
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace TextEditor;
using namespace Utils; using namespace Utils;
namespace ClangFormat { namespace ClangFormat {
@@ -299,4 +308,38 @@ void ClangFormatGlobalConfigWidget::finish()
!ClangFormatSettings::instance().useCustomSettings()); !ClangFormatSettings::instance().useCustomSettings());
} }
class ClangFormatStyleFactory final : public CppCodeStylePreferencesFactory
{
public:
Indenter *createIndenter(QTextDocument *doc) const final
{
return new ClangFormatForwardingIndenter(doc);
}
std::pair<TextEditor::CodeStyleEditorWidget *, QString> 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 } // namespace ClangFormat

View File

@@ -61,4 +61,6 @@ private:
QLabel *m_currentProjectLabel; QLabel *m_currentProjectLabel;
}; };
void setupClangFormatStyleFactory(QObject *guard);
} // namespace ClangFormat } // namespace ClangFormat

View File

@@ -1,61 +1,27 @@
// Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "clangformatconfigwidget.h"
#include "clangformatconstants.h" #include "clangformatconstants.h"
#include "clangformatglobalconfigwidget.h" #include "clangformatglobalconfigwidget.h"
#include "clangformatindenter.h"
#include "clangformattr.h" #include "clangformattr.h"
#include "clangformatutils.h"
#include "tests/clangformat-test.h" #include "tests/clangformat-test.h"
#include <coreplugin/actionmanager/actioncontainer.h> #include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h> #include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/idocument.h> #include <coreplugin/idocument.h>
#include <cppeditor/cppcodestylepreferencesfactory.h>
#include <cppeditor/cppeditorconstants.h> #include <cppeditor/cppeditorconstants.h>
#include <extensionsystem/iplugin.h> #include <extensionsystem/iplugin.h>
#include <projectexplorer/project.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/texteditorsettings.h>
#include <QAction>
using namespace Core; using namespace Core;
using namespace CppEditor;
using namespace ProjectExplorer;
using namespace TextEditor;
using namespace Utils; using namespace Utils;
namespace ClangFormat { namespace ClangFormat {
class ClangFormatStyleFactory final : public CppCodeStylePreferencesFactory FilePath configForFile(const FilePath &fileName);
{
public:
Indenter *createIndenter(QTextDocument *doc) const override
{
return new ClangFormatForwardingIndenter(doc);
}
std::pair<TextEditor::CodeStyleEditorWidget *, QString> 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);
}
};
class ClangFormatPlugin final : public ExtensionSystem::IPlugin class ClangFormatPlugin final : public ExtensionSystem::IPlugin
{ {
@@ -64,15 +30,11 @@ class ClangFormatPlugin final : public ExtensionSystem::IPlugin
~ClangFormatPlugin() final ~ClangFormatPlugin() final
{ {
TextEditorSettings::unregisterCodeStyleFactory(CppEditor::Constants::CPP_SETTINGS_ID);
delete m_factory;
} }
void initialize() final void initialize() final
{ {
TextEditorSettings::unregisterCodeStyleFactory(CppEditor::Constants::CPP_SETTINGS_ID); setupClangFormatStyleFactory(this); // This overrides the default, see implementation.
m_factory = new ClangFormatStyleFactory;
TextEditorSettings::registerCodeStyleFactory(m_factory);
ActionContainer *contextMenu = ActionManager::actionContainer(CppEditor::Constants::M_CONTEXT); ActionContainer *contextMenu = ActionManager::actionContainer(CppEditor::Constants::M_CONTEXT);
if (contextMenu) { if (contextMenu) {
@@ -94,8 +56,6 @@ class ClangFormatPlugin final : public ExtensionSystem::IPlugin
addTestCreator(Internal::createClangFormatTest); addTestCreator(Internal::createClangFormatTest);
#endif #endif
} }
TextEditor::ICodeStylePreferencesFactory *m_factory = nullptr;
}; };
} // ClangFormat } // ClangFormat