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_CODE_ON_SAVE_ID[] = "ClangFormat.FormatCodeOnSave";
|
||||||
static const char FORMAT_WHILE_TYPING_ID[] = "ClangFormat.FormatWhileTyping";
|
static const char FORMAT_WHILE_TYPING_ID[] = "ClangFormat.FormatWhileTyping";
|
||||||
static const char MODE_ID[] = "ClangFormat.Mode";
|
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 USE_GLOBAL_SETTINGS[] = "ClangFormat.UseGlobalSettings";
|
||||||
static const char OPEN_CURRENT_CONFIG_ID[] = "ClangFormat.OpenCurrentConfig";
|
static const char OPEN_CURRENT_CONFIG_ID[] = "ClangFormat.OpenCurrentConfig";
|
||||||
} // namespace Constants
|
} // namespace Constants
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include <QCheckBox>
|
#include <QCheckBox>
|
||||||
#include <QComboBox>
|
#include <QComboBox>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
#include <QSpinBox>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@@ -31,8 +32,14 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(
|
|||||||
, m_project(project)
|
, m_project(project)
|
||||||
, m_codeStyle(codeStyle)
|
, 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_projectHasClangFormat = new QLabel(this);
|
||||||
m_formattingModeLabel = new QLabel(Tr::tr("Formatting mode:"));
|
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_indentingOrFormatting = new QComboBox(this);
|
||||||
m_formatWhileTyping = new QCheckBox(Tr::tr("Format while typing"));
|
m_formatWhileTyping = new QCheckBox(Tr::tr("Format while typing"));
|
||||||
m_formatOnSave = new QCheckBox(Tr::tr("Format edited code on file save"));
|
m_formatOnSave = new QCheckBox(Tr::tr("Format edited code on file save"));
|
||||||
@@ -50,7 +57,10 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(
|
|||||||
title(Tr::tr("ClangFormat settings:")),
|
title(Tr::tr("ClangFormat settings:")),
|
||||||
Column {
|
Column {
|
||||||
m_useGlobalSettings,
|
m_useGlobalSettings,
|
||||||
Row { m_formattingModeLabel, m_indentingOrFormatting, st },
|
Form {
|
||||||
|
m_formattingModeLabel, m_indentingOrFormatting, st, br,
|
||||||
|
m_fileSizeThresholdLabel, m_fileSizeThresholdSpinBox, st, br
|
||||||
|
},
|
||||||
m_formatWhileTyping,
|
m_formatWhileTyping,
|
||||||
m_formatOnSave,
|
m_formatOnSave,
|
||||||
m_projectHasClangFormat,
|
m_projectHasClangFormat,
|
||||||
@@ -67,6 +77,7 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(
|
|||||||
initIndentationOrFormattingCombobox();
|
initIndentationOrFormattingCombobox();
|
||||||
initOverrideCheckBox();
|
initOverrideCheckBox();
|
||||||
initUseGlobalSettingsCheckBox();
|
initUseGlobalSettingsCheckBox();
|
||||||
|
initFileSizeThresholdSpinBox();
|
||||||
|
|
||||||
if (project) {
|
if (project) {
|
||||||
m_formattingModeLabel->hide();
|
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()
|
bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists()
|
||||||
{
|
{
|
||||||
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder
|
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder
|
||||||
@@ -215,6 +243,7 @@ void ClangFormatGlobalConfigWidget::apply()
|
|||||||
settings.setMode(
|
settings.setMode(
|
||||||
static_cast<ClangFormatSettings::Mode>(m_indentingOrFormatting->currentIndex()));
|
static_cast<ClangFormatSettings::Mode>(m_indentingOrFormatting->currentIndex()));
|
||||||
settings.setOverrideDefaultFile(m_overrideDefault->isChecked());
|
settings.setOverrideDefaultFile(m_overrideDefault->isChecked());
|
||||||
|
settings.setFileSizeThreshold(m_fileSizeThresholdSpinBox->value());
|
||||||
m_overrideDefaultFile = m_overrideDefault->isChecked();
|
m_overrideDefaultFile = m_overrideDefault->isChecked();
|
||||||
}
|
}
|
||||||
settings.write();
|
settings.write();
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ QT_BEGIN_NAMESPACE
|
|||||||
class QCheckBox;
|
class QCheckBox;
|
||||||
class QComboBox;
|
class QComboBox;
|
||||||
class QLabel;
|
class QLabel;
|
||||||
|
class QSpinBox;
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
namespace ProjectExplorer { class Project; }
|
namespace ProjectExplorer { class Project; }
|
||||||
@@ -37,6 +38,7 @@ private:
|
|||||||
void initIndentationOrFormattingCombobox();
|
void initIndentationOrFormattingCombobox();
|
||||||
void initOverrideCheckBox();
|
void initOverrideCheckBox();
|
||||||
void initUseGlobalSettingsCheckBox();
|
void initUseGlobalSettingsCheckBox();
|
||||||
|
void initFileSizeThresholdSpinBox();
|
||||||
|
|
||||||
bool projectClangFormatFileExists();
|
bool projectClangFormatFileExists();
|
||||||
|
|
||||||
@@ -47,6 +49,8 @@ private:
|
|||||||
|
|
||||||
QLabel *m_projectHasClangFormat;
|
QLabel *m_projectHasClangFormat;
|
||||||
QLabel *m_formattingModeLabel;
|
QLabel *m_formattingModeLabel;
|
||||||
|
QLabel *m_fileSizeThresholdLabel;
|
||||||
|
QSpinBox *m_fileSizeThresholdSpinBox;
|
||||||
QComboBox *m_indentingOrFormatting;
|
QComboBox *m_indentingOrFormatting;
|
||||||
QCheckBox *m_formatWhileTyping;
|
QCheckBox *m_formatWhileTyping;
|
||||||
QCheckBox *m_formatOnSave;
|
QCheckBox *m_formatOnSave;
|
||||||
|
|||||||
@@ -130,7 +130,8 @@ TextEditor::Indenter *ClangFormatForwardingIndenter::currentIndenter() const
|
|||||||
{
|
{
|
||||||
ClangFormatSettings::Mode mode = getCurrentIndentationOrFormattingSettings(m_fileName);
|
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_cppIndenter.get();
|
||||||
|
|
||||||
return m_clangFormatIndenter.get();
|
return m_clangFormatIndenter.get();
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ ClangFormatSettings::ClangFormatSettings()
|
|||||||
.toBool();
|
.toBool();
|
||||||
m_formatOnSave = settings->value(QLatin1String(Constants::FORMAT_CODE_ON_SAVE_ID), false)
|
m_formatOnSave = settings->value(QLatin1String(Constants::FORMAT_CODE_ON_SAVE_ID), false)
|
||||||
.toBool();
|
.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
|
// Convert old settings to new ones. New settings were added to QtC 8.0
|
||||||
bool isOldFormattingOn
|
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_WHILE_TYPING_ID), m_formatWhileTyping);
|
||||||
settings->setValue(QLatin1String(Constants::FORMAT_CODE_ON_SAVE_ID), m_formatOnSave);
|
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::MODE_ID), static_cast<int>(m_mode));
|
||||||
|
settings->setValue(QLatin1String(Constants::FILE_SIZE_THREDSHOLD), m_fileSizeThreshold);
|
||||||
settings->endGroup();
|
settings->endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,4 +97,14 @@ ClangFormatSettings::Mode ClangFormatSettings::mode() const
|
|||||||
return m_mode;
|
return m_mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ClangFormatSettings::setFileSizeThreshold(int fileSizeInKb)
|
||||||
|
{
|
||||||
|
m_fileSizeThreshold = fileSizeInKb;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ClangFormatSettings::fileSizeThreshold() const
|
||||||
|
{
|
||||||
|
return m_fileSizeThreshold;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace ClangFormat
|
} // namespace ClangFormat
|
||||||
|
|||||||
@@ -33,11 +33,15 @@ public:
|
|||||||
void setFormatOnSave(bool enable);
|
void setFormatOnSave(bool enable);
|
||||||
bool formatOnSave() const;
|
bool formatOnSave() const;
|
||||||
|
|
||||||
|
void setFileSizeThreshold(int fileSizeInKb);
|
||||||
|
int fileSizeThreshold() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mode m_mode;
|
Mode m_mode;
|
||||||
bool m_overrideDefaultFile = false;
|
bool m_overrideDefaultFile = false;
|
||||||
bool m_formatWhileTyping = false;
|
bool m_formatWhileTyping = false;
|
||||||
bool m_formatOnSave = false;
|
bool m_formatOnSave = false;
|
||||||
|
int m_fileSizeThreshold = 1024;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ClangFormat
|
} // namespace ClangFormat
|
||||||
|
|||||||
Reference in New Issue
Block a user