diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp index a48dde53ea6bb578cd7573d04ab141ee39fc5915..86976b03400f795c965731a6307c3e5f8f033b4a 100644 --- a/src/plugins/cppeditor/cppeditor.cpp +++ b/src/plugins/cppeditor/cppeditor.cpp @@ -1573,6 +1573,7 @@ static void indentCPPBlock(const CPPEditor::TabSettings &ts, Indenter &indenter = Indenter::instance(); indenter.setIndentSize(ts.m_indentSize); indenter.setTabSize(ts.m_tabSize); + indenter.setIndentBraces(ts.m_indentBraces); const TextEditor::TextBlockIterator current(block); const int indent = indenter.indentForBottomLine(current, programBegin, programEnd, typedChar); diff --git a/src/plugins/texteditor/behaviorsettingspage.cpp b/src/plugins/texteditor/behaviorsettingspage.cpp index 94d24b059799250b51365123de0621712715719e..563c0b5a4c32137e8b1ee2caf2da8adee34f9d98 100644 --- a/src/plugins/texteditor/behaviorsettingspage.cpp +++ b/src/plugins/texteditor/behaviorsettingspage.cpp @@ -163,6 +163,7 @@ void BehaviorSettingsPage::settingsFromUI(TabSettings &tabSettings, tabSettings.m_smartBackspace = m_d->m_page.smartBackspace->isChecked(); tabSettings.m_tabSize = m_d->m_page.tabSize->value(); tabSettings.m_indentSize = m_d->m_page.indentSize->value(); + tabSettings.m_indentBraces = m_d->m_page.indentBraces->isChecked(); tabSettings.m_tabKeyBehavior = (TabSettings::TabKeyBehavior)m_d->m_page.tabKeyBehavior->currentIndex(); storageSettings.m_cleanWhitespace = m_d->m_page.cleanWhitespace->isChecked(); @@ -182,6 +183,7 @@ void BehaviorSettingsPage::settingsToUI() m_d->m_page.smartBackspace->setChecked(tabSettings.m_smartBackspace); m_d->m_page.tabSize->setValue(tabSettings.m_tabSize); m_d->m_page.indentSize->setValue(tabSettings.m_indentSize); + m_d->m_page.indentBraces->setChecked(tabSettings.m_indentBraces); m_d->m_page.tabKeyBehavior->setCurrentIndex(tabSettings.m_tabKeyBehavior); const StorageSettings &storageSettings = m_d->m_storageSettings; diff --git a/src/plugins/texteditor/behaviorsettingspage.ui b/src/plugins/texteditor/behaviorsettingspage.ui index 52bd0970c2a6bf305a78742df82e4d70da9145e1..31258741672995599532d2af318f5f4c9188e881 100644 --- a/src/plugins/texteditor/behaviorsettingspage.ui +++ b/src/plugins/texteditor/behaviorsettingspage.ui @@ -6,7 +6,7 @@ <rect> <x>0</x> <y>0</y> - <width>463</width> + <width>615</width> <height>421</height> </rect> </property> @@ -107,7 +107,7 @@ </property> </widget> </item> - <item row="0" column="1" rowspan="3"> + <item row="0" column="1" rowspan="4"> <spacer name="horizontalSpacer"> <property name="orientation"> <enum>Qt::Horizontal</enum> @@ -120,7 +120,7 @@ </property> </spacer> </item> - <item row="0" column="4" rowspan="3"> + <item row="0" column="4" rowspan="4"> <spacer name="horizontalSpacer_3"> <property name="orientation"> <enum>Qt::Horizontal</enum> @@ -133,6 +133,16 @@ </property> </spacer> </item> + <item row="3" column="0"> + <widget class="QCheckBox" name="indentBraces"> + <property name="toolTip"> + <string>Braces will be flush with indented block, except first indent level.</string> + </property> + <property name="text"> + <string>Indent brace&s</string> + </property> + </widget> + </item> </layout> </item> <item> diff --git a/src/plugins/texteditor/tabsettings.cpp b/src/plugins/texteditor/tabsettings.cpp index daa84307667d89fc2df5d3dbf7380177edfbd994..c15c8bceee89562ea83ff7ac3410649a8d2ff356 100644 --- a/src/plugins/texteditor/tabsettings.cpp +++ b/src/plugins/texteditor/tabsettings.cpp @@ -40,6 +40,7 @@ static const char *smartBackspaceKey = "SmartBackspace"; static const char *autoIndentKey = "AutoIndent"; static const char *tabSizeKey = "TabSize"; static const char *indentSizeKey = "IndentSize"; +static const char *indentBracesKey = "IndentBraces"; static const char *tabKeyBehaviorKey = "TabKeyBehavior"; static const char *groupPostfix = "TabSettings"; @@ -51,6 +52,7 @@ TabSettings::TabSettings() : m_smartBackspace(false), m_tabSize(8), m_indentSize(4), + m_indentBraces(false), m_tabKeyBehavior(TabNeverIndents) { } @@ -66,6 +68,7 @@ void TabSettings::toSettings(const QString &category, QSettings *s) const s->setValue(QLatin1String(smartBackspaceKey), m_smartBackspace); s->setValue(QLatin1String(tabSizeKey), m_tabSize); s->setValue(QLatin1String(indentSizeKey), m_indentSize); + s->setValue(QLatin1String(indentBracesKey), m_indentBraces); s->setValue(QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior); s->endGroup(); } @@ -84,6 +87,7 @@ void TabSettings::fromSettings(const QString &category, const QSettings *s) m_smartBackspace = s->value(group + QLatin1String(smartBackspaceKey), m_smartBackspace).toBool(); m_tabSize = s->value(group + QLatin1String(tabSizeKey), m_tabSize).toInt(); m_indentSize = s->value(group + QLatin1String(indentSizeKey), m_indentSize).toInt(); + m_indentBraces = s->value(group + QLatin1String(indentBracesKey), m_indentBraces).toBool(); m_tabKeyBehavior = (TabKeyBehavior)s->value(group + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior).toInt(); } @@ -296,6 +300,7 @@ bool TabSettings::equals(const TabSettings &ts) const && m_smartBackspace == ts.m_smartBackspace && m_tabSize == ts.m_tabSize && m_indentSize == ts.m_indentSize + && m_indentBraces == ts.m_indentBraces && m_tabKeyBehavior == ts.m_tabKeyBehavior; } diff --git a/src/plugins/texteditor/tabsettings.h b/src/plugins/texteditor/tabsettings.h index ad6c1cd6d851996d306ce042a0cb201784bd9ff9..4f2b4785a681c9ddc325cc24190a18875155bb07 100644 --- a/src/plugins/texteditor/tabsettings.h +++ b/src/plugins/texteditor/tabsettings.h @@ -80,6 +80,7 @@ struct TEXTEDITOR_EXPORT TabSettings bool m_smartBackspace; int m_tabSize; int m_indentSize; + bool m_indentBraces; TabKeyBehavior m_tabKeyBehavior; bool equals(const TabSettings &ts) const; diff --git a/src/shared/indenter/indenter.h b/src/shared/indenter/indenter.h index 2ce3dca669c8de3ffafd672a2cb32fde094da1ad..cacb550e56921483019482e99f4267df40fb0edb 100644 --- a/src/shared/indenter/indenter.h +++ b/src/shared/indenter/indenter.h @@ -93,6 +93,7 @@ public: void setIndentSize(int size); void setTabSize(int size ); + void setIndentBraces(bool indent); /* Return indentation for the last line of the sequence * based on the previous lines. */ @@ -123,6 +124,7 @@ private: IndenterInternal::Constants m_constants; int ppHardwareTabSize; int ppIndentSize; + bool ppIndentBraces; int ppContinuationIndentSize; Iterator yyProgramBegin; diff --git a/src/shared/indenter/indenter_impl.h b/src/shared/indenter/indenter_impl.h index 728d4b609df2d86e263802e0b73ad630d9a7dc5f..0131f25ec6b75979103356066c756f4417311cad 100644 --- a/src/shared/indenter/indenter_impl.h +++ b/src/shared/indenter/indenter_impl.h @@ -95,6 +95,8 @@ namespace { line. * ppCommentOffset is the indentation within a C-style comment, when it cannot be picked up. + * ppIndentBraces will indent braces flush with an indented code + block. */ @@ -105,6 +107,7 @@ template <class Iterator> Indenter<Iterator>::Indenter() : ppHardwareTabSize(8), ppIndentSize(4), + ppIndentBraces(false), ppContinuationIndentSize(8), yyLinizerState(new LinizerState), yyLine(0), @@ -138,6 +141,12 @@ void Indenter<Iterator>::setTabSize(int size ) { ppHardwareTabSize = size; } + +template <class Iterator> +void Indenter<Iterator>::setIndentBraces(bool indent) +{ + ppIndentBraces = indent; +} /* Returns the first non-space character in the string t, or QChar::null if the string is made only of white space. @@ -1044,6 +1053,7 @@ int Indenter<Iterator>::indentForBottomLine(const Iterator ¤t, int indent; const QChar hash = QLatin1Char('#'); + const QChar openingBrace = QLatin1Char('{'); const QChar closingBrace = QLatin1Char('}'); const QChar colon = QLatin1Char(':'); @@ -1070,7 +1080,9 @@ int Indenter<Iterator>::indentForBottomLine(const Iterator ¤t, indent = indentForStandaloneLine(); } - if ( firstCh == closingBrace ) { + if ( ppIndentBraces && firstCh == openingBrace ) { + indent += ppIndentSize; + } else if ( !ppIndentBraces && firstCh == closingBrace ) { /* A closing brace is one level more to the left than the code it follows. @@ -1098,6 +1110,10 @@ int Indenter<Iterator>::indentForBottomLine(const Iterator ¤t, } } } + if ( ppIndentBraces && indent == ppIndentSize && + (firstCh == openingBrace || firstCh == closingBrace ) ) { + indent = 0; + } return qMax( 0, indent ); }