diff --git a/doc/src/editors/creator-beautifier.qdoc b/doc/src/editors/creator-beautifier.qdoc index b8f7f36db55..0da50727e90 100644 --- a/doc/src/editors/creator-beautifier.qdoc +++ b/doc/src/editors/creator-beautifier.qdoc @@ -137,6 +137,11 @@ same directory as the source file or in one of its parent directories. + To specify a fallback style to use if the style configuration + file is not available, use the \uicontrol {Fallback style} + combo box. Select \uicontrol Default to use the default style. + Select \uicontrol None to skip formatting. + \li Select the \uicontrol {Use customized style} option, and then \uicontrol Add to define your own style. diff --git a/src/plugins/beautifier/clangformat/clangformat.cpp b/src/plugins/beautifier/clangformat/clangformat.cpp index efc7c73399d..cf4abc6453a 100644 --- a/src/plugins/beautifier/clangformat/clangformat.cpp +++ b/src/plugins/beautifier/clangformat/clangformat.cpp @@ -137,7 +137,14 @@ Command ClangFormat::command() const command.setProcessing(Command::PipeProcessing); if (m_settings->usePredefinedStyle()) { - command.addOption("-style=" + m_settings->predefinedStyle()); + const QString predefinedStyle = m_settings->predefinedStyle(); + command.addOption("-style=" + predefinedStyle); + if (predefinedStyle == "File") { + const QString fallbackStyle = m_settings->fallbackStyle(); + if (fallbackStyle != "Default") + command.addOption("-fallback-style=" + fallbackStyle); + } + command.addOption("-assume-filename=%file"); } else { command.addOption("-style=file"); diff --git a/src/plugins/beautifier/clangformat/clangformatoptionspage.cpp b/src/plugins/beautifier/clangformat/clangformatoptionspage.cpp index 59f9ef5f5b2..a9ed3cef25d 100644 --- a/src/plugins/beautifier/clangformat/clangformatoptionspage.cpp +++ b/src/plugins/beautifier/clangformat/clangformatoptionspage.cpp @@ -47,10 +47,18 @@ ClangFormatOptionsPageWidget::ClangFormatOptionsPageWidget(ClangFormatSettings * ui->setupUi(this); ui->options->setEnabled(false); ui->predefinedStyle->addItems(m_settings->predefinedStyles()); + ui->fallbackStyle->addItems(m_settings->fallbackStyles()); ui->command->setExpectedKind(Utils::PathChooser::ExistingCommand); ui->command->setPromptDialogTitle( BeautifierPlugin::msgCommandPromptDialogTitle("Clang Format")); connect(ui->command, &Utils::PathChooser::validChanged, ui->options, &QWidget::setEnabled); + connect(ui->predefinedStyle, &QComboBox::currentTextChanged, [this](const QString &item) { + ui->fallbackStyle->setEnabled(item == "File"); + }); + connect(ui->usePredefinedStyle, &QRadioButton::toggled, [this](bool checked) { + ui->fallbackStyle->setEnabled(checked && ui->predefinedStyle->currentText() == "File"); + ui->predefinedStyle->setEnabled(checked); + }); ui->configurations->setSettings(m_settings); } @@ -63,9 +71,12 @@ void ClangFormatOptionsPageWidget::restore() { ui->command->setPath(m_settings->command()); ui->mime->setText(m_settings->supportedMimeTypesAsString()); - const int textIndex = ui->predefinedStyle->findText(m_settings->predefinedStyle()); - if (textIndex != -1) - ui->predefinedStyle->setCurrentIndex(textIndex); + const int predefinedStyleIndex = ui->predefinedStyle->findText(m_settings->predefinedStyle()); + if (predefinedStyleIndex != -1) + ui->predefinedStyle->setCurrentIndex(predefinedStyleIndex); + const int fallbackStyleIndex = ui->fallbackStyle->findText(m_settings->fallbackStyle()); + if (fallbackStyleIndex != -1) + ui->fallbackStyle->setCurrentIndex(fallbackStyleIndex); ui->formatEntireFileFallback->setChecked(m_settings->formatEntireFileFallback()); ui->configurations->setSettings(m_settings); ui->configurations->setCurrentConfiguration(m_settings->customStyle()); @@ -82,6 +93,7 @@ void ClangFormatOptionsPageWidget::apply() m_settings->setSupportedMimeTypes(ui->mime->text()); m_settings->setUsePredefinedStyle(ui->usePredefinedStyle->isChecked()); m_settings->setPredefinedStyle(ui->predefinedStyle->currentText()); + m_settings->setFallbackStyle(ui->fallbackStyle->currentText()); m_settings->setCustomStyle(ui->configurations->currentConfiguration()); m_settings->setFormatEntireFileFallback(ui->formatEntireFileFallback->isChecked()); m_settings->save(); diff --git a/src/plugins/beautifier/clangformat/clangformatoptionspage.ui b/src/plugins/beautifier/clangformat/clangformatoptionspage.ui index 91788a31465..c40b28c0417 100644 --- a/src/plugins/beautifier/clangformat/clangformatoptionspage.ui +++ b/src/plugins/beautifier/clangformat/clangformatoptionspage.ui @@ -6,15 +6,102 @@ 0 0 - 817 + 727 631 Form - - + + + + + Options + + + + + + + 0 + 0 + + + + Use predefined style: + + + true + + + true + + + + + + + + + + + 0 + 0 + + + + Fallback style: + + + + + + + false + + + + + + + Use customized style: + + + true + + + + + + + + + + For action Format Selected Text. + + + Format entire file if no text was selected + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + Configuration @@ -43,70 +130,6 @@ - - - - Options - - - - QFormLayout::AllNonFixedFieldsGrow - - - - - Use predefined style: - - - true - - - true - - - - - - - - - - Use customized style: - - - true - - - - - - - - - - For action Format Selected Text - - - Format entire file if no text was selected - - - - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - @@ -124,22 +147,5 @@ - - - usePredefinedStyle - toggled(bool) - predefinedStyle - setEnabled(bool) - - - 67 - 108 - - - 240 - 113 - - - - + diff --git a/src/plugins/beautifier/clangformat/clangformatsettings.cpp b/src/plugins/beautifier/clangformat/clangformatsettings.cpp index d91569c3e64..f4c2189181b 100644 --- a/src/plugins/beautifier/clangformat/clangformatsettings.cpp +++ b/src/plugins/beautifier/clangformat/clangformatsettings.cpp @@ -41,6 +41,7 @@ namespace ClangFormat { namespace { const char USE_PREDEFINED_STYLE[] = "usePredefinedStyle"; const char PREDEFINED_STYLE[] = "predefinedStyle"; +const char FALLBACK_STYLE[] = "fallbackStyle"; const char CUSTOM_STYLE[] = "customStyle"; const char FORMAT_ENTIRE_FILE_FALLBACK[] = "formatEntireFileFallback"; } @@ -51,6 +52,7 @@ ClangFormatSettings::ClangFormatSettings() : setCommand("clang-format"); m_settings.insert(USE_PREDEFINED_STYLE, QVariant(true)); m_settings.insert(PREDEFINED_STYLE, "LLVM"); + m_settings.insert(FALLBACK_STYLE, "Default"); m_settings.insert(CUSTOM_STYLE, QVariant()); m_settings.insert(FORMAT_ENTIRE_FILE_FALLBACK, QVariant(true)); read(); @@ -191,6 +193,18 @@ void ClangFormatSettings::setPredefinedStyle(const QString &predefinedStyle) m_settings.insert(PREDEFINED_STYLE, QVariant(predefinedStyle)); } +QString ClangFormatSettings::fallbackStyle() const +{ + return m_settings.value(FALLBACK_STYLE).toString(); +} + +void ClangFormatSettings::setFallbackStyle(const QString &fallbackStyle) +{ + const QStringList test = fallbackStyles(); + if (test.contains(fallbackStyle)) + m_settings.insert(FALLBACK_STYLE, QVariant(fallbackStyle)); +} + QString ClangFormatSettings::customStyle() const { return m_settings.value(CUSTOM_STYLE).toString(); @@ -216,6 +230,11 @@ QStringList ClangFormatSettings::predefinedStyles() const return {"LLVM", "Google", "Chromium", "Mozilla", "WebKit", "File"}; } +QStringList ClangFormatSettings::fallbackStyles() const +{ + return {"Default", "None", "LLVM", "Google", "Chromium", "Mozilla", "WebKit"}; +} + QString ClangFormatSettings::styleFileName(const QString &key) const { return m_styleDir.absolutePath() + '/' + key + '/' + m_ending; diff --git a/src/plugins/beautifier/clangformat/clangformatsettings.h b/src/plugins/beautifier/clangformat/clangformatsettings.h index 287fdaea664..b8a62be6bdc 100644 --- a/src/plugins/beautifier/clangformat/clangformatsettings.h +++ b/src/plugins/beautifier/clangformat/clangformatsettings.h @@ -48,6 +48,9 @@ public: QString predefinedStyle() const; void setPredefinedStyle(const QString &predefinedStyle); + QString fallbackStyle() const; + void setFallbackStyle(const QString &fallbackStyle); + QString customStyle() const; void setCustomStyle(const QString &customStyle); @@ -55,6 +58,7 @@ public: void setFormatEntireFileFallback(bool formatEntireFileFallback); QStringList predefinedStyles() const; + QStringList fallbackStyles() const; QString styleFileName(const QString &key) const override;