From 486b23b8ef1df404c026514b93f073163885d324 Mon Sep 17 00:00:00 2001
From: Christian Kamm <christian.d.kamm@nokia.com>
Date: Thu, 8 Jul 2010 08:48:36 +0200
Subject: [PATCH] C++ indenter: Don't add continuation indent after function
 return type.

Task-number: QTCREATORBUG-1816
---
 src/plugins/cpptools/cppcodeformatter.cpp     | 14 +++++++++++++-
 .../codeformatter/tst_codeformatter.cpp       | 19 ++++++++++++++++++-
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp
index 1c84bae4b82..ac4d94faf9e 100644
--- a/src/plugins/cpptools/cppcodeformatter.cpp
+++ b/src/plugins/cpptools/cppcodeformatter.cpp
@@ -932,7 +932,6 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
     case statement_with_condition:
     case for_statement:
     case switch_statement:
-    case declaration_start:
     case if_statement:
     case return_statement:
         if (firstToken)
@@ -940,6 +939,19 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
         *indentDepth = *savedIndentDepth + 2*m_indentSize;
         break;
 
+    case declaration_start:
+        if (firstToken)
+            *savedIndentDepth = tokenPosition;
+        // continuation indent in function bodies only, to not indent
+        // after the return type in "void\nfoo() {}"
+        for (int i = 0; state(i).type != topmost_intro; ++i) {
+            if (state(i).type == defun_open) {
+                *indentDepth = *savedIndentDepth + 2*m_indentSize;
+                break;
+            }
+        }
+        break;
+
     case arglist_open:
     case condition_paren_open:
         if (!lastToken)
diff --git a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp
index 2cc5ee9737b..3141e76002f 100644
--- a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp
+++ b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp
@@ -43,6 +43,7 @@ private Q_SLOTS:
     void gnuStyle();
     void whitesmithsStyle();
     void singleLineEnum();
+    void functionReturnType();
 };
 
 struct Line {
@@ -758,7 +759,6 @@ void tst_CodeFormatter::whitesmithsStyle()
 
 void tst_CodeFormatter::singleLineEnum()
 {
-    enum { a, b};
     QList<Line> data;
     data << Line("enum { foo, bar, car = 2 };")
          << Line("void blah() {")
@@ -769,6 +769,23 @@ void tst_CodeFormatter::singleLineEnum()
     checkIndent(data);
 }
 
+void tst_CodeFormatter::functionReturnType()
+{
+    QList<Line> data;
+    data
+         << Line("void")
+         << Line("foo(int) {}")
+         << Line("")
+         << Line("const QList<int> &")
+         << Line("A::foo() {}")
+         << Line("")
+         << Line("template <class T>")
+         << Line("const QList<QMap<T, T> > &")
+         << Line("A::B::foo() {}")
+         ;
+    checkIndent(data);
+}
+
 QTEST_APPLESS_MAIN(tst_CodeFormatter)
 #include "tst_codeformatter.moc"
 
-- 
GitLab