ClangFormat: Move settings to the Code Style widget

It makes sense to unify the indenter creation by replacing
the CppCodeStylePreferencesFactory instead of removing it.
We are reusing the same options page but with different
kind of settings.

With this change wizards will no more be confused by missing
factory and will create the proper indenter.

Fixes: QTCREATORBUG-21516
Change-Id: I38964d5fa1f2257617c66a1441db723d239a3237
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Ivan Donchevskii
2018-11-20 11:23:30 +01:00
parent 18669c8215
commit e57c1268ee
14 changed files with 72 additions and 99 deletions

View File

@@ -132,10 +132,15 @@ void ClangFormatConfigWidget::initialize()
m_ui->clangFormatOptionsTable->show(); m_ui->clangFormatOptionsTable->show();
m_ui->applyButton->show(); m_ui->applyButton->show();
QLayoutItem *lastItem = m_ui->verticalLayout->itemAt(m_ui->verticalLayout->count() - 1);
if (QSpacerItem *spacer = lastItem->spacerItem())
m_ui->verticalLayout->removeItem(lastItem);
if (m_project && !m_project->projectDirectory().appendPath(SETTINGS_FILE_NAME).exists()) { if (m_project && !m_project->projectDirectory().appendPath(SETTINGS_FILE_NAME).exists()) {
m_ui->projectHasClangFormat->setText(tr("No .clang-format file for the project.")); m_ui->projectHasClangFormat->setText(tr("No .clang-format file for the project."));
m_ui->clangFormatOptionsTable->hide(); m_ui->clangFormatOptionsTable->hide();
m_ui->applyButton->hide(); m_ui->applyButton->hide();
m_ui->verticalLayout->addStretch(1);
connect(m_ui->createFileButton, &QPushButton::clicked, connect(m_ui->createFileButton, &QPushButton::clicked,
this, [this]() { this, [this]() {
@@ -158,7 +163,7 @@ void ClangFormatConfigWidget::initialize()
} else { } else {
m_ui->projectHasClangFormat->setText( m_ui->projectHasClangFormat->setText(
tr("Current project has its own .clang-format file " tr("Current project has its own .clang-format file "
"and can be configured in Projects > Clang Format.")); "and can be configured in Projects > Code Style > C++."));
} }
createStyleFileIfNeeded(true); createStyleFileIfNeeded(true);
m_ui->applyButton->hide(); m_ui->applyButton->hide();

View File

@@ -36,13 +36,14 @@
#include <coreplugin/actionmanager/command.h> #include <coreplugin/actionmanager/command.h>
#include <coreplugin/actionmanager/actioncontainer.h> #include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/coreconstants.h> #include <coreplugin/coreconstants.h>
#include <coreplugin/dialogs/ioptionspage.h>
#include <cpptools/cppcodestylepreferencesfactory.h>
#include <cpptools/cpptoolsconstants.h> #include <cpptools/cpptoolsconstants.h>
#include <cpptools/cppmodelmanager.h> #include <cpptools/cppmodelmanager.h>
#include <projectexplorer/kitinformation.h> #include <projectexplorer/kitinformation.h>
#include <projectexplorer/projectpanelfactory.h> #include <projectexplorer/project.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h> #include <projectexplorer/target.h>
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
@@ -63,59 +64,38 @@ using namespace ProjectExplorer;
namespace ClangFormat { namespace ClangFormat {
class ClangFormatOptionsPage : public Core::IOptionsPage
{
public:
explicit ClangFormatOptionsPage()
{
setId("Cpp.CodeStyle.ClangFormat");
setDisplayName(QCoreApplication::translate(
"ClangFormat::Internal::ClangFormatOptionsPage",
"Clang Format"));
setCategory(CppTools::Constants::CPP_SETTINGS_CATEGORY);
}
QWidget *widget()
{
if (!m_widget)
m_widget = new ClangFormatConfigWidget;
return m_widget;
}
void apply()
{
m_widget->apply();
}
void finish()
{
delete m_widget;
}
private:
QPointer<ClangFormatConfigWidget> m_widget;
};
ClangFormatPlugin::ClangFormatPlugin() = default; ClangFormatPlugin::ClangFormatPlugin() = default;
ClangFormatPlugin::~ClangFormatPlugin() = default; ClangFormatPlugin::~ClangFormatPlugin() = default;
#ifdef KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED #ifdef KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED
static void disableCppCodeStyle() class ClangFormatStyleFactory : public CppTools::CppCodeStylePreferencesFactory
{
public:
QWidget *createCodeStyleEditor(TextEditor::ICodeStylePreferences *,
QWidget *parent = nullptr) override
{
if (!parent)
return new ClangFormatConfigWidget;
return new ClangFormatConfigWidget(SessionManager::startupProject());
}
QWidget *createEditor(TextEditor::ICodeStylePreferences *, QWidget *) const override
{
return nullptr;
}
TextEditor::Indenter *createIndenter() const override
{
return new ClangFormatIndenter();
}
};
static void replaceCppCodeStyle()
{ {
using namespace TextEditor; using namespace TextEditor;
TextEditorSettings::unregisterCodeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID); TextEditorSettings::unregisterCodeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID);
TextEditorSettings::unregisterCodeStylePool(CppTools::Constants::CPP_SETTINGS_ID); ICodeStylePreferencesFactory *factory = new ClangFormatStyleFactory();
TextEditorSettings::unregisterCodeStyle(CppTools::Constants::CPP_SETTINGS_ID); TextEditorSettings::registerCodeStyleFactory(factory);
QList<Core::IOptionsPage *> pages = Core::IOptionsPage::allOptionsPages();
int codeStylePageIndex = Utils::indexOf(pages, [](Core::IOptionsPage *page) {
return page->id() == CppTools::Constants::CPP_CODE_STYLE_SETTINGS_ID;
});
if (codeStylePageIndex >= 0) {
auto *page = pages[codeStylePageIndex];
page->finish();
page->deleteLater();
}
} }
#endif #endif
@@ -124,21 +104,7 @@ bool ClangFormatPlugin::initialize(const QStringList &arguments, QString *errorS
Q_UNUSED(arguments); Q_UNUSED(arguments);
Q_UNUSED(errorString); Q_UNUSED(errorString);
#ifdef KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED #ifdef KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED
m_optionsPage = std::make_unique<ClangFormatOptionsPage>(); replaceCppCodeStyle();
auto panelFactory = new ProjectPanelFactory();
panelFactory->setPriority(120);
panelFactory->setDisplayName(tr("Clang Format"));
panelFactory->setCreateWidgetFunction([](Project *project) {
return new ClangFormatConfigWidget(project);
});
ProjectPanelFactory::registerFactory(panelFactory);
CppTools::CppModelManager::instance()->setCppIndenterCreator([]() {
return new ClangFormatIndenter();
});
disableCppCodeStyle();
#endif #endif
return true; return true;
} }

View File

@@ -27,12 +27,8 @@
#include <extensionsystem/iplugin.h> #include <extensionsystem/iplugin.h>
#include <memory>
namespace ClangFormat { namespace ClangFormat {
class ClangFormatOptionsPage;
class ClangFormatPlugin : public ExtensionSystem::IPlugin class ClangFormatPlugin : public ExtensionSystem::IPlugin
{ {
Q_OBJECT Q_OBJECT
@@ -45,8 +41,6 @@ public:
private: private:
bool initialize(const QStringList &arguments, QString *errorString) final; bool initialize(const QStringList &arguments, QString *errorString) final;
void extensionsInitialized() final {} void extensionsInitialized() final {}
std::unique_ptr<ClangFormatOptionsPage> m_optionsPage;
}; };
} // namespace ClangTools } // namespace ClangTools

View File

@@ -43,6 +43,9 @@
#include <projectexplorer/session.h> #include <projectexplorer/session.h>
#include <texteditor/icodestylepreferencesfactory.h>
#include <texteditor/texteditorsettings.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <utils/mimetypes/mimedatabase.h> #include <utils/mimetypes/mimedatabase.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
@@ -59,6 +62,8 @@ CppTools::CppModelManager *mm()
} // anonymous namespace } // anonymous namespace
using namespace TextEditor;
namespace CppEditor { namespace CppEditor {
namespace Internal { namespace Internal {
@@ -103,7 +108,10 @@ CppEditorDocument::CppEditorDocument()
{ {
setId(CppEditor::Constants::CPPEDITOR_ID); setId(CppEditor::Constants::CPPEDITOR_ID);
setSyntaxHighlighter(new CppHighlighter); setSyntaxHighlighter(new CppHighlighter);
setIndenter(CppTools::CppModelManager::instance()->createCppIndenter());
ICodeStylePreferencesFactory *factory
= TextEditorSettings::codeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID);
setIndenter(factory->createIndenter());
connect(this, &TextEditor::TextDocument::tabSettingsChanged, connect(this, &TextEditor::TextDocument::tabSettingsChanged,
this, &CppEditorDocument::invalidateFormatterCache); this, &CppEditorDocument::invalidateFormatterCache);

View File

@@ -25,11 +25,13 @@
#pragma once #pragma once
#include "cpptools_global.h"
#include <texteditor/icodestylepreferencesfactory.h> #include <texteditor/icodestylepreferencesfactory.h>
namespace CppTools { namespace CppTools {
class CppCodeStylePreferencesFactory : public TextEditor::ICodeStylePreferencesFactory class CPPTOOLS_EXPORT CppCodeStylePreferencesFactory : public TextEditor::ICodeStylePreferencesFactory
{ {
public: public:
CppCodeStylePreferencesFactory(); CppCodeStylePreferencesFactory();

View File

@@ -35,6 +35,7 @@
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <cppeditor/cppeditorconstants.h> #include <cppeditor/cppeditorconstants.h>
#include <texteditor/codestyleeditor.h> #include <texteditor/codestyleeditor.h>
#include <texteditor/icodestylepreferencesfactory.h>
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <texteditor/displaysettings.h> #include <texteditor/displaysettings.h>
#include <texteditor/snippets/snippetprovider.h> #include <texteditor/snippets/snippetprovider.h>
@@ -523,16 +524,19 @@ CppCodeStyleSettingsPage::CppCodeStyleSettingsPage(QWidget *parent) :
QWidget *CppCodeStyleSettingsPage::widget() QWidget *CppCodeStyleSettingsPage::widget()
{ {
if (!m_widget) { if (!m_widget) {
CppCodeStylePreferences *originalCodeStylePreferences CppCodeStylePreferences *originalCodeStylePreferences = CppToolsSettings::instance()
= CppToolsSettings::instance()->cppCodeStyle(); ->cppCodeStyle();
m_pageCppCodeStylePreferences = new CppCodeStylePreferences(m_widget); m_pageCppCodeStylePreferences = new CppCodeStylePreferences();
m_pageCppCodeStylePreferences->setDelegatingPool(originalCodeStylePreferences->delegatingPool()); m_pageCppCodeStylePreferences->setDelegatingPool(
m_pageCppCodeStylePreferences->setCodeStyleSettings(originalCodeStylePreferences->codeStyleSettings()); originalCodeStylePreferences->delegatingPool());
m_pageCppCodeStylePreferences->setCurrentDelegate(originalCodeStylePreferences->currentDelegate()); m_pageCppCodeStylePreferences->setCodeStyleSettings(
originalCodeStylePreferences->codeStyleSettings());
m_pageCppCodeStylePreferences->setCurrentDelegate(
originalCodeStylePreferences->currentDelegate());
// we set id so that it won't be possible to set delegate to the original prefs // we set id so that it won't be possible to set delegate to the original prefs
m_pageCppCodeStylePreferences->setId(originalCodeStylePreferences->id()); m_pageCppCodeStylePreferences->setId(originalCodeStylePreferences->id());
m_widget = new CodeStyleEditor(TextEditorSettings::codeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID), m_widget = TextEditorSettings::codeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID)
m_pageCppCodeStylePreferences); ->createCodeStyleEditor(m_pageCppCodeStylePreferences);
} }
return m_widget; return m_widget;
} }

View File

@@ -91,7 +91,7 @@ public:
private: private:
CppCodeStylePreferences *m_pageCppCodeStylePreferences; CppCodeStylePreferences *m_pageCppCodeStylePreferences;
QPointer<TextEditor::CodeStyleEditor> m_widget; QPointer<QWidget> m_widget;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -39,7 +39,6 @@
#include "cpplocatordata.h" #include "cpplocatordata.h"
#include "cpplocatorfilter.h" #include "cpplocatorfilter.h"
#include "cppbuiltinmodelmanagersupport.h" #include "cppbuiltinmodelmanagersupport.h"
#include "cppqtstyleindenter.h"
#include "cpprefactoringchanges.h" #include "cpprefactoringchanges.h"
#include "cpprefactoringengine.h" #include "cpprefactoringengine.h"
#include "cppsourceprocessor.h" #include "cppsourceprocessor.h"
@@ -510,7 +509,6 @@ void CppModelManager::initializeBuiltinModelManagerSupport()
CppModelManager::CppModelManager() CppModelManager::CppModelManager()
: CppModelManagerBase(nullptr) : CppModelManagerBase(nullptr)
, createCppIndenter([]() { return new CppQtStyleIndenter; })
, d(new CppModelManagerPrivate) , d(new CppModelManagerPrivate)
{ {
d->m_indexingSupporter = 0; d->m_indexingSupporter = 0;

View File

@@ -215,13 +215,6 @@ public:
RefactoringEngineInterface *refactoringEngine); RefactoringEngineInterface *refactoringEngine);
static void removeRefactoringEngine(RefactoringEngineType type); static void removeRefactoringEngine(RefactoringEngineType type);
using CppIndenterCreator = std::function<TextEditor::Indenter *()>;
void setCppIndenterCreator(CppIndenterCreator indenterCreator)
{
createCppIndenter = std::move(indenterCreator);
}
CppIndenterCreator createCppIndenter;
void setLocatorFilter(std::unique_ptr<Core::ILocatorFilter> &&filter); void setLocatorFilter(std::unique_ptr<Core::ILocatorFilter> &&filter);
void setClassesFilter(std::unique_ptr<Core::ILocatorFilter> &&filter); void setClassesFilter(std::unique_ptr<Core::ILocatorFilter> &&filter);
void setIncludesFilter(std::unique_ptr<Core::ILocatorFilter> &&filter); void setIncludesFilter(std::unique_ptr<Core::ILocatorFilter> &&filter);

View File

@@ -49,8 +49,9 @@ CodeStyleSettingsWidget::CodeStyleSettingsWidget(Project *project) : QWidget(),
Core::Id languageId = factory->languageId(); Core::Id languageId = factory->languageId();
ICodeStylePreferences *codeStylePreferences = config->codeStyle(languageId); ICodeStylePreferences *codeStylePreferences = config->codeStyle(languageId);
auto preview = new CodeStyleEditor(factory, codeStylePreferences, m_ui.stackedWidget); auto preview = factory->createCodeStyleEditor(codeStylePreferences, m_ui.stackedWidget);
preview->clearMargins(); if (preview && preview->layout())
preview->layout()->setContentsMargins(QMargins());
m_ui.stackedWidget->addWidget(preview); m_ui.stackedWidget->addWidget(preview);
m_ui.languageComboBox->addItem(factory->displayName()); m_ui.languageComboBox->addItem(factory->displayName());
} }

View File

@@ -79,11 +79,6 @@ CodeStyleEditor::CodeStyleEditor(ICodeStylePreferencesFactory *factory,
updatePreview(); updatePreview();
} }
void CodeStyleEditor::clearMargins()
{
m_layout->setContentsMargins(QMargins());
}
void CodeStyleEditor::updatePreview() void CodeStyleEditor::updatePreview()
{ {
QTextDocument *doc = m_preview->document(); QTextDocument *doc = m_preview->document();

View File

@@ -46,8 +46,6 @@ public:
CodeStyleEditor(ICodeStylePreferencesFactory *factory, CodeStyleEditor(ICodeStylePreferencesFactory *factory,
ICodeStylePreferences *codeStyle, QWidget *parent = nullptr); ICodeStylePreferences *codeStyle, QWidget *parent = nullptr);
void clearMargins();
private: private:
void updatePreview(); void updatePreview();

View File

@@ -25,6 +25,8 @@
#include "icodestylepreferencesfactory.h" #include "icodestylepreferencesfactory.h"
#include "codestyleeditor.h"
using namespace TextEditor; using namespace TextEditor;
ICodeStylePreferencesFactory::ICodeStylePreferencesFactory(QObject *parent) : ICodeStylePreferencesFactory::ICodeStylePreferencesFactory(QObject *parent) :
@@ -32,3 +34,8 @@ ICodeStylePreferencesFactory::ICodeStylePreferencesFactory(QObject *parent) :
{ {
} }
QWidget *ICodeStylePreferencesFactory::createCodeStyleEditor(ICodeStylePreferences *codeStyle,
QWidget *parent)
{
return new CodeStyleEditor(this, codeStyle, parent);
}

View File

@@ -42,6 +42,8 @@ class TEXTEDITOR_EXPORT ICodeStylePreferencesFactory : public QObject
public: public:
explicit ICodeStylePreferencesFactory(QObject *parent = nullptr); explicit ICodeStylePreferencesFactory(QObject *parent = nullptr);
virtual QWidget *createCodeStyleEditor(ICodeStylePreferences *codeStyle,
QWidget *parent = nullptr);
virtual Core::Id languageId() = 0; virtual Core::Id languageId() = 0;
virtual QString displayName() = 0; virtual QString displayName() = 0;
virtual ICodeStylePreferences *createCodeStyle() const = 0; virtual ICodeStylePreferences *createCodeStyle() const = 0;