diff --git a/src/libs/qmljs/qmljscodeformatter.cpp b/src/libs/qmljs/qmljscodeformatter.cpp
index 7b1a5e250bb732a225afa8a73f62f011d52f9c36..1ef3c80c0852d380a57d6268d38ee387063d2f2e 100644
--- a/src/libs/qmljs/qmljscodeformatter.cpp
+++ b/src/libs/qmljs/qmljscodeformatter.cpp
@@ -396,7 +396,6 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
             } break;
 
         case maybe_catch_or_finally:
-            dump();
             switch (kind) {
             case Catch:             turnInto(catch_statement); break;
             case Finally:           turnInto(finally_statement); break;
@@ -1125,17 +1124,27 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
         }
         break;
 
-    case function_start:
-        if (parentState.type == expression) {
-            // undo the continuation indent of the expression
-            *indentDepth = parentState.savedIndentDepth;
-            *savedIndentDepth = *indentDepth;
-        } else {
-            // always align to function keyword
+    case function_start: {
+        // in these states, align to the 'function' keyword
+        const int parentType = parentState.type;
+        if (parentType == objectdefinition_open
+                || parentType == paren_open
+                || parentType == bracket_open) {
             *indentDepth = tokenPosition;
             *savedIndentDepth = *indentDepth;
+            break;
+        }
+
+        // otherwise find the enclosing expression end state and align to that
+        for (int i = 1; state(i).type != topmost_intro; ++i) {
+            const int type = state(i).type;
+            if (isExpressionEndState(type)) {
+                *indentDepth = state(i - 1).savedIndentDepth;
+                break;
+            }
         }
         break;
+    }
 
     case do_statement_while_paren_open:
     case statement_with_condition_paren_open:
diff --git a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp
index 5a4411a1e9da4a0329253bc796b56df88fa00436..82cefa729beb4a0f5e81293f4d01fee8f0c7a598 100644
--- a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp
+++ b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp
@@ -56,6 +56,9 @@ private Q_SLOTS:
     void functionDeclaration();
     void functionExpression1();
     void functionExpression2();
+    void functionExpression3();
+    void functionExpression4();
+    void functionExpression5();
     void propertyDeclarations();
     void signalDeclarations();
     void ifBinding1();
@@ -415,6 +418,76 @@ void tst_QMLCodeFormatter::functionExpression2()
     checkIndent(data);
 }
 
+void tst_QMLCodeFormatter::functionExpression3()
+{
+    QList<Line> data;
+    data << Line("Rectangle {")
+         << Line("    function foo(a, b, c) {")
+         << Line("        var foo = {")
+         << Line("            bar: function() {")
+         << Line("                bar = 2")
+         << Line("            },")
+         << Line("            bar2: function()")
+         << Line("            {")
+         << Line("                bar = 2")
+         << Line("            }")
+         << Line("        }")
+         << Line("        Foo.proto.bar = function() {")
+         << Line("            bar = 2")
+         << Line("        }")
+         << Line("        Foo.proto.bar = function()")
+         << Line("        {")
+         << Line("            bar = 2")
+         << Line("        }")
+         << Line("    }")
+         << Line("}")
+         ;
+    checkIndent(data);
+}
+
+void tst_QMLCodeFormatter::functionExpression4()
+{
+    QList<Line> data;
+    data << Line("Rectangle {")
+         << Line("    function foo(a, b, c) {")
+         << Line("        baz = function() {")
+         << Line("            bar = 2")
+         << Line("        }")
+         << Line("        baz = function()")
+         << Line("        {")
+         << Line("            bar = 2")
+         << Line("        }")
+         << Line("        var buz = new function() {")
+         << Line("            this.bar = function() {")
+         << Line("                bar = 2")
+         << Line("            }")
+         << Line("            this.bar = function()")
+         << Line("            {")
+         << Line("                bar = 2")
+         << Line("            }")
+         << Line("        }")
+         << Line("    }")
+         << Line("}")
+         ;
+    checkIndent(data);
+}
+
+void tst_QMLCodeFormatter::functionExpression5()
+{
+    QList<Line> data;
+    data << Line("Rectangle {")
+         << Line("    property var foo: function() {")
+         << Line("        bar = 2")
+         << Line("    }")
+         << Line("    property var foo: function()")
+         << Line("    {")
+         << Line("        bar = 2")
+         << Line("    }")
+         << Line("}")
+         ;
+    checkIndent(data);
+}
+
 void tst_QMLCodeFormatter::propertyDeclarations()
 {
     QList<Line> data;