diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp index 438da7697c702ed97ae6b686b95ac1a14710f505..39acd23ba333cfd11a0c177216cf39f459c5cf27 100644 --- a/src/plugins/cppeditor/cppeditor.cpp +++ b/src/plugins/cppeditor/cppeditor.cpp @@ -1669,6 +1669,7 @@ static void indentCPPBlock(const CPPEditor::TabSettings &ts, indenter.setIndentSize(ts.m_indentSize); indenter.setTabSize(ts.m_tabSize); indenter.setIndentBraces(ts.m_indentBraces); + indenter.setDoubleIndentBlocks(ts.m_doubleIndentBlocks); 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 f49abc713d23a073c5f5e1293f5d0164e3b46d80..a753ac7a88ab57f4b9191bed3fb8f25ecc41e7f7 100644 --- a/src/plugins/texteditor/behaviorsettingspage.cpp +++ b/src/plugins/texteditor/behaviorsettingspage.cpp @@ -170,6 +170,8 @@ void BehaviorSettingsPage::settingsFromUI(TabSettings &tabSettings, 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_doubleIndentBlocks = m_d->m_page.doubleIndentBlocks->isChecked(); + tabSettings.m_tabKeyBehavior = (TabSettings::TabKeyBehavior)m_d->m_page.tabKeyBehavior->currentIndex(); storageSettings.m_cleanWhitespace = m_d->m_page.cleanWhitespace->isChecked(); @@ -191,6 +193,7 @@ void BehaviorSettingsPage::settingsToUI() 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.doubleIndentBlocks->setChecked(tabSettings.m_doubleIndentBlocks); 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 349d272a949bc036155a158067d376986ff483e3..a785188c29972610be10664d7bbca19ecec65774 100644 --- a/src/plugins/texteditor/behaviorsettingspage.ui +++ b/src/plugins/texteditor/behaviorsettingspage.ui @@ -176,6 +176,16 @@ </property> </widget> </item> + <item row="5" column="0"> + <widget class="QCheckBox" name="doubleIndentBlocks"> + <property name="enabled"> + <bool>true</bool> + </property> + <property name="text"> + <string>Indent blocks t&wice</string> + </property> + </widget> + </item> </layout> </item> <item> diff --git a/src/plugins/texteditor/tabsettings.cpp b/src/plugins/texteditor/tabsettings.cpp index 1f0874eb3f0fe284063b6b33cc6ae9bf77dba626..20e80446634bf934333d798595fc1b20bc24acd1 100644 --- a/src/plugins/texteditor/tabsettings.cpp +++ b/src/plugins/texteditor/tabsettings.cpp @@ -42,6 +42,7 @@ static const char *autoIndentKey = "AutoIndent"; static const char *tabSizeKey = "TabSize"; static const char *indentSizeKey = "IndentSize"; static const char *indentBracesKey = "IndentBraces"; +static const char *doubleIndentBlocksKey = "DoubleIndentBlocks"; static const char *tabKeyBehaviorKey = "TabKeyBehavior"; static const char *groupPostfix = "TabSettings"; @@ -55,6 +56,7 @@ TabSettings::TabSettings() : m_tabSize(8), m_indentSize(4), m_indentBraces(false), + m_doubleIndentBlocks(false), m_tabKeyBehavior(TabNeverIndents) { } @@ -72,6 +74,7 @@ void TabSettings::toSettings(const QString &category, QSettings *s) const s->setValue(QLatin1String(tabSizeKey), m_tabSize); s->setValue(QLatin1String(indentSizeKey), m_indentSize); s->setValue(QLatin1String(indentBracesKey), m_indentBraces); + s->setValue(QLatin1String(doubleIndentBlocksKey), m_doubleIndentBlocks); s->setValue(QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior); s->endGroup(); } @@ -92,6 +95,9 @@ void TabSettings::fromSettings(const QString &category, const QSettings *s) 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_doubleIndentBlocks + = s->value(group + QLatin1String(doubleIndentBlocksKey), m_doubleIndentBlocks).toBool(); + m_tabKeyBehavior = (TabKeyBehavior)s->value(group + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior).toInt(); } @@ -339,6 +345,7 @@ bool TabSettings::equals(const TabSettings &ts) const && m_tabSize == ts.m_tabSize && m_indentSize == ts.m_indentSize && m_indentBraces == ts.m_indentBraces + && m_doubleIndentBlocks == ts.m_doubleIndentBlocks && m_tabKeyBehavior == ts.m_tabKeyBehavior; } diff --git a/src/plugins/texteditor/tabsettings.h b/src/plugins/texteditor/tabsettings.h index 135acd7fa2f20144e5c18ea342db129d94569c75..59ae3d4c009355e1ea6d7e74c989d1bc43591291 100644 --- a/src/plugins/texteditor/tabsettings.h +++ b/src/plugins/texteditor/tabsettings.h @@ -83,6 +83,7 @@ struct TEXTEDITOR_EXPORT TabSettings int m_tabSize; int m_indentSize; bool m_indentBraces; + bool m_doubleIndentBlocks; TabKeyBehavior m_tabKeyBehavior; bool equals(const TabSettings &ts) const; diff --git a/src/shared/indenter/indenter.h b/src/shared/indenter/indenter.h index f6e78f6a9f76883eed459e6af202611ff0337ab3..f5091038ea5b23b8e96ccf768c375a37664c12c3 100644 --- a/src/shared/indenter/indenter.h +++ b/src/shared/indenter/indenter.h @@ -94,7 +94,7 @@ public: void setIndentSize(int size); void setTabSize(int size ); void setIndentBraces(bool indent); - + void setDoubleIndentBlocks(bool indent); /* Return indentation for the last line of the sequence * based on the previous lines. */ int indentForBottomLine(const Iterator ¤t, @@ -126,6 +126,7 @@ private: int ppIndentSize; bool ppIndentBraces; int ppContinuationIndentSize; + bool ppDoubleIndentBlocks; Iterator yyProgramBegin; Iterator yyProgramEnd; diff --git a/src/shared/indenter/indenter_impl.h b/src/shared/indenter/indenter_impl.h index 7de7aeb3dec148c4eee72a3283894b2e9587ab7b..de2cbe452f052db7fb53a3bb9b54dfd48c87ece9 100644 --- a/src/shared/indenter/indenter_impl.h +++ b/src/shared/indenter/indenter_impl.h @@ -97,6 +97,8 @@ namespace { when it cannot be picked up. * ppIndentBraces will indent braces flush with an indented code block. + * ppDoubleIndentBlocks do double indent of blocks so as together + with ppIndentBraces enabled it form some sort of GNU indenting style */ @@ -109,6 +111,7 @@ Indenter<Iterator>::Indenter() : ppIndentSize(4), ppIndentBraces(false), ppContinuationIndentSize(8), + ppDoubleIndentBlocks(false), yyLinizerState(new LinizerState), yyLine(0), yyBraceDepth(0), @@ -147,6 +150,13 @@ void Indenter<Iterator>::setIndentBraces(bool indent) { ppIndentBraces = indent; } + +template <class Iterator> +void Indenter<Iterator>::setDoubleIndentBlocks(bool indent) +{ + ppDoubleIndentBlocks = indent; +} + /* Returns the first non-space character in the string t, or QChar::null if the string is made only of white space. @@ -846,8 +856,8 @@ int Indenter<Iterator>::indentForContinuationLine() The "{" should be flush left. */ if ( !isContinuationLine() ) - return indentOfLine( *yyLine ); - } else if ( isContinuationLine() || yyLine->endsWith(comma)) { + return indentOfLine( *yyLine ); + } else if ( isContinuationLine() || yyLine->endsWith(comma)) { /* We have @@ -1012,8 +1022,15 @@ int Indenter<Iterator>::indentForStandaloneLine() Never trust lines containing only '{' or '}', as some people (Richard M. Stallman) format them weirdly. */ - if ( yyLine->trimmed().length() > 1 ) - return indentOfLine( *yyLine ) - *yyBraceDepth * ppIndentSize; + if ( yyLine->trimmed().length() > 1 ) { + if (!ppDoubleIndentBlocks) + return indentOfLine( *yyLine ) - *yyBraceDepth * ppIndentSize; + else { + if (*yyBraceDepth == -1 && indentOfLine( *yyLine ) == 0) + return ppIndentSize; // don't do double indent for upper level blocks + return indentOfLine( *yyLine ) - *yyBraceDepth * ppIndentSize * 2; + } + } } if ( !readLine() ) @@ -1082,12 +1099,23 @@ int Indenter<Iterator>::indentForBottomLine(const Iterator ¤t, if ( ppIndentBraces && firstCh == openingBrace ) { indent += ppIndentSize; - } else if ( !ppIndentBraces && firstCh == closingBrace ) { + } else if ( firstCh == closingBrace ) { /* A closing brace is one level more to the left than the code it follows. - */ + */ indent -= ppIndentSize; + /* + But if braces indenting is enabled the shift exists + */ + if (ppIndentBraces) + indent += ppIndentSize; + /* + Double indenting of code blocks makes block righter, so move our brace back + */ + if (ppDoubleIndentBlocks) + indent -= ppIndentSize; + } else if ( okay(typedIn, colon) ) { if ( m_constants.m_caseLabel.indexIn(bottomLine) != -1 ) { /*