ClangFormat: Separate ClangFormat and Standard Indenter UI

The combined UI was a bit misleading for the user and cause
bunch of synchronization problems.

The synchronization between old CodeStyle settings and ClangFormat
settings was removed. Corresponding classes and functions
were also cleared.

The behavior now:
When "Indenting only" or "Full Formatting" modes are chosen then
The ClangFormat settings page is visible and the ClangFormat indenter
is used.
For "Disable" mode standard CodeStyle pages are shown and standard
indenter respectively.

Change-Id: Idb4974c68ceb16ef2e55b108043cc6f56f859840
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2024-01-11 11:31:00 +01:00
parent 2ff533b499
commit bb88c8c9be
13 changed files with 45 additions and 184 deletions

View File

@@ -49,7 +49,7 @@ using namespace Utils;
namespace ClangFormat {
class ClangFormatConfigWidget final : public CppEditor::CppCodeStyleWidget
class ClangFormatConfigWidget final : public TextEditor::CodeStyleEditorWidget
{
public:
ClangFormatConfigWidget(TextEditor::ICodeStylePreferences *codeStyle,
@@ -57,9 +57,6 @@ public:
QWidget *parent);
void apply() final;
void finish() final;
void setCodeStyleSettings(const CppEditor::CppCodeStyleSettings &settings) final;
void setTabSettings(const TextEditor::TabSettings &settings) final;
void synchronize() final;
private:
bool eventFilter(QObject *object, QEvent *event) final;
@@ -103,7 +100,7 @@ bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event)
ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferences *codeStyle,
Project *project,
QWidget *parent)
: CppCodeStyleWidget(parent)
: CodeStyleEditorWidget(parent)
{
m_project = project;
m_config = std::make_unique<ClangFormatFile>(codeStyle->currentPreferences());
@@ -115,7 +112,7 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
m_checksScrollArea->setWidget(m_checksWidget);
m_checksScrollArea->setWidgetResizable(true);
m_checksWidget->setEnabled(!codeStyle->isReadOnly() && !codeStyle->isTemporarilyReadOnly()
&& !codeStyle->isAdditionalTabDisabled());
&& codeStyle->isAdditionalTabVisible());
static const int expectedMajorVersion = 17;
@@ -189,7 +186,7 @@ void ClangFormatConfigWidget::slotCodeStyleChanged(
m_style = m_config->style();
m_checksWidget->setEnabled(!codeStyle->isReadOnly() && !codeStyle->isTemporarilyReadOnly()
&& !codeStyle->isAdditionalTabDisabled());
&& codeStyle->isAdditionalTabVisible());
fillTable();
updatePreview();
@@ -479,29 +476,6 @@ void ClangFormatConfigWidget::saveChanges(QObject *sender)
fillTable();
updatePreview();
synchronize();
}
void ClangFormatConfigWidget::setCodeStyleSettings(const CppEditor::CppCodeStyleSettings &settings)
{
m_config->fromCppCodeStyleSettings(settings);
fillTable();
updatePreview();
}
void ClangFormatConfigWidget::setTabSettings(const TextEditor::TabSettings &settings)
{
m_config->fromTabSettings(settings);
fillTable();
updatePreview();
}
void ClangFormatConfigWidget::synchronize()
{
emit codeStyleSettingsChanged(m_config->toCppCodeStyleSettings(m_project));
emit tabSettingsChanged(m_config->toTabSettings(m_project));
}
void ClangFormatConfigWidget::apply()
@@ -520,7 +494,7 @@ void ClangFormatConfigWidget::finish()
m_config->setStyle(m_style);
}
CppEditor::CppCodeStyleWidget *createClangFormatConfigWidget(
TextEditor::CodeStyleEditorWidget *createClangFormatConfigWidget(
TextEditor::ICodeStylePreferences *codeStyle,
Project *project,
QWidget *parent)

View File

@@ -9,13 +9,16 @@ QT_BEGIN_NAMESPACE
class QWidget;
QT_END_NAMESPACE
namespace CppEditor { class CppCodeStyleWidget; }
namespace TextEditor { class ICodeStylePreferences; }
namespace TextEditor {
class ICodeStylePreferences;
class CodeStyleEditorWidget;
} // namespace TextEditor
namespace ProjectExplorer { class Project; }
namespace ClangFormat {
CppEditor::CppCodeStyleWidget *createClangFormatConfigWidget(
TextEditor::CodeStyleEditorWidget *createClangFormatConfigWidget(
TextEditor::ICodeStylePreferences *codeStyle,
ProjectExplorer::Project *project,
QWidget *parent);

View File

@@ -129,93 +129,3 @@ void ClangFormatFile::saveStyleToFile(clang::format::FormatStyle style, Utils::F
styleStr.append("\n");
filePath.writeFileContents(QByteArray::fromStdString(styleStr));
}
CppEditor::CppCodeStyleSettings ClangFormatFile::toCppCodeStyleSettings(
ProjectExplorer::Project *project) const
{
using namespace clang::format;
auto settings = CppEditor::CppCodeStyleSettings::getProjectCodeStyle(project);
FormatStyle style;
bool success = parseConfigurationFile(m_filePath, style);
QTC_ASSERT(success, return settings);
// Modifier offset should be opposite to indent width in order indentAccessSpecifiers
// to be false
settings.indentAccessSpecifiers = (style.AccessModifierOffset != -1 * int(style.IndentWidth));
if (style.NamespaceIndentation == FormatStyle::NamespaceIndentationKind::NI_All) {
settings.indentNamespaceBody = true;
settings.indentNamespaceBraces = settings.indentNamespaceBody;
}
if (style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
settings.indentClassBraces = true;
settings.indentEnumBraces = settings.indentClassBraces;
settings.indentBlockBraces = settings.indentClassBraces;
settings.indentFunctionBraces = settings.indentClassBraces;
}
settings.indentSwitchLabels = style.IndentCaseLabels;
#if LLVM_VERSION_MAJOR >= 11
settings.indentBlocksRelativeToSwitchLabels = style.IndentCaseBlocks;
#endif
if (style.DerivePointerAlignment
&& ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting) {
settings.bindStarToIdentifier = style.PointerAlignment == FormatStyle::PAS_Right;
settings.bindStarToTypeName = style.PointerAlignment == FormatStyle::PAS_Left;
settings.bindStarToLeftSpecifier = style.PointerAlignment == FormatStyle::PAS_Left;
settings.bindStarToRightSpecifier = style.PointerAlignment == FormatStyle::PAS_Right;
}
settings.extraPaddingForConditionsIfConfusingAlign = style.BreakBeforeBinaryOperators
== FormatStyle::BOS_All;
settings.alignAssignments = style.BreakBeforeBinaryOperators == FormatStyle::BOS_All
|| style.BreakBeforeBinaryOperators
== FormatStyle::BOS_NonAssignment;
return settings;
}
void ClangFormatFile::fromCppCodeStyleSettings(const CppEditor::CppCodeStyleSettings &settings)
{
::fromCppCodeStyleSettings(m_style, settings);
saveNewFormat();
}
TextEditor::TabSettings ClangFormatFile::toTabSettings(ProjectExplorer::Project *project) const
{
using namespace clang::format;
auto settings = CppEditor::CppCodeStyleSettings::getProjectTabSettings(project);
FormatStyle style;
bool success = parseConfigurationFile(m_filePath, style);
QTC_ASSERT(success, return settings);
settings.m_indentSize = style.IndentWidth;
settings.m_tabSize = style.TabWidth;
switch (style.UseTab) {
#if LLVM_VERSION_MAJOR >= 11
case FormatStyle::UT_AlignWithSpaces:
#endif
case FormatStyle::UT_ForIndentation:
case FormatStyle::UT_ForContinuationAndIndentation:
settings.m_tabPolicy = TextEditor::TabSettings::TabPolicy::MixedTabPolicy;
break;
case FormatStyle::UT_Never:
settings.m_tabPolicy = TextEditor::TabSettings::TabPolicy::SpacesOnlyTabPolicy;
break;
case FormatStyle::UT_Always:
settings.m_tabPolicy = TextEditor::TabSettings::TabPolicy::TabsOnlyTabPolicy;
break;
}
return settings;
}
void ClangFormatFile::fromTabSettings(const TextEditor::TabSettings &settings)
{
::fromTabSettings(m_style, settings);
saveNewFormat();
}

View File

@@ -32,10 +32,6 @@ public:
using Field = std::pair<QString, QString>;
QString changeFields(QList<Field> fields);
QString changeField(Field field);
CppEditor::CppCodeStyleSettings toCppCodeStyleSettings(ProjectExplorer::Project *project) const;
TextEditor::TabSettings toTabSettings(ProjectExplorer::Project *project) const;
void fromCppCodeStyleSettings(const CppEditor::CppCodeStyleSettings &settings);
void fromTabSettings(const TextEditor::TabSettings &settings);
bool isReadOnly() const;
void setIsReadOnly(bool isReadOnly);

View File

@@ -27,7 +27,7 @@ namespace ClangFormat {
ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(
TextEditor::ICodeStylePreferences *codeStyle, ProjectExplorer::Project *project, QWidget *parent)
: CppCodeStyleWidget(parent)
: TextEditor::CodeStyleEditorWidget(parent)
, m_project(project)
, m_codeStyle(codeStyle)
{
@@ -122,7 +122,7 @@ void ClangFormatGlobalConfigWidget::initIndentationOrFormattingCombobox()
m_indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Formatting),
Tr::tr("Full formatting"));
m_indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Disable),
Tr::tr("Disable"));
Tr::tr("Use built-in indenter"));
m_indentingOrFormatting->setCurrentIndex(
static_cast<int>(getProjectIndentationOrFormattingSettings(m_project)));
@@ -226,8 +226,10 @@ void ClangFormatGlobalConfigWidget::initCustomSettingsCheckBox()
if (m_ignoreChanges.isLocked())
return;
Utils::GuardLocker locker(m_ignoreChanges);
m_codeStyle->currentPreferences()->setTemporarilyReadOnly(!m_useCustomSettingsCheckBox->isChecked());
m_codeStyle->currentPreferences()->setIsAdditionalTabDisabled(!m_useCustomSettingsCheckBox->isEnabled());
m_codeStyle->currentPreferences()->setTemporarilyReadOnly(
!m_useCustomSettingsCheckBox->isChecked());
m_codeStyle->currentPreferences()->setIsAdditionalTabVisible(
m_useCustomSettingsCheckBox->isEnabled());
ClangFormatSettings::instance().write();
emit m_codeStyle->currentPreferencesChanged(m_codeStyle->currentPreferences());
};

View File

@@ -15,11 +15,14 @@ class QSpinBox;
QT_END_NAMESPACE
namespace ProjectExplorer { class Project; }
namespace TextEditor { class ICodeStylePreferences; }
namespace TextEditor {
class ICodeStylePreferences;
class CodeStyleEditorWidget;
} // namespace TextEditor
namespace ClangFormat {
class ClangFormatGlobalConfigWidget : public CppEditor::CppCodeStyleWidget
class ClangFormatGlobalConfigWidget : public TextEditor::CodeStyleEditorWidget
{
Q_OBJECT

View File

@@ -44,7 +44,7 @@ public:
return new ClangFormatForwardingIndenter(doc);
}
std::pair<CppCodeStyleWidget *, QString> additionalTab(
std::pair<TextEditor::CodeStyleEditorWidget *, QString> additionalTab(
ICodeStylePreferences *codeStyle, Project *project, QWidget *parent) const override
{
return {createClangFormatConfigWidget(codeStyle, project, parent), Tr::tr("ClangFormat")};