forked from qt-creator/qt-creator
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:
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<int>(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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user