From fc3628098b3b45626840fc405f8a0f94daaf36fe Mon Sep 17 00:00:00 2001
From: Christian Kamm <christian.d.kamm@nokia.com>
Date: Wed, 19 May 2010 10:09:13 +0200
Subject: [PATCH] QmlJS: Fix completion context finder if cursor is on empty
 line.

It would calculate an incorrect start token in this case as the
linizer skips past the empty line.

Task-number: QTCREATORBUG-1412
---
 .../qmljs/qmljscompletioncontextfinder.cpp    | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/src/libs/qmljs/qmljscompletioncontextfinder.cpp b/src/libs/qmljs/qmljscompletioncontextfinder.cpp
index 645472c2203..c316b7b4a74 100644
--- a/src/libs/qmljs/qmljscompletioncontextfinder.cpp
+++ b/src/libs/qmljs/qmljscompletioncontextfinder.cpp
@@ -25,14 +25,19 @@ CompletionContextFinder::CompletionContextFinder(const QTextCursor &cursor)
     initialize(cursor.document()->begin(), lastBlock);
 
     m_startTokenIndex = yyLinizerState.tokens.size() - 1;
-    for (; m_startTokenIndex >= 0; --m_startTokenIndex) {
-        const Token &token = yyLinizerState.tokens.at(m_startTokenIndex);
-        if (token.end() <= cursor.positionInBlock())
-            break;
-    }
 
-    if (m_startTokenIndex == yyLinizerState.tokens.size() - 1 && yyLinizerState.insertedSemicolon)
-        --m_startTokenIndex;
+    // Initialize calls readLine - which skips empty lines. We should only adjust
+    // the start token index if the linizer still is in the same block as the cursor.
+    if (yyLinizerState.iter == cursor.block()) {
+        for (; m_startTokenIndex >= 0; --m_startTokenIndex) {
+            const Token &token = yyLinizerState.tokens.at(m_startTokenIndex);
+            if (token.end() <= cursor.positionInBlock())
+                break;
+        }
+
+        if (m_startTokenIndex == yyLinizerState.tokens.size() - 1 && yyLinizerState.insertedSemicolon)
+            --m_startTokenIndex;
+    }
 
     getQmlObjectTypeName(m_startTokenIndex);
     checkBinding();
-- 
GitLab