From fa5f9246c2d7922e8e294b64d4417c6a983d4c56 Mon Sep 17 00:00:00 2001
From: Jeffrey Baumes <jeff.baumes@kitware.com>
Date: Tue, 27 Oct 2009 13:34:48 -0400
Subject: [PATCH] Adding indented braces option.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Merge-request: 1949
Reviewed-by: Thorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com>
---
 src/plugins/cppeditor/cppeditor.cpp            |  1 +
 .../texteditor/behaviorsettingspage.cpp        |  2 ++
 src/plugins/texteditor/behaviorsettingspage.ui | 16 +++++++++++++---
 src/plugins/texteditor/tabsettings.cpp         |  5 +++++
 src/plugins/texteditor/tabsettings.h           |  1 +
 src/shared/indenter/indenter.h                 |  2 ++
 src/shared/indenter/indenter_impl.h            | 18 +++++++++++++++++-
 7 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp
index a48dde53ea6..86976b03400 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 94d24b05979..563c0b5a4c3 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 52bd0970c2a..31258741672 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&amp;s</string>
+          </property>
+         </widget>
+        </item>
        </layout>
       </item>
       <item>
diff --git a/src/plugins/texteditor/tabsettings.cpp b/src/plugins/texteditor/tabsettings.cpp
index daa84307667..c15c8bceee8 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 ad6c1cd6d85..4f2b4785a68 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 2ce3dca669c..cacb550e569 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 728d4b609df..0131f25ec6b 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 &current,
     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 &current,
 	    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 &current,
 	    }
 	}
     }
+    if ( ppIndentBraces && indent == ppIndentSize &&
+         (firstCh == openingBrace || firstCh == closingBrace ) ) {
+        indent = 0;
+    }
     return qMax( 0, indent );
 }
 
-- 
GitLab