diff options
author | Artem Sokolovskii <artem.sokolovskii@qt.io> | 2022-02-16 15:28:16 +0100 |
---|---|---|
committer | Artem Sokolovskii <artem.sokolovskii@qt.io> | 2022-03-25 14:17:48 +0000 |
commit | 4022ed547defe2ef9ad932e633c03ad228135476 (patch) | |
tree | 950009f41519036b653090fea833ce55cda12273 /src/plugins/clangformat | |
parent | ed94e0c0662aa01f97af327b8e269613806e853d (diff) |
[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>
Diffstat (limited to 'src/plugins/clangformat')
4 files changed, 38 insertions, 106 deletions
diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index d9e36e3717..2c1cdf1a64 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -585,7 +585,7 @@ Utils::Text::Replacements ClangFormatBaseIndenter::indentsFor(QTextBlock startBl const QByteArray buffer = m_doc->toPlainText().toUtf8(); ReplacementsToKeep replacementsToKeep = ReplacementsToKeep::OnlyIndent; - if (formatWhileTyping() + if (formatCodeInsteadOfIndent() && (cursorPositionInEditor == -1 || cursorPositionInEditor >= startBlockPosition) && (typedChar == ';' || typedChar == '}')) { // Format before current position only in case the cursor is inside the indented block. diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index 1a28f4d699..4a81d3bd69 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -60,9 +60,6 @@ using namespace ProjectExplorer; namespace ClangFormat { -static const char kFileSaveWarning[] - = "Disable formatting on file save in the Beautifier plugin to enable this check"; - static bool isBeautifierPluginActivated() { const QVector<ExtensionSystem::PluginSpec *> specs = ExtensionSystem::PluginManager::plugins(); @@ -90,6 +87,10 @@ static bool isBeautifierOnSaveActivated() return activated; } +static int indentIndex() { return 0; } +static int formatIndex() { return 1; } + + bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event) { 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); } -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) : CppCodeStyleWidget(parent) , m_project(project) @@ -130,16 +115,12 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *proje m_config = std::make_unique<ClangFormatFile>(filePath); initChecksAndPreview(); + showCombobox(); if (m_project) { - m_ui->applyButton->show(); - hideGlobalCheckboxes(); - m_ui->fallbackConfig->hide(); m_ui->overrideDefault->setChecked( m_project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool()); } else { - m_ui->applyButton->hide(); - showGlobalCheckboxes(); m_ui->overrideDefault->setChecked(ClangFormatSettings::instance().overrideDefaultFile()); m_ui->overrideDefault->setToolTip( tr("Override Clang Format configuration file with the fallback configuration.")); @@ -180,11 +161,11 @@ void ClangFormatConfigWidget::initChecksAndPreview() Utils::FilePath fileName; if (m_project) { - connect(m_ui->applyButton, &QPushButton::clicked, this, &ClangFormatConfigWidget::apply); fileName = m_project->projectFilePath().pathAppended("snippet.cpp"); } else { fileName = Core::ICore::userResourcePath("snippet.cpp"); } + m_preview->textDocument()->indenter()->setFileName(fileName); } @@ -215,28 +196,22 @@ void ClangFormatConfigWidget::onTableChanged() saveChanges(sender()); } -void ClangFormatConfigWidget::hideGlobalCheckboxes() +void ClangFormatConfigWidget::showCombobox() { - m_ui->formatAlways->hide(); - m_ui->formatWhileTyping->hide(); - m_ui->formatOnSave->hide(); -} + m_ui->indentingOrFormatting->insertItem(indentIndex(), tr("Indenting only")); + m_ui->indentingOrFormatting->insertItem(formatIndex(), tr("Full formatting")); -void ClangFormatConfigWidget::showGlobalCheckboxes() -{ - m_ui->formatAlways->setChecked(ClangFormatSettings::instance().formatCodeInsteadOfIndent()); - m_ui->formatAlways->show(); - - m_ui->formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping()); - m_ui->formatWhileTyping->show(); - - m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave()); - m_ui->formatOnSave->show(); - if (isBeautifierOnSaveActivated()) { - m_ui->formatOnSave->setChecked(false); - m_ui->formatOnSave->setEnabled(false); - m_ui->fileSaveWarning->setText(tr(kFileSaveWarning)); - } + connect(m_ui->indentingOrFormatting, &QComboBox::currentIndexChanged, this, [this](int) { + if (m_project) + apply(); + }); + + if (ClangFormatSettings::instance().formatCodeInsteadOfIndent()) + m_ui->indentingOrFormatting->setCurrentIndex(formatIndex()); + else + m_ui->indentingOrFormatting->setCurrentIndex(indentIndex()); + + m_ui->indentingOrFormatting->show(); } static bool projectConfigExists() @@ -268,9 +243,7 @@ void ClangFormatConfigWidget::showOrHideWidgets() m_checksScrollArea->show(); m_preview->show(); - if (m_project) { - m_ui->projectHasClangFormat->hide(); - } else { + if (!m_project) { const Project *currentProject = SessionManager::startupProject(); if (!currentProject || !projectConfigExists()) { m_ui->projectHasClangFormat->hide(); @@ -467,12 +440,16 @@ void ClangFormatConfigWidget::synchronize() void ClangFormatConfigWidget::apply() { ClangFormatSettings &settings = ClangFormatSettings::instance(); - if (!m_project) { - settings.setFormatCodeInsteadOfIndent(m_ui->formatAlways->isChecked()); - settings.setFormatWhileTyping(m_ui->formatWhileTyping->isChecked()); - settings.setFormatOnSave(m_ui->formatOnSave->isChecked()); - settings.setOverrideDefaultFile(m_ui->overrideDefault->isChecked()); - } else { + const bool isFormatting = m_ui->indentingOrFormatting->currentIndex() + == formatIndex(); + settings.setFormatCodeInsteadOfIndent(isFormatting); + settings.setOverrideDefaultFile(m_ui->overrideDefault->isChecked()); + + if (!isBeautifierOnSaveActivated()) { + settings.setFormatOnSave(isFormatting); + } + + if (m_project) { m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, m_ui->overrideDefault->isChecked()); } settings.write(); diff --git a/src/plugins/clangformat/clangformatconfigwidget.h b/src/plugins/clangformat/clangformatconfigwidget.h index cf4bc1104c..fb6ffa04e0 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.h +++ b/src/plugins/clangformat/clangformatconfigwidget.h @@ -60,7 +60,6 @@ private: void onTableChanged(); bool eventFilter(QObject *object, QEvent *event) override; - void showEvent(QShowEvent *event) override; void showOrHideWidgets(); void initChecksAndPreview(); @@ -69,8 +68,7 @@ private: void fillTable(); void saveChanges(QObject *sender); - void hideGlobalCheckboxes(); - void showGlobalCheckboxes(); + void showCombobox(); void updatePreview(); ProjectExplorer::Project *m_project; diff --git a/src/plugins/clangformat/clangformatconfigwidget.ui b/src/plugins/clangformat/clangformatconfigwidget.ui index 45b9801d7f..814f558ca8 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.ui +++ b/src/plugins/clangformat/clangformatconfigwidget.ui @@ -27,34 +27,16 @@ <number>8</number> </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> <layout class="QHBoxLayout" name="horizontalLayout_3"> <item> - <widget class="QCheckBox" name="formatOnSave"> + <widget class="QLabel" name="formattingModeLabel"> <property name="text"> - <string>Format edited code on file save</string> + <string>Formatting mode:</string> </property> </widget> </item> <item> - <widget class="QLabel" name="fileSaveWarning"> - <property name="text"> - <string/> - </property> - </widget> + <widget class="QComboBox" name="indentingOrFormatting"/> </item> <item> <spacer name="horizontalSpacer_2"> @@ -88,37 +70,12 @@ <item> <widget class="QLabel" name="fallbackConfig"> <property name="text"> - <string>Fallback configuration</string> + <string>Clang-Format Style</string> </property> </widget> </item> <item> - <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> + <layout class="QHBoxLayout" name="horizontalLayout_2"/> </item> </layout> </widget> |