Commit 2862a824 authored by Dominic Tubach's avatar Dominic Tubach Committed by Milian Wolff
Browse files

Astyle formatter: add support for padding parenthesis headers

The patch adds an option to enable/disable padding parenthesis headers
in the astyle code formatter.

For improved Qt compatibility, "foreach" is added to the list of headers
and it is assured that "&" and "*" are not treated as operators at this point.

The KDELibs and Qt styles are fixed to properly use this new feature.

BUG: 302593
REVIEW: 106032
parent 41a13a62
......@@ -130,6 +130,7 @@ void AStyleFormatter::updateFormatter()
AStyleFormatter::setOperatorPaddingMode(m_options["PadOperators"].toBool());
AStyleFormatter::setParensInsidePaddingMode(m_options["PadParenthesesIn"].toBool());
AStyleFormatter::setParensOutsidePaddingMode(m_options["PadParenthesesOut"].toBool());
AStyleFormatter::setParensHeaderPaddingMode(m_options["PadParenthesesHeader"].toBool());
AStyleFormatter::setParensUnPaddingMode(m_options["PadParenthesesUn"].toBool());
// oneliner
......@@ -176,6 +177,7 @@ void AStyleFormatter::resetStyle()
setOperatorPaddingMode(false);
setParensInsidePaddingMode(true);
setParensOutsidePaddingMode(true);
setParensHeaderPaddingMode(true);
setParensUnPaddingMode(true);
}
......@@ -289,6 +291,7 @@ bool AStyleFormatter::predefinedStyle( const QString & style )
setNamespaceIndent(false);
return true;
} else if (style == "KDELibs") {
// http://techbase.kde.org/Policies/Kdelibs_Coding_Style
resetStyle();
setSpaceIndentation(4);
setBracketFormatMode(astyle::LINUX_MODE);
......@@ -296,7 +299,8 @@ bool AStyleFormatter::predefinedStyle( const QString & style )
setLabelIndent(true);
setOperatorPaddingMode(true);
setParensInsidePaddingMode(false);
setParensOutsidePaddingMode(true);
setParensOutsidePaddingMode(false);
setParensHeaderPaddingMode(true);
setParensUnPaddingMode(true);
setSingleStatementsMode(false);
setTabSpaceConversionMode(true);
......@@ -306,6 +310,7 @@ bool AStyleFormatter::predefinedStyle( const QString & style )
setNamespaceIndent(false);
return true;
} else if (style == "Qt") {
// http://qt-project.org/wiki/Qt_Coding_Style
resetStyle();
setPointerAlignment(astyle::ALIGN_NAME);
setOperatorPaddingMode(true);
......@@ -313,6 +318,7 @@ bool AStyleFormatter::predefinedStyle( const QString & style )
setSwitchIndent(false);
setParensInsidePaddingMode(false);
setParensOutsidePaddingMode(false);
setParensHeaderPaddingMode(true);
setParensUnPaddingMode(true);
setSpaceIndentation(4);
setClassIndent(false);
......@@ -529,6 +535,11 @@ void AStyleFormatter::setParensInsidePaddingMode(bool mode)
ASFormatter::setParensInsidePaddingMode(mode);
}
void AStyleFormatter::setParensHeaderPaddingMode(bool mode) {
m_options["PadParenthesesHeader"] = mode;
ASFormatter::setParensHeaderPaddingMode(mode);
}
void AStyleFormatter::setParensUnPaddingMode(bool state)
{
m_options["PadParenthesesUn"] = state;
......
......@@ -80,6 +80,7 @@ class AStyleFormatter : public astyle::ASFormatter
void setOperatorPaddingMode(bool mode);
void setParensOutsidePaddingMode(bool mode);
void setParensInsidePaddingMode(bool mode);
void setParensHeaderPaddingMode(bool mode);
void setParensUnPaddingMode(bool state);
//oneliners
void setBreakOneLineBlocksMode(bool state);
......
......@@ -75,6 +75,7 @@ AStylePreferences::AStylePreferences(Language lang, QWidget *parent)
AStylePreferences::~AStylePreferences( )
{
delete m_formatter;
}
void AStylePreferences::init()
......@@ -107,6 +108,7 @@ void AStylePreferences::init()
connect(chkBlockIfElse, SIGNAL(stateChanged(int)), this, SLOT(blocksChanged()));
connect(cbParenthesisPadding, SIGNAL(currentIndexChanged(int)), this, SLOT(paddingChanged()));
connect(chkPadParenthesisHeader, SIGNAL(stateChanged(int)), this, SLOT(paddingChanged()));
connect(chkPadOperators, SIGNAL(stateChanged(int)), this, SLOT(paddingChanged()));
connect(chkKeepStatements, SIGNAL(stateChanged(int)), this, SLOT(onelinersChanged()));
......@@ -203,6 +205,11 @@ void AStylePreferences::updateWidgets()
} else
cbParenthesisPadding->setCurrentIndex(PADDING_NOCHANGE);
// padding header has no influence with padding out
if (padout)
chkPadParenthesisHeader->setDisabled(true);
chkPadParenthesisHeader->setChecked(m_formatter->option("PadParenthesesHeader").toBool());
chkPadOperators->setChecked(m_formatter->option("PadOperators").toBool());
// oneliner
chkKeepStatements->setChecked(m_formatter->option("KeepStatements").toBool());
......@@ -349,29 +356,37 @@ void AStylePreferences::paddingChanged()
m_formatter->setParensUnPaddingMode(false);
m_formatter->setParensInsidePaddingMode(false);
m_formatter->setParensOutsidePaddingMode(false);
chkPadParenthesisHeader->setDisabled(false);
break;
case PADDING_NO:
m_formatter->setParensUnPaddingMode(true);
m_formatter->setParensInsidePaddingMode(false);
m_formatter->setParensOutsidePaddingMode(false);
chkPadParenthesisHeader->setDisabled(false);
break;
case PADDING_IN:
m_formatter->setParensUnPaddingMode(true);
m_formatter->setParensInsidePaddingMode(true);
m_formatter->setParensOutsidePaddingMode(false);
chkPadParenthesisHeader->setDisabled(false);
break;
case PADDING_OUT:
m_formatter->setParensUnPaddingMode(true);
m_formatter->setParensInsidePaddingMode(false);
m_formatter->setParensOutsidePaddingMode(true);
// padding header has no influence with padding out
chkPadParenthesisHeader->setDisabled(true);
break;
case PADDING_INOUT:
m_formatter->setParensUnPaddingMode(true);
m_formatter->setParensInsidePaddingMode(true);
m_formatter->setParensOutsidePaddingMode(true);
// padding header has no influence with padding out
chkPadParenthesisHeader->setDisabled(true);
break;
}
m_formatter->setParensHeaderPaddingMode(chkPadParenthesisHeader->isChecked());
m_formatter->setOperatorPaddingMode(chkPadOperators->isChecked());
updatePreviewText();
......
......@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>332</width>
<height>499</height>
<height>519</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
......@@ -413,16 +413,6 @@ correctly.
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<layout class="QGridLayout" name="gridLayout_6">
<item row="0" column="0">
<widget class="QLabel" name="lblParenthesis">
<property name="text">
<string>Pad parenthesis</string>
</property>
<property name="margin">
<number>6</number>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="KComboBox" name="cbParenthesisPadding">
<property name="sizePolicy">
......@@ -458,7 +448,7 @@ correctly.
</item>
</widget>
</item>
<item row="1" column="0" colspan="2">
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="chkPadOperators">
<property name="toolTip">
<string>Insert space padding around operators.
......@@ -470,6 +460,26 @@ There is no unpad operator option.</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="lblParenthesis">
<property name="text">
<string>Pad parenthesis</string>
</property>
<property name="margin">
<number>6</number>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QCheckBox" name="chkPadParenthesisHeader">
<property name="toolTip">
<string>Insert space padding after parenthesis headers (e.g. 'if', 'for', 'while', ...)</string>
</property>
<property name="text">
<string>Add spaces after parenthesis &amp;headers</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
......@@ -565,16 +575,16 @@ multiple single-statement lines.</string>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>KComboBox</class>
<extends>QComboBox</extends>
<header>kcombobox.h</header>
</customwidget>
<customwidget>
<class>KListWidget</class>
<extends>QListWidget</extends>
<header>klistwidget.h</header>
</customwidget>
<customwidget>
<class>KComboBox</class>
<extends>QComboBox</extends>
<header>kcombobox.h</header>
</customwidget>
<customwidget>
<class>KTabWidget</class>
<extends>QTabWidget</extends>
......
......@@ -2057,7 +2057,9 @@ bool ASFormatter::isPointerOrReference() const
|| previousNonWSChar == '('
|| previousNonWSChar == '['
|| currentHeader == &AS_CATCH
|| isCharImmediatelyPostReturn)
|| isCharImmediatelyPostReturn
// see: https://git.reviewboard.kde.org/r/106032
|| currentHeader == &AS_FOREACH)
return true;
// get the last legal word (may be a number)
......
......@@ -233,6 +233,12 @@ void ASResource::buildHeaders(vector<const string*>* headers, int fileType, bool
headers->push_back(&AS_TRY);
headers->push_back(&AS_CATCH);
if (fileType == C_TYPE)
{
// see: https://git.reviewboard.kde.org/r/106032
headers->push_back(&AS_FOREACH);
}
if (fileType == JAVA_TYPE)
{
headers->push_back(&AS_FINALLY);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment