[ClangFormat] Remove redundant checkboxes

- Removed redundant checkboxes
- Removed apply button
- Combined checkboxes to combobox

ToDo:
- Specify behavior for global and project settings

Change-Id: I39a00ac8439ae7be3041890f7fc882849685d102
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Artem Sokolovskii
2022-02-16 15:28:16 +01:00
parent ed94e0c066
commit 4022ed547d
4 changed files with 36 additions and 104 deletions

View File

@@ -585,7 +585,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 (formatWhileTyping() if (formatCodeInsteadOfIndent()
&& (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

@@ -60,9 +60,6 @@ using namespace ProjectExplorer;
namespace ClangFormat { namespace ClangFormat {
static const char kFileSaveWarning[]
= "Disable formatting on file save in the Beautifier plugin to enable this check";
static bool isBeautifierPluginActivated() static bool isBeautifierPluginActivated()
{ {
const QVector<ExtensionSystem::PluginSpec *> specs = ExtensionSystem::PluginManager::plugins(); const QVector<ExtensionSystem::PluginSpec *> specs = ExtensionSystem::PluginManager::plugins();
@@ -90,6 +87,10 @@ static bool isBeautifierOnSaveActivated()
return activated; return activated;
} }
static int indentIndex() { return 0; }
static int formatIndex() { return 1; }
bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event) bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event)
{ {
if (event->type() == QEvent::Wheel && qobject_cast<QComboBox *>(object)) { if (event->type() == QEvent::Wheel && qobject_cast<QComboBox *>(object)) {
@@ -99,22 +100,6 @@ bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event)
return QWidget::eventFilter(object, event); return QWidget::eventFilter(object, event);
} }
void ClangFormatConfigWidget::showEvent(QShowEvent *event)
{
TextEditor::CodeStyleEditorWidget::showEvent(event);
if (isBeautifierOnSaveActivated()) {
bool wasEnabled = m_ui->formatOnSave->isEnabled();
m_ui->formatOnSave->setChecked(false);
m_ui->formatOnSave->setEnabled(false);
m_ui->fileSaveWarning->setText(tr(kFileSaveWarning));
if (wasEnabled)
apply();
} else {
m_ui->formatOnSave->setEnabled(true);
m_ui->fileSaveWarning->setText("");
}
}
ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *project, QWidget *parent) ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *project, QWidget *parent)
: CppCodeStyleWidget(parent) : CppCodeStyleWidget(parent)
, m_project(project) , m_project(project)
@@ -130,16 +115,12 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *proje
m_config = std::make_unique<ClangFormatFile>(filePath); m_config = std::make_unique<ClangFormatFile>(filePath);
initChecksAndPreview(); initChecksAndPreview();
showCombobox();
if (m_project) { if (m_project) {
m_ui->applyButton->show();
hideGlobalCheckboxes();
m_ui->fallbackConfig->hide();
m_ui->overrideDefault->setChecked( m_ui->overrideDefault->setChecked(
m_project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool()); m_project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool());
} else { } else {
m_ui->applyButton->hide();
showGlobalCheckboxes();
m_ui->overrideDefault->setChecked(ClangFormatSettings::instance().overrideDefaultFile()); m_ui->overrideDefault->setChecked(ClangFormatSettings::instance().overrideDefaultFile());
m_ui->overrideDefault->setToolTip( m_ui->overrideDefault->setToolTip(
tr("Override Clang Format configuration file with the fallback configuration.")); tr("Override Clang Format configuration file with the fallback configuration."));
@@ -180,11 +161,11 @@ void ClangFormatConfigWidget::initChecksAndPreview()
Utils::FilePath fileName; Utils::FilePath fileName;
if (m_project) { if (m_project) {
connect(m_ui->applyButton, &QPushButton::clicked, this, &ClangFormatConfigWidget::apply);
fileName = m_project->projectFilePath().pathAppended("snippet.cpp"); fileName = m_project->projectFilePath().pathAppended("snippet.cpp");
} else { } else {
fileName = Core::ICore::userResourcePath("snippet.cpp"); fileName = Core::ICore::userResourcePath("snippet.cpp");
} }
m_preview->textDocument()->indenter()->setFileName(fileName); m_preview->textDocument()->indenter()->setFileName(fileName);
} }
@@ -215,28 +196,22 @@ void ClangFormatConfigWidget::onTableChanged()
saveChanges(sender()); saveChanges(sender());
} }
void ClangFormatConfigWidget::hideGlobalCheckboxes() void ClangFormatConfigWidget::showCombobox()
{ {
m_ui->formatAlways->hide(); m_ui->indentingOrFormatting->insertItem(indentIndex(), tr("Indenting only"));
m_ui->formatWhileTyping->hide(); m_ui->indentingOrFormatting->insertItem(formatIndex(), tr("Full formatting"));
m_ui->formatOnSave->hide();
}
void ClangFormatConfigWidget::showGlobalCheckboxes() connect(m_ui->indentingOrFormatting, &QComboBox::currentIndexChanged, this, [this](int) {
{ if (m_project)
m_ui->formatAlways->setChecked(ClangFormatSettings::instance().formatCodeInsteadOfIndent()); apply();
m_ui->formatAlways->show(); });
m_ui->formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping()); if (ClangFormatSettings::instance().formatCodeInsteadOfIndent())
m_ui->formatWhileTyping->show(); m_ui->indentingOrFormatting->setCurrentIndex(formatIndex());
else
m_ui->indentingOrFormatting->setCurrentIndex(indentIndex());
m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave()); m_ui->indentingOrFormatting->show();
m_ui->formatOnSave->show();
if (isBeautifierOnSaveActivated()) {
m_ui->formatOnSave->setChecked(false);
m_ui->formatOnSave->setEnabled(false);
m_ui->fileSaveWarning->setText(tr(kFileSaveWarning));
}
} }
static bool projectConfigExists() static bool projectConfigExists()
@@ -268,9 +243,7 @@ void ClangFormatConfigWidget::showOrHideWidgets()
m_checksScrollArea->show(); m_checksScrollArea->show();
m_preview->show(); m_preview->show();
if (m_project) { if (!m_project) {
m_ui->projectHasClangFormat->hide();
} else {
const Project *currentProject = SessionManager::startupProject(); const Project *currentProject = SessionManager::startupProject();
if (!currentProject || !projectConfigExists()) { if (!currentProject || !projectConfigExists()) {
m_ui->projectHasClangFormat->hide(); m_ui->projectHasClangFormat->hide();
@@ -467,12 +440,16 @@ void ClangFormatConfigWidget::synchronize()
void ClangFormatConfigWidget::apply() void ClangFormatConfigWidget::apply()
{ {
ClangFormatSettings &settings = ClangFormatSettings::instance(); ClangFormatSettings &settings = ClangFormatSettings::instance();
if (!m_project) { const bool isFormatting = m_ui->indentingOrFormatting->currentIndex()
settings.setFormatCodeInsteadOfIndent(m_ui->formatAlways->isChecked()); == formatIndex();
settings.setFormatWhileTyping(m_ui->formatWhileTyping->isChecked()); settings.setFormatCodeInsteadOfIndent(isFormatting);
settings.setFormatOnSave(m_ui->formatOnSave->isChecked()); settings.setOverrideDefaultFile(m_ui->overrideDefault->isChecked());
settings.setOverrideDefaultFile(m_ui->overrideDefault->isChecked());
} else { if (!isBeautifierOnSaveActivated()) {
settings.setFormatOnSave(isFormatting);
}
if (m_project) {
m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, m_ui->overrideDefault->isChecked()); m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, m_ui->overrideDefault->isChecked());
} }
settings.write(); settings.write();

View File

@@ -60,7 +60,6 @@ private:
void onTableChanged(); void onTableChanged();
bool eventFilter(QObject *object, QEvent *event) override; bool eventFilter(QObject *object, QEvent *event) override;
void showEvent(QShowEvent *event) override;
void showOrHideWidgets(); void showOrHideWidgets();
void initChecksAndPreview(); void initChecksAndPreview();
@@ -69,8 +68,7 @@ private:
void fillTable(); void fillTable();
void saveChanges(QObject *sender); void saveChanges(QObject *sender);
void hideGlobalCheckboxes(); void showCombobox();
void showGlobalCheckboxes();
void updatePreview(); void updatePreview();
ProjectExplorer::Project *m_project; ProjectExplorer::Project *m_project;

View File

@@ -26,35 +26,17 @@
<property name="bottomMargin"> <property name="bottomMargin">
<number>8</number> <number>8</number>
</property> </property>
<item>
<widget class="QCheckBox" name="formatAlways">
<property name="text">
<string>Format instead of indenting</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="formatWhileTyping">
<property name="text">
<string>Format while typing</string>
</property>
</widget>
</item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_3"> <layout class="QHBoxLayout" name="horizontalLayout_3">
<item> <item>
<widget class="QCheckBox" name="formatOnSave"> <widget class="QLabel" name="formattingModeLabel">
<property name="text"> <property name="text">
<string>Format edited code on file save</string> <string>Formatting mode:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QLabel" name="fileSaveWarning"> <widget class="QComboBox" name="indentingOrFormatting"/>
<property name="text">
<string/>
</property>
</widget>
</item> </item>
<item> <item>
<spacer name="horizontalSpacer_2"> <spacer name="horizontalSpacer_2">
@@ -88,37 +70,12 @@
<item> <item>
<widget class="QLabel" name="fallbackConfig"> <widget class="QLabel" name="fallbackConfig">
<property name="text"> <property name="text">
<string>Fallback configuration</string> <string>Clang-Format Style</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QHBoxLayout" name="horizontalLayout_2"/>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="applyButton">
<property name="text">
<string>Apply</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item> </item>
</layout> </layout>
</widget> </widget>