Clangformat: Brings back some of checkboxes

Brought back checkboxes: format while typing, format on save.
Brought back format while typing feature.
Global checkboxes will be hidden in project settings,
and visible for global.

Change-Id: I193cf9e13b10de22091edb5fe04aef957dd74586
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Artem Sokolovskii
2022-06-28 16:17:02 +02:00
parent 7899fd6fc4
commit 2f18256633
11 changed files with 111 additions and 13 deletions

View File

@@ -594,7 +594,7 @@ Utils::Text::Replacements ClangFormatBaseIndenter::indentsFor(QTextBlock startBl
const QByteArray buffer = m_doc->toPlainText().toUtf8(); const QByteArray buffer = m_doc->toPlainText().toUtf8();
ReplacementsToKeep replacementsToKeep = ReplacementsToKeep::OnlyIndent; ReplacementsToKeep replacementsToKeep = ReplacementsToKeep::OnlyIndent;
if (formatCodeInsteadOfIndent() if (formatWhileTyping()
&& (cursorPositionInEditor == -1 || cursorPositionInEditor >= startBlockPosition) && (cursorPositionInEditor == -1 || cursorPositionInEditor >= startBlockPosition)
&& (typedChar == ';' || typedChar == '}')) { && (typedChar == ';' || typedChar == '}')) {
// Format before current position only in case the cursor is inside the indented block. // Format before current position only in case the cursor is inside the indented block.

View File

@@ -72,6 +72,7 @@ public:
protected: protected:
virtual bool formatCodeInsteadOfIndent() const { return false; } virtual bool formatCodeInsteadOfIndent() const { return false; }
virtual bool formatWhileTyping() const { return false; }
virtual int lastSaveRevision() const { return 0; } virtual int lastSaveRevision() const { return 0; }
private: private:

View File

@@ -85,6 +85,7 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
initChecksAndPreview(!codeStyle->isReadOnly()); initChecksAndPreview(!codeStyle->isReadOnly());
initOverrideCheckBox(); initOverrideCheckBox();
initCheckBoxes();
initIndentationOrFormattingCombobox(); initIndentationOrFormattingCombobox();
showOrHideWidgets(); showOrHideWidgets();
@@ -128,6 +129,45 @@ void ClangFormatConfigWidget::initChecksAndPreview(bool enabled)
m_preview->textDocument()->indenter()->setFileName(fileName); m_preview->textDocument()->indenter()->setFileName(fileName);
} }
void ClangFormatConfigWidget::initCheckBoxes()
{
if (m_project) {
m_ui->formatOnSave->hide();
m_ui->formatWhileTyping->hide();
return;
}
m_ui->formatOnSave->show();
m_ui->formatWhileTyping->show();
auto setEnableCheckBoxes = [this](int index) {
bool isFormatting = index == static_cast<int>(ClangFormatSettings::Mode::Formatting);
m_ui->formatOnSave->setEnabled(isFormatting);
m_ui->formatWhileTyping->setEnabled(isFormatting);
};
setEnableCheckBoxes(m_ui->indentingOrFormatting->currentIndex());
connect(m_ui->indentingOrFormatting, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, setEnableCheckBoxes);
m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave());
m_ui->formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping());
connect(m_ui->formatOnSave, &QCheckBox::toggled,
this, [](bool checked) {
ClangFormatSettings &settings = ClangFormatSettings::instance();
settings.setFormatOnSave(checked);
settings.write();
});
connect(m_ui->formatWhileTyping, &QCheckBox::toggled,
this, [](bool checked) {
ClangFormatSettings &settings = ClangFormatSettings::instance();
settings.setFormatWhileTyping(checked);
settings.write();
});
}
void ClangFormatConfigWidget::initOverrideCheckBox() void ClangFormatConfigWidget::initOverrideCheckBox()
{ {
if (m_project) { if (m_project) {
@@ -190,7 +230,9 @@ void ClangFormatConfigWidget::initIndentationOrFormattingCombobox()
m_ui->indentingOrFormatting->setCurrentIndex( m_ui->indentingOrFormatting->setCurrentIndex(
static_cast<int>(ClangFormatSettings::instance().mode())); static_cast<int>(ClangFormatSettings::instance().mode()));
m_ui->indentingOrFormatting->show(); const bool isGlobal = !m_project;
m_ui->indentingOrFormatting->setVisible(isGlobal);
m_ui->formattingModeLabel->setVisible(isGlobal);
connect(m_ui->indentingOrFormatting, QOverload<int>::of(&QComboBox::currentIndexChanged), connect(m_ui->indentingOrFormatting, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, [](int index) { this, [](int index) {
@@ -219,6 +261,7 @@ void ClangFormatConfigWidget::showOrHideWidgets()
if (!m_ui->overrideDefault->isChecked() && m_project) { if (!m_ui->overrideDefault->isChecked() && m_project) {
// Show the fallback configuration only globally. // Show the fallback configuration only globally.
m_ui->fallbackConfig->hide();
m_checksScrollArea->hide(); m_checksScrollArea->hide();
m_preview->hide(); m_preview->hide();
m_ui->verticalLayout->addStretch(1); m_ui->verticalLayout->addStretch(1);
@@ -226,6 +269,7 @@ void ClangFormatConfigWidget::showOrHideWidgets()
} }
createStyleFileIfNeeded(!m_project); createStyleFileIfNeeded(!m_project);
m_ui->fallbackConfig->show();
m_checksScrollArea->show(); m_checksScrollArea->show();
m_preview->show(); m_preview->show();

View File

@@ -65,6 +65,7 @@ private:
void showOrHideWidgets(); void showOrHideWidgets();
void initChecksAndPreview(bool enabled); void initChecksAndPreview(bool enabled);
void initOverrideCheckBox(); void initOverrideCheckBox();
void initCheckBoxes();
void connectChecks(); void connectChecks();
void fillTable(); void fillTable();

View File

@@ -27,7 +27,7 @@
<number>8</number> <number>8</number>
</property> </property>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_3"> <layout class="QHBoxLayout" name="formattingModeLayout">
<item> <item>
<widget class="QLabel" name="formattingModeLabel"> <widget class="QLabel" name="formattingModeLabel">
<property name="text"> <property name="text">
@@ -39,7 +39,7 @@
<widget class="QComboBox" name="indentingOrFormatting"/> <widget class="QComboBox" name="indentingOrFormatting"/>
</item> </item>
<item> <item>
<spacer name="horizontalSpacer_2"> <spacer name="formattingModeSpacer">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
@@ -53,6 +53,20 @@
</item> </item>
</layout> </layout>
</item> </item>
<item>
<widget class="QCheckBox" name="formatWhileTyping">
<property name="text">
<string>Format while typing</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="formatOnSave">
<property name="text">
<string>Format edited code on file save</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QLabel" name="projectHasClangFormat"> <widget class="QLabel" name="projectHasClangFormat">
<property name="text"> <property name="text">

View File

@@ -32,6 +32,8 @@ static const char SETTINGS_FILE_ALT_NAME[] = "_clang-format";
static const char SAMPLE_FILE_NAME[] = "test.cpp"; static const char SAMPLE_FILE_NAME[] = "test.cpp";
static const char SETTINGS_ID[] = "ClangFormat"; static const char SETTINGS_ID[] = "ClangFormat";
static const char OVERRIDE_FILE_ID[] = "ClangFormat.OverrideFile"; 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 MODE_ID[] = "ClangFormat.Mode";
static const char OPEN_CURRENT_CONFIG_ID[] = "ClangFormat.OpenCurrentConfig"; static const char OPEN_CURRENT_CONFIG_ID[] = "ClangFormat.OpenCurrentConfig";
} // namespace Constants } // namespace Constants

View File

@@ -114,7 +114,13 @@ int ClangFormatIndenter::lastSaveRevision() const
bool ClangFormatIndenter::formatOnSave() const bool ClangFormatIndenter::formatOnSave() const
{ {
return !isBeautifierOnSaveActivated() && formatCodeInsteadOfIndent(); return ClangFormatSettings::instance().formatOnSave() && !isBeautifierOnSaveActivated()
&& formatCodeInsteadOfIndent();
}
bool ClangFormatIndenter::formatWhileTyping() const
{
return ClangFormatSettings::instance().formatWhileTyping() && formatCodeInsteadOfIndent();
} }
} // namespace ClangFormat } // namespace ClangFormat

View File

@@ -40,6 +40,7 @@ public:
private: private:
bool formatCodeInsteadOfIndent() const override; bool formatCodeInsteadOfIndent() const override;
bool formatWhileTyping() const override;
int lastSaveRevision() const override; int lastSaveRevision() const override;
}; };

View File

@@ -30,8 +30,6 @@
namespace ClangFormat { namespace ClangFormat {
static const char FORMAT_CODE_INSTEAD_OF_INDENT_ID[] = "ClangFormat.FormatCodeInsteadOfIndent"; static const char FORMAT_CODE_INSTEAD_OF_INDENT_ID[] = "ClangFormat.FormatCodeInsteadOfIndent";
static const char FORMAT_CODE_ON_SAVE_ID[] = "ClangFormat.FormatCodeOnSave";
static const char FORMAT_WHILE_TYPING_ID[] = "ClangFormat.FormatWhileTyping";
ClangFormatSettings &ClangFormatSettings::instance() ClangFormatSettings &ClangFormatSettings::instance()
{ {
@@ -45,15 +43,15 @@ ClangFormatSettings::ClangFormatSettings()
settings->beginGroup(QLatin1String(Constants::SETTINGS_ID)); settings->beginGroup(QLatin1String(Constants::SETTINGS_ID));
m_overrideDefaultFile = settings->value(QLatin1String(Constants::OVERRIDE_FILE_ID), false) m_overrideDefaultFile = settings->value(QLatin1String(Constants::OVERRIDE_FILE_ID), false)
.toBool(); .toBool();
m_formatWhileTyping = settings->value(QLatin1String(Constants::FORMAT_WHILE_TYPING_ID), false)
.toBool();
m_formatOnSave = settings->value(QLatin1String(Constants::FORMAT_CODE_ON_SAVE_ID), false)
.toBool();
// 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
= settings->value(QLatin1String(FORMAT_CODE_INSTEAD_OF_INDENT_ID), false).toBool() = settings->value(QLatin1String(FORMAT_CODE_INSTEAD_OF_INDENT_ID), false).toBool();
|| settings->value(QLatin1String(FORMAT_CODE_ON_SAVE_ID), false).toBool();
Core::ICore::settings()->remove(QLatin1String(FORMAT_CODE_INSTEAD_OF_INDENT_ID)); Core::ICore::settings()->remove(QLatin1String(FORMAT_CODE_INSTEAD_OF_INDENT_ID));
Core::ICore::settings()->remove(QLatin1String(FORMAT_CODE_ON_SAVE_ID));
Core::ICore::settings()->remove(QLatin1String(FORMAT_WHILE_TYPING_ID));
if (isOldFormattingOn) { if (isOldFormattingOn) {
settings->setValue(QLatin1String(Constants::MODE_ID), settings->setValue(QLatin1String(Constants::MODE_ID),
@@ -72,6 +70,8 @@ void ClangFormatSettings::write() const
QSettings *settings = Core::ICore::settings(); QSettings *settings = Core::ICore::settings();
settings->beginGroup(QLatin1String(Constants::SETTINGS_ID)); settings->beginGroup(QLatin1String(Constants::SETTINGS_ID));
settings->setValue(QLatin1String(Constants::OVERRIDE_FILE_ID), m_overrideDefaultFile); settings->setValue(QLatin1String(Constants::OVERRIDE_FILE_ID), m_overrideDefaultFile);
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::MODE_ID), static_cast<int>(m_mode));
settings->endGroup(); settings->endGroup();
} }
@@ -86,6 +86,26 @@ bool ClangFormatSettings::overrideDefaultFile() const
return m_overrideDefaultFile; return m_overrideDefaultFile;
} }
void ClangFormatSettings::setFormatWhileTyping(bool enable)
{
m_formatWhileTyping = enable;
}
bool ClangFormatSettings::formatWhileTyping() const
{
return m_formatWhileTyping;
}
void ClangFormatSettings::setFormatOnSave(bool enable)
{
m_formatOnSave = enable;
}
bool ClangFormatSettings::formatOnSave() const
{
return m_formatOnSave;
}
void ClangFormatSettings::setMode(Mode mode) void ClangFormatSettings::setMode(Mode mode)
{ {
m_mode = mode; m_mode = mode;

View File

@@ -49,9 +49,17 @@ public:
void setMode(Mode mode); void setMode(Mode mode);
Mode mode() const; Mode mode() const;
void setFormatWhileTyping(bool enable);
bool formatWhileTyping() const;
void setFormatOnSave(bool enable);
bool formatOnSave() const;
private: private:
bool m_overrideDefaultFile = false;
Mode m_mode; Mode m_mode;
bool m_overrideDefaultFile = false;
bool m_formatWhileTyping = false;
bool m_formatOnSave = false;
}; };
} // namespace ClangFormat } // namespace ClangFormat

View File

@@ -53,6 +53,7 @@ public:
private: private:
bool formatCodeInsteadOfIndent() const override { return true; } bool formatCodeInsteadOfIndent() const override { return true; }
bool formatWhileTyping() const override { return true; }
}; };
ClangFormatTest::ClangFormatTest() ClangFormatTest::ClangFormatTest()