diff options
author | Przemyslaw Gorszkowski <pgorszkowski@gmail.com> | 2017-03-01 20:40:27 +0100 |
---|---|---|
committer | Przemyslaw Gorszkowski <pgorszkowski@gmail.com> | 2017-03-29 08:35:24 +0000 |
commit | 163b2c027d25dfc5971f94ffcb24396fbf3dcf66 (patch) | |
tree | cc22abb3be15431129b9abf20df54420d63d11ce /src/plugins/beautifier/clangformat | |
parent | 666648e9979e11fa1416684b18f7671203275aff (diff) | |
download | qt-creator-163b2c027d25dfc5971f94ffcb24396fbf3dcf66.tar.gz |
Beautifier: added support for fallback-style option for clang-format
Task-number: QTCREATORBUG-17813
Change-Id: I36adee4889d1b851171d34852eed29639c68b574
Reviewed-by: Lorenz Haas <lorenz.haas@histomatics.de>
Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/plugins/beautifier/clangformat')
5 files changed, 112 insertions, 64 deletions
diff --git a/src/plugins/beautifier/clangformat/clangformat.cpp b/src/plugins/beautifier/clangformat/clangformat.cpp index efc7c73399..cf4abc6453 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 59f9ef5f5b..a9ed3cef25 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 91788a3146..c40b28c041 100644 --- a/src/plugins/beautifier/clangformat/clangformatoptionspage.ui +++ b/src/plugins/beautifier/clangformat/clangformatoptionspage.ui @@ -6,54 +6,28 @@ <rect> <x>0</x> <y>0</y> - <width>817</width> + <width>727</width> <height>631</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> - <layout class="QVBoxLayout" name="verticalLayout"> - <item> - <widget class="QGroupBox" name="configuration"> - <property name="title"> - <string>Configuration</string> - </property> - <layout class="QFormLayout" name="formLayout_2"> - <item row="0" column="0"> - <widget class="QLabel" name="commandLabel"> - <property name="text"> - <string>Clang Format command:</string> - </property> - </widget> - </item> - <item row="0" column="1"> - <widget class="Utils::PathChooser" name="command" native="true"/> - </item> - <item row="1" column="0"> - <widget class="QLabel" name="mimeLabel"> - <property name="text"> - <string>Restrict to MIME types:</string> - </property> - </widget> - </item> - <item row="1" column="1"> - <widget class="QLineEdit" name="mime"/> - </item> - </layout> - </widget> - </item> - <item> + <layout class="QGridLayout" name="gridLayout_2"> + <item row="1" column="0" colspan="2"> <widget class="QGroupBox" name="options"> <property name="title"> <string>Options</string> </property> - <layout class="QFormLayout" name="formLayout"> - <property name="fieldGrowthPolicy"> - <enum>QFormLayout::AllNonFixedFieldsGrow</enum> - </property> + <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> <widget class="QRadioButton" name="usePredefinedStyle"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> <property name="text"> <string>Use predefined style:</string> </property> @@ -65,10 +39,30 @@ </property> </widget> </item> - <item row="0" column="1"> + <item row="0" column="1" colspan="2"> <widget class="QComboBox" name="predefinedStyle"/> </item> - <item row="1" column="0"> + <item row="1" column="1"> + <widget class="QLabel" name="fallbackStyleLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Fixed" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>Fallback style:</string> + </property> + </widget> + </item> + <item row="1" column="2"> + <widget class="QComboBox" name="fallbackStyle"> + <property name="enabled"> + <bool>false</bool> + </property> + </widget> + </item> + <item row="2" column="0"> <widget class="QRadioButton" name="useCustomizedStyle"> <property name="text"> <string>Use customized style:</string> @@ -78,13 +72,13 @@ </property> </widget> </item> - <item row="1" column="1"> + <item row="2" column="1" colspan="2"> <widget class="Beautifier::Internal::ConfigurationPanel" name="configurations" native="true"/> </item> - <item row="2" column="0" colspan="2"> + <item row="3" column="0" colspan="2"> <widget class="QCheckBox" name="formatEntireFileFallback"> <property name="toolTip"> - <string>For action Format Selected Text</string> + <string>For action Format Selected Text.</string> </property> <property name="text"> <string>Format entire file if no text was selected</string> @@ -94,7 +88,7 @@ </layout> </widget> </item> - <item> + <item row="2" column="1"> <spacer name="verticalSpacer"> <property name="orientation"> <enum>Qt::Vertical</enum> @@ -107,6 +101,35 @@ </property> </spacer> </item> + <item row="0" column="0" colspan="2"> + <widget class="QGroupBox" name="configuration"> + <property name="title"> + <string>Configuration</string> + </property> + <layout class="QFormLayout" name="formLayout_2"> + <item row="0" column="0"> + <widget class="QLabel" name="commandLabel"> + <property name="text"> + <string>Clang Format command:</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="Utils::PathChooser" name="command" native="true"/> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="mimeLabel"> + <property name="text"> + <string>Restrict to MIME types:</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLineEdit" name="mime"/> + </item> + </layout> + </widget> + </item> </layout> </widget> <customwidgets> @@ -124,22 +147,5 @@ </customwidget> </customwidgets> <resources/> - <connections> - <connection> - <sender>usePredefinedStyle</sender> - <signal>toggled(bool)</signal> - <receiver>predefinedStyle</receiver> - <slot>setEnabled(bool)</slot> - <hints> - <hint type="sourcelabel"> - <x>67</x> - <y>108</y> - </hint> - <hint type="destinationlabel"> - <x>240</x> - <y>113</y> - </hint> - </hints> - </connection> - </connections> + <connections/> </ui> diff --git a/src/plugins/beautifier/clangformat/clangformatsettings.cpp b/src/plugins/beautifier/clangformat/clangformatsettings.cpp index d91569c3e6..f4c2189181 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 287fdaea66..b8a62be6bd 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; |