forked from qt-creator/qt-creator
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:
@@ -594,7 +594,7 @@ Utils::Text::Replacements ClangFormatBaseIndenter::indentsFor(QTextBlock startBl
|
||||
const QByteArray buffer = m_doc->toPlainText().toUtf8();
|
||||
|
||||
ReplacementsToKeep replacementsToKeep = ReplacementsToKeep::OnlyIndent;
|
||||
if (formatCodeInsteadOfIndent()
|
||||
if (formatWhileTyping()
|
||||
&& (cursorPositionInEditor == -1 || cursorPositionInEditor >= startBlockPosition)
|
||||
&& (typedChar == ';' || typedChar == '}')) {
|
||||
// Format before current position only in case the cursor is inside the indented block.
|
||||
|
||||
@@ -72,6 +72,7 @@ public:
|
||||
|
||||
protected:
|
||||
virtual bool formatCodeInsteadOfIndent() const { return false; }
|
||||
virtual bool formatWhileTyping() const { return false; }
|
||||
virtual int lastSaveRevision() const { return 0; }
|
||||
|
||||
private:
|
||||
|
||||
@@ -85,6 +85,7 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
|
||||
|
||||
initChecksAndPreview(!codeStyle->isReadOnly());
|
||||
initOverrideCheckBox();
|
||||
initCheckBoxes();
|
||||
initIndentationOrFormattingCombobox();
|
||||
|
||||
showOrHideWidgets();
|
||||
@@ -128,6 +129,45 @@ void ClangFormatConfigWidget::initChecksAndPreview(bool enabled)
|
||||
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()
|
||||
{
|
||||
if (m_project) {
|
||||
@@ -190,7 +230,9 @@ void ClangFormatConfigWidget::initIndentationOrFormattingCombobox()
|
||||
m_ui->indentingOrFormatting->setCurrentIndex(
|
||||
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),
|
||||
this, [](int index) {
|
||||
@@ -219,6 +261,7 @@ void ClangFormatConfigWidget::showOrHideWidgets()
|
||||
|
||||
if (!m_ui->overrideDefault->isChecked() && m_project) {
|
||||
// Show the fallback configuration only globally.
|
||||
m_ui->fallbackConfig->hide();
|
||||
m_checksScrollArea->hide();
|
||||
m_preview->hide();
|
||||
m_ui->verticalLayout->addStretch(1);
|
||||
@@ -226,6 +269,7 @@ void ClangFormatConfigWidget::showOrHideWidgets()
|
||||
}
|
||||
|
||||
createStyleFileIfNeeded(!m_project);
|
||||
m_ui->fallbackConfig->show();
|
||||
m_checksScrollArea->show();
|
||||
m_preview->show();
|
||||
|
||||
|
||||
@@ -65,6 +65,7 @@ private:
|
||||
void showOrHideWidgets();
|
||||
void initChecksAndPreview(bool enabled);
|
||||
void initOverrideCheckBox();
|
||||
void initCheckBoxes();
|
||||
void connectChecks();
|
||||
|
||||
void fillTable();
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
<number>8</number>
|
||||
</property>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_3">
|
||||
<layout class="QHBoxLayout" name="formattingModeLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="formattingModeLabel">
|
||||
<property name="text">
|
||||
@@ -39,7 +39,7 @@
|
||||
<widget class="QComboBox" name="indentingOrFormatting"/>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_2">
|
||||
<spacer name="formattingModeSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
@@ -53,6 +53,20 @@
|
||||
</item>
|
||||
</layout>
|
||||
</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>
|
||||
<widget class="QLabel" name="projectHasClangFormat">
|
||||
<property name="text">
|
||||
|
||||
@@ -32,6 +32,8 @@ static const char SETTINGS_FILE_ALT_NAME[] = "_clang-format";
|
||||
static const char SAMPLE_FILE_NAME[] = "test.cpp";
|
||||
static const char SETTINGS_ID[] = "ClangFormat";
|
||||
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 OPEN_CURRENT_CONFIG_ID[] = "ClangFormat.OpenCurrentConfig";
|
||||
} // namespace Constants
|
||||
|
||||
@@ -114,7 +114,13 @@ int ClangFormatIndenter::lastSaveRevision() 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
|
||||
|
||||
@@ -40,6 +40,7 @@ public:
|
||||
|
||||
private:
|
||||
bool formatCodeInsteadOfIndent() const override;
|
||||
bool formatWhileTyping() const override;
|
||||
int lastSaveRevision() const override;
|
||||
};
|
||||
|
||||
|
||||
@@ -30,8 +30,6 @@
|
||||
|
||||
namespace ClangFormat {
|
||||
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()
|
||||
{
|
||||
@@ -45,15 +43,15 @@ ClangFormatSettings::ClangFormatSettings()
|
||||
settings->beginGroup(QLatin1String(Constants::SETTINGS_ID));
|
||||
m_overrideDefaultFile = settings->value(QLatin1String(Constants::OVERRIDE_FILE_ID), false)
|
||||
.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
|
||||
bool isOldFormattingOn
|
||||
= settings->value(QLatin1String(FORMAT_CODE_INSTEAD_OF_INDENT_ID), false).toBool()
|
||||
|| settings->value(QLatin1String(FORMAT_CODE_ON_SAVE_ID), false).toBool();
|
||||
|
||||
= settings->value(QLatin1String(FORMAT_CODE_INSTEAD_OF_INDENT_ID), false).toBool();
|
||||
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) {
|
||||
settings->setValue(QLatin1String(Constants::MODE_ID),
|
||||
@@ -72,6 +70,8 @@ void ClangFormatSettings::write() const
|
||||
QSettings *settings = Core::ICore::settings();
|
||||
settings->beginGroup(QLatin1String(Constants::SETTINGS_ID));
|
||||
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->endGroup();
|
||||
}
|
||||
@@ -86,6 +86,26 @@ bool ClangFormatSettings::overrideDefaultFile() const
|
||||
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)
|
||||
{
|
||||
m_mode = mode;
|
||||
|
||||
@@ -49,9 +49,17 @@ public:
|
||||
void setMode(Mode mode);
|
||||
Mode mode() const;
|
||||
|
||||
void setFormatWhileTyping(bool enable);
|
||||
bool formatWhileTyping() const;
|
||||
|
||||
void setFormatOnSave(bool enable);
|
||||
bool formatOnSave() const;
|
||||
|
||||
private:
|
||||
bool m_overrideDefaultFile = false;
|
||||
Mode m_mode;
|
||||
bool m_overrideDefaultFile = false;
|
||||
bool m_formatWhileTyping = false;
|
||||
bool m_formatOnSave = false;
|
||||
};
|
||||
|
||||
} // namespace ClangFormat
|
||||
|
||||
@@ -53,6 +53,7 @@ public:
|
||||
|
||||
private:
|
||||
bool formatCodeInsteadOfIndent() const override { return true; }
|
||||
bool formatWhileTyping() const override { return true; }
|
||||
};
|
||||
|
||||
ClangFormatTest::ClangFormatTest()
|
||||
|
||||
Reference in New Issue
Block a user