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 "clangformatconfigwidget.h"
#include "clangformatconstants.h"
#include "clangformatindenter.h"
#include "clangformatsettings.h"
#include "clangformattr.h"
#include "clangformatutils.h"
#include <cppeditor/cppcodestylepreferencesfactory.h>
#include <cppeditor/cppeditorconstants.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projecttree.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/texteditorsettings.h>
#include <utils/layoutbuilder.h>
@@ -20,7 +27,9 @@
#include <QSpinBox>
#include <QWidget>
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<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

View File

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

View File

@@ -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 <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/idocument.h>
#include <cppeditor/cppcodestylepreferencesfactory.h>
#include <cppeditor/cppeditorconstants.h>
#include <extensionsystem/iplugin.h>
#include <projectexplorer/project.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/texteditorsettings.h>
#include <QAction>
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<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);
}
};
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