diff --git a/src/plugins/clangformat/clangformatconstants.h b/src/plugins/clangformat/clangformatconstants.h index 5bd8b1246c3..572effee21e 100644 --- a/src/plugins/clangformat/clangformatconstants.h +++ b/src/plugins/clangformat/clangformatconstants.h @@ -13,6 +13,7 @@ static const char OVERRIDE_FILE_ID[] = "ClangFormat.OverrideFile"; static const char FORMAT_CODE_ON_SAVE_ID[] = "ClangFormat.FormatCodeOnSave"; static const char FORMAT_WHILE_TYPING_ID[] = "ClangFormat.FormatWhileTyping"; static const char MODE_ID[] = "ClangFormat.Mode"; +static const char FILE_SIZE_THREDSHOLD[] = "ClangFormat.FileSizeThreshold"; static const char USE_GLOBAL_SETTINGS[] = "ClangFormat.UseGlobalSettings"; static const char OPEN_CURRENT_CONFIG_ID[] = "ClangFormat.OpenCurrentConfig"; } // namespace Constants diff --git a/src/plugins/clangformat/clangformatglobalconfigwidget.cpp b/src/plugins/clangformat/clangformatglobalconfigwidget.cpp index af8a0846b15..d8533f268c7 100644 --- a/src/plugins/clangformat/clangformatglobalconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatglobalconfigwidget.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -31,8 +32,14 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget( , m_project(project) , m_codeStyle(codeStyle) { + const QString sizeThresholdToolTip = Tr::tr( + "Files greater than this will not be indented by ClangFormat.\n" + "The built-in code indenter will handle indentation."); + m_projectHasClangFormat = new QLabel(this); m_formattingModeLabel = new QLabel(Tr::tr("Formatting mode:")); + m_fileSizeThresholdLabel = new QLabel(Tr::tr("Ignore files greater than:")); + m_fileSizeThresholdSpinBox = new QSpinBox(this); m_indentingOrFormatting = new QComboBox(this); m_formatWhileTyping = new QCheckBox(Tr::tr("Format while typing")); m_formatOnSave = new QCheckBox(Tr::tr("Format edited code on file save")); @@ -50,7 +57,10 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget( title(Tr::tr("ClangFormat settings:")), Column { m_useGlobalSettings, - Row { m_formattingModeLabel, m_indentingOrFormatting, st }, + Form { + m_formattingModeLabel, m_indentingOrFormatting, st, br, + m_fileSizeThresholdLabel, m_fileSizeThresholdSpinBox, st, br + }, m_formatWhileTyping, m_formatOnSave, m_projectHasClangFormat, @@ -67,6 +77,7 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget( initIndentationOrFormattingCombobox(); initOverrideCheckBox(); initUseGlobalSettingsCheckBox(); + initFileSizeThresholdSpinBox(); if (project) { m_formattingModeLabel->hide(); @@ -139,6 +150,23 @@ void ClangFormatGlobalConfigWidget::initUseGlobalSettingsCheckBox() }); } +void ClangFormatGlobalConfigWidget::initFileSizeThresholdSpinBox() +{ + m_fileSizeThresholdSpinBox->setMinimum(1); + m_fileSizeThresholdSpinBox->setMaximum(std::numeric_limits::max()); + m_fileSizeThresholdSpinBox->setSuffix(" KB"); + m_fileSizeThresholdSpinBox->setValue(ClangFormatSettings::instance().fileSizeThreshold()); + if (m_project) + m_fileSizeThresholdSpinBox->hide(); + + connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged, this, [this](int index) { + m_fileSizeThresholdLabel->setEnabled( + index != static_cast(ClangFormatSettings::Mode::Disable)); + m_fileSizeThresholdSpinBox->setEnabled( + index != static_cast(ClangFormatSettings::Mode::Disable)); + }); +} + bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists() { llvm::Expected styleFromProjectFolder @@ -215,6 +243,7 @@ void ClangFormatGlobalConfigWidget::apply() settings.setMode( static_cast(m_indentingOrFormatting->currentIndex())); settings.setOverrideDefaultFile(m_overrideDefault->isChecked()); + settings.setFileSizeThreshold(m_fileSizeThresholdSpinBox->value()); m_overrideDefaultFile = m_overrideDefault->isChecked(); } settings.write(); diff --git a/src/plugins/clangformat/clangformatglobalconfigwidget.h b/src/plugins/clangformat/clangformatglobalconfigwidget.h index 7b2d6fe7c9c..e4a7be50478 100644 --- a/src/plugins/clangformat/clangformatglobalconfigwidget.h +++ b/src/plugins/clangformat/clangformatglobalconfigwidget.h @@ -13,6 +13,7 @@ QT_BEGIN_NAMESPACE class QCheckBox; class QComboBox; class QLabel; +class QSpinBox; QT_END_NAMESPACE namespace ProjectExplorer { class Project; } @@ -37,6 +38,7 @@ private: void initIndentationOrFormattingCombobox(); void initOverrideCheckBox(); void initUseGlobalSettingsCheckBox(); + void initFileSizeThresholdSpinBox(); bool projectClangFormatFileExists(); @@ -47,6 +49,8 @@ private: QLabel *m_projectHasClangFormat; QLabel *m_formattingModeLabel; + QLabel *m_fileSizeThresholdLabel; + QSpinBox *m_fileSizeThresholdSpinBox; QComboBox *m_indentingOrFormatting; QCheckBox *m_formatWhileTyping; QCheckBox *m_formatOnSave; diff --git a/src/plugins/clangformat/clangformatindenter.cpp b/src/plugins/clangformat/clangformatindenter.cpp index 67d5171b010..35f0c8b1ede 100644 --- a/src/plugins/clangformat/clangformatindenter.cpp +++ b/src/plugins/clangformat/clangformatindenter.cpp @@ -130,7 +130,8 @@ TextEditor::Indenter *ClangFormatForwardingIndenter::currentIndenter() const { ClangFormatSettings::Mode mode = getCurrentIndentationOrFormattingSettings(m_fileName); - if (mode == ClangFormatSettings::Disable) + if (mode == ClangFormatSettings::Disable + || m_fileName.fileSize() >= ClangFormatSettings::instance().fileSizeThreshold() * 1024) return m_cppIndenter.get(); return m_clangFormatIndenter.get(); diff --git a/src/plugins/clangformat/clangformatsettings.cpp b/src/plugins/clangformat/clangformatsettings.cpp index 4f4ea8ac232..1c76d14df6b 100644 --- a/src/plugins/clangformat/clangformatsettings.cpp +++ b/src/plugins/clangformat/clangformatsettings.cpp @@ -25,6 +25,8 @@ ClangFormatSettings::ClangFormatSettings() .toBool(); m_formatOnSave = settings->value(QLatin1String(Constants::FORMAT_CODE_ON_SAVE_ID), false) .toBool(); + m_fileSizeThreshold + = settings->value(QLatin1String(Constants::FILE_SIZE_THREDSHOLD), 1024).toInt(); // Convert old settings to new ones. New settings were added to QtC 8.0 bool isOldFormattingOn @@ -51,6 +53,7 @@ void ClangFormatSettings::write() const settings->setValue(QLatin1String(Constants::FORMAT_WHILE_TYPING_ID), m_formatWhileTyping); settings->setValue(QLatin1String(Constants::FORMAT_CODE_ON_SAVE_ID), m_formatOnSave); settings->setValue(QLatin1String(Constants::MODE_ID), static_cast(m_mode)); + settings->setValue(QLatin1String(Constants::FILE_SIZE_THREDSHOLD), m_fileSizeThreshold); settings->endGroup(); } @@ -94,4 +97,14 @@ ClangFormatSettings::Mode ClangFormatSettings::mode() const return m_mode; } +void ClangFormatSettings::setFileSizeThreshold(int fileSizeInKb) +{ + m_fileSizeThreshold = fileSizeInKb; +} + +int ClangFormatSettings::fileSizeThreshold() const +{ + return m_fileSizeThreshold; +} + } // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatsettings.h b/src/plugins/clangformat/clangformatsettings.h index c2ce53f4c76..07e854df461 100644 --- a/src/plugins/clangformat/clangformatsettings.h +++ b/src/plugins/clangformat/clangformatsettings.h @@ -33,11 +33,15 @@ public: void setFormatOnSave(bool enable); bool formatOnSave() const; + void setFileSizeThreshold(int fileSizeInKb); + int fileSizeThreshold() const; + private: Mode m_mode; bool m_overrideDefaultFile = false; bool m_formatWhileTyping = false; bool m_formatOnSave = false; + int m_fileSizeThreshold = 1024; }; } // namespace ClangFormat