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>
This commit is contained in:
Przemyslaw Gorszkowski
2017-03-01 20:40:27 +01:00
parent 666648e997
commit 163b2c027d
6 changed files with 142 additions and 89 deletions

View File

@@ -137,6 +137,11 @@
same directory as the source file or in one of its parent same directory as the source file or in one of its parent
directories. 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 \li Select the \uicontrol {Use customized style} option, and
then \uicontrol Add to define your own style. then \uicontrol Add to define your own style.

View File

@@ -137,7 +137,14 @@ Command ClangFormat::command() const
command.setProcessing(Command::PipeProcessing); command.setProcessing(Command::PipeProcessing);
if (m_settings->usePredefinedStyle()) { 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"); command.addOption("-assume-filename=%file");
} else { } else {
command.addOption("-style=file"); command.addOption("-style=file");

View File

@@ -47,10 +47,18 @@ ClangFormatOptionsPageWidget::ClangFormatOptionsPageWidget(ClangFormatSettings *
ui->setupUi(this); ui->setupUi(this);
ui->options->setEnabled(false); ui->options->setEnabled(false);
ui->predefinedStyle->addItems(m_settings->predefinedStyles()); ui->predefinedStyle->addItems(m_settings->predefinedStyles());
ui->fallbackStyle->addItems(m_settings->fallbackStyles());
ui->command->setExpectedKind(Utils::PathChooser::ExistingCommand); ui->command->setExpectedKind(Utils::PathChooser::ExistingCommand);
ui->command->setPromptDialogTitle( ui->command->setPromptDialogTitle(
BeautifierPlugin::msgCommandPromptDialogTitle("Clang Format")); BeautifierPlugin::msgCommandPromptDialogTitle("Clang Format"));
connect(ui->command, &Utils::PathChooser::validChanged, ui->options, &QWidget::setEnabled); 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); ui->configurations->setSettings(m_settings);
} }
@@ -63,9 +71,12 @@ void ClangFormatOptionsPageWidget::restore()
{ {
ui->command->setPath(m_settings->command()); ui->command->setPath(m_settings->command());
ui->mime->setText(m_settings->supportedMimeTypesAsString()); ui->mime->setText(m_settings->supportedMimeTypesAsString());
const int textIndex = ui->predefinedStyle->findText(m_settings->predefinedStyle()); const int predefinedStyleIndex = ui->predefinedStyle->findText(m_settings->predefinedStyle());
if (textIndex != -1) if (predefinedStyleIndex != -1)
ui->predefinedStyle->setCurrentIndex(textIndex); 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->formatEntireFileFallback->setChecked(m_settings->formatEntireFileFallback());
ui->configurations->setSettings(m_settings); ui->configurations->setSettings(m_settings);
ui->configurations->setCurrentConfiguration(m_settings->customStyle()); ui->configurations->setCurrentConfiguration(m_settings->customStyle());
@@ -82,6 +93,7 @@ void ClangFormatOptionsPageWidget::apply()
m_settings->setSupportedMimeTypes(ui->mime->text()); m_settings->setSupportedMimeTypes(ui->mime->text());
m_settings->setUsePredefinedStyle(ui->usePredefinedStyle->isChecked()); m_settings->setUsePredefinedStyle(ui->usePredefinedStyle->isChecked());
m_settings->setPredefinedStyle(ui->predefinedStyle->currentText()); m_settings->setPredefinedStyle(ui->predefinedStyle->currentText());
m_settings->setFallbackStyle(ui->fallbackStyle->currentText());
m_settings->setCustomStyle(ui->configurations->currentConfiguration()); m_settings->setCustomStyle(ui->configurations->currentConfiguration());
m_settings->setFormatEntireFileFallback(ui->formatEntireFileFallback->isChecked()); m_settings->setFormatEntireFileFallback(ui->formatEntireFileFallback->isChecked());
m_settings->save(); m_settings->save();

View File

@@ -6,15 +6,102 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>817</width> <width>727</width>
<height>631</height> <height>631</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Form</string> <string>Form</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QGridLayout" name="gridLayout_2">
<item> <item row="1" column="0" colspan="2">
<widget class="QGroupBox" name="options">
<property name="title">
<string>Options</string>
</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>
<property name="checked">
<bool>true</bool>
</property>
<property name="autoExclusive">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<widget class="QComboBox" name="predefinedStyle"/>
</item>
<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>
</property>
<property name="autoExclusive">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<widget class="Beautifier::Internal::ConfigurationPanel" name="configurations" native="true"/>
</item>
<item row="3" column="0" colspan="2">
<widget class="QCheckBox" name="formatEntireFileFallback">
<property name="toolTip">
<string>For action Format Selected Text.</string>
</property>
<property name="text">
<string>Format entire file if no text was selected</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0" colspan="2">
<widget class="QGroupBox" name="configuration"> <widget class="QGroupBox" name="configuration">
<property name="title"> <property name="title">
<string>Configuration</string> <string>Configuration</string>
@@ -43,70 +130,6 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item>
<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>
<item row="0" column="0">
<widget class="QRadioButton" name="usePredefinedStyle">
<property name="text">
<string>Use predefined style:</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="autoExclusive">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="predefinedStyle"/>
</item>
<item row="1" column="0">
<widget class="QRadioButton" name="useCustomizedStyle">
<property name="text">
<string>Use customized style:</string>
</property>
<property name="autoExclusive">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="Beautifier::Internal::ConfigurationPanel" name="configurations" native="true"/>
</item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="formatEntireFileFallback">
<property name="toolTip">
<string>For action Format Selected Text</string>
</property>
<property name="text">
<string>Format entire file if no text was selected</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout> </layout>
</widget> </widget>
<customwidgets> <customwidgets>
@@ -124,22 +147,5 @@
</customwidget> </customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections> <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>
</ui> </ui>

View File

@@ -41,6 +41,7 @@ namespace ClangFormat {
namespace { namespace {
const char USE_PREDEFINED_STYLE[] = "usePredefinedStyle"; const char USE_PREDEFINED_STYLE[] = "usePredefinedStyle";
const char PREDEFINED_STYLE[] = "predefinedStyle"; const char PREDEFINED_STYLE[] = "predefinedStyle";
const char FALLBACK_STYLE[] = "fallbackStyle";
const char CUSTOM_STYLE[] = "customStyle"; const char CUSTOM_STYLE[] = "customStyle";
const char FORMAT_ENTIRE_FILE_FALLBACK[] = "formatEntireFileFallback"; const char FORMAT_ENTIRE_FILE_FALLBACK[] = "formatEntireFileFallback";
} }
@@ -51,6 +52,7 @@ ClangFormatSettings::ClangFormatSettings() :
setCommand("clang-format"); setCommand("clang-format");
m_settings.insert(USE_PREDEFINED_STYLE, QVariant(true)); m_settings.insert(USE_PREDEFINED_STYLE, QVariant(true));
m_settings.insert(PREDEFINED_STYLE, "LLVM"); m_settings.insert(PREDEFINED_STYLE, "LLVM");
m_settings.insert(FALLBACK_STYLE, "Default");
m_settings.insert(CUSTOM_STYLE, QVariant()); m_settings.insert(CUSTOM_STYLE, QVariant());
m_settings.insert(FORMAT_ENTIRE_FILE_FALLBACK, QVariant(true)); m_settings.insert(FORMAT_ENTIRE_FILE_FALLBACK, QVariant(true));
read(); read();
@@ -191,6 +193,18 @@ void ClangFormatSettings::setPredefinedStyle(const QString &predefinedStyle)
m_settings.insert(PREDEFINED_STYLE, QVariant(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 QString ClangFormatSettings::customStyle() const
{ {
return m_settings.value(CUSTOM_STYLE).toString(); return m_settings.value(CUSTOM_STYLE).toString();
@@ -216,6 +230,11 @@ QStringList ClangFormatSettings::predefinedStyles() const
return {"LLVM", "Google", "Chromium", "Mozilla", "WebKit", "File"}; 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 QString ClangFormatSettings::styleFileName(const QString &key) const
{ {
return m_styleDir.absolutePath() + '/' + key + '/' + m_ending; return m_styleDir.absolutePath() + '/' + key + '/' + m_ending;

View File

@@ -48,6 +48,9 @@ public:
QString predefinedStyle() const; QString predefinedStyle() const;
void setPredefinedStyle(const QString &predefinedStyle); void setPredefinedStyle(const QString &predefinedStyle);
QString fallbackStyle() const;
void setFallbackStyle(const QString &fallbackStyle);
QString customStyle() const; QString customStyle() const;
void setCustomStyle(const QString &customStyle); void setCustomStyle(const QString &customStyle);
@@ -55,6 +58,7 @@ public:
void setFormatEntireFileFallback(bool formatEntireFileFallback); void setFormatEntireFileFallback(bool formatEntireFileFallback);
QStringList predefinedStyles() const; QStringList predefinedStyles() const;
QStringList fallbackStyles() const;
QString styleFileName(const QString &key) const override; QString styleFileName(const QString &key) const override;