diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index d9e36e3717a..2c1cdf1a647 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 1a28f4d6995..4a81d3bd694 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 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(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(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(); + connect(m_ui->indentingOrFormatting, &QComboBox::currentIndexChanged, this, [this](int) { + if (m_project) + apply(); + }); - m_ui->formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping()); - m_ui->formatWhileTyping->show(); + if (ClangFormatSettings::instance().formatCodeInsteadOfIndent()) + m_ui->indentingOrFormatting->setCurrentIndex(formatIndex()); + else + m_ui->indentingOrFormatting->setCurrentIndex(indentIndex()); - 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)); - } + 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 cf4bc1104c3..fb6ffa04e0b 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 45b9801d7ff..814f558ca8a 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.ui +++ b/src/plugins/clangformat/clangformatconfigwidget.ui @@ -26,35 +26,17 @@ 8 - - - - Format instead of indenting - - - - - - - Format while typing - - - - + - Format edited code on file save + Formatting mode: - - - - - + @@ -88,37 +70,12 @@ - Fallback configuration + Clang-Format Style - - - - - - - - - Apply - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - +