ClangFormat: Add file size threshold

Add file size threshold to prevent qtcreator freeze
when a file is big. The default value is 1MB.

Change-Id: I356c64cd5ca99a34413043896076dbab859538b6
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Artem Sokolovskii
2023-05-11 11:34:08 +02:00
parent 80665d6acf
commit ac24b23ff8
6 changed files with 54 additions and 2 deletions

View File

@@ -16,6 +16,7 @@
#include <QCheckBox>
#include <QComboBox>
#include <QLabel>
#include <QSpinBox>
#include <QWidget>
#include <sstream>
@@ -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<int>::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<int>(ClangFormatSettings::Mode::Disable));
m_fileSizeThresholdSpinBox->setEnabled(
index != static_cast<int>(ClangFormatSettings::Mode::Disable));
});
}
bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists()
{
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder
@@ -215,6 +243,7 @@ void ClangFormatGlobalConfigWidget::apply()
settings.setMode(
static_cast<ClangFormatSettings::Mode>(m_indentingOrFormatting->currentIndex()));
settings.setOverrideDefaultFile(m_overrideDefault->isChecked());
settings.setFileSizeThreshold(m_fileSizeThresholdSpinBox->value());
m_overrideDefaultFile = m_overrideDefault->isChecked();
}
settings.write();