From 62dba8e4c17f0871cc8cd8a5296c980d4f7a441e Mon Sep 17 00:00:00 2001
From: Christian Kamm <christian.d.kamm@nokia.com>
Date: Wed, 10 Nov 2010 10:34:47 +0100
Subject: [PATCH] QmlJS: Fix incorrect completion inside 'Item\n{ ... }'.

The newline made the linizer add an automatic semicolon after Item,
causing the completion context finder to not recognize it as an object
definition.

Task-number: QTCREATORBUG-2658
Reviewed-by: Roberto Raggi
---
 src/libs/qmljs/qmljslineinfo.cpp | 34 +++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/src/libs/qmljs/qmljslineinfo.cpp b/src/libs/qmljs/qmljslineinfo.cpp
index 5bae0d727c5..ed13e7bc6da 100644
--- a/src/libs/qmljs/qmljslineinfo.cpp
+++ b/src/libs/qmljs/qmljslineinfo.cpp
@@ -191,9 +191,41 @@ QString LineInfo::trimmedCodeLine(const QString &t)
             needSemicolon = true;
             break;
 
-        case Token::Identifier:
+        case Token::Identifier: {
+            // need to disambiguate
+            // "Rectangle\n{" in a QML context from
+            // "a = Somevar\n{" in a JS context
+            // What's done here does not cover all cases, but goes as far as possible
+            // with the limited information that's available.
+            const QStringRef text = tokenText(last);
+            if (yyLinizerState.leftBraceFollows && !text.isEmpty() && text.at(0).isUpper()) {
+                int i = index;
+
+                // skip any preceeding 'identifier.'; these could appear in both cases
+                while (i >= 2) {
+                    const Token &prev = yyLinizerState.tokens.at(i-1);
+                    const Token &prevPrev = yyLinizerState.tokens.at(i-2);
+                    if (prev.kind == Token::Dot && prevPrev.kind == Token::Identifier) {
+                        i -= 2;
+                    } else {
+                        break;
+                    }
+                }
+
+                // it could also be 'a = \n Foo \n {', but that sounds unlikely
+                if (i == 0)
+                    break;
+
+                // these indicate a QML context
+                const Token &prev = yyLinizerState.tokens.at(i-1);
+                if (prev.kind == Token::Semicolon || prev.kind == Token::Identifier
+                        || prev.kind == Token::RightBrace || prev.kind == Token::RightBracket) {
+                    break;
+                }
+            }
             needSemicolon = true;
             break;
+        }
 
         case Token::Keyword:
             if (tokenText(last) != QLatin1String("else"))
-- 
GitLab