From 1bc838fd2b7a8059d47d7177c594682c32df705f Mon Sep 17 00:00:00 2001
From: Roberto Raggi <roberto.raggi@nokia.com>
Date: Tue, 19 Jan 2010 12:50:03 +0100
Subject: [PATCH] Handle comments at the end of braceless control statements.

---
 src/libs/qmljs/qmljsindenter.cpp | 19 ++++++++++++++++++-
 src/libs/qmljs/qmljsindenter.h   |  2 ++
 src/libs/qmljs/qmljsscanner.h    | 11 +++++++----
 3 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/src/libs/qmljs/qmljsindenter.cpp b/src/libs/qmljs/qmljsindenter.cpp
index dc5407c8c93..ea42bd4f65c 100644
--- a/src/libs/qmljs/qmljsindenter.cpp
+++ b/src/libs/qmljs/qmljsindenter.cpp
@@ -309,6 +309,18 @@ bool QmlJSIndenter::okay(QChar typedIn, QChar okayCh) const
     return typedIn == QChar() || typedIn == okayCh;
 }
 
+QmlJSScanner::Token QmlJSIndenter::lastToken() const
+{
+    for (int index = yyLinizerState.tokens.size() - 1; index != -1; --index) {
+        const QmlJSScanner::Token &token = yyLinizerState.tokens.at(index);
+
+        if (token.isNot(QmlJSScanner::Token::Comment))
+            return token;
+    }
+
+    return QmlJSScanner::Token();
+}
+
 /*
     Saves and restores the state of the global linizer. This enables
     backtracking.
@@ -473,7 +485,7 @@ bool QmlJSIndenter::matchBracelessControlStatement()
     int delimDepth = 0;
 
     if (! yyLinizerState.tokens.isEmpty()) {
-        const QmlJSScanner::Token &tk = yyLinizerState.tokens.last();
+        QmlJSScanner::Token tk = lastToken();
 
         if (tk.is(QmlJSScanner::Token::Identifier) &&
             yyLinizerState.line.midRef(tk.offset, tk.length) == QLatin1String("else"))
@@ -491,6 +503,10 @@ bool QmlJSIndenter::matchBracelessControlStatement()
             default:
                 break;
 
+            case QmlJSScanner::Token::Comment:
+                // skip comments
+                break;
+
             case QmlJSScanner::Token::RightParenthesis:
                 ++delimDepth;
                 break;
@@ -1056,3 +1072,4 @@ int QmlJSIndenter::indentForBottomLine(QTextBlock begin, QTextBlock end, QChar t
 
     return qMax(0, indent);
 }
+
diff --git a/src/libs/qmljs/qmljsindenter.h b/src/libs/qmljs/qmljsindenter.h
index d880a1a3939..2fee67390ff 100644
--- a/src/libs/qmljs/qmljsindenter.h
+++ b/src/libs/qmljs/qmljsindenter.h
@@ -84,6 +84,8 @@ private:
     int indentForContinuationLine();
     int indentForStandaloneLine();
 
+    QmlJSScanner::Token lastToken() const;
+
 private:
     int ppHardwareTabSize;
     int ppIndentSize;
diff --git a/src/libs/qmljs/qmljsscanner.h b/src/libs/qmljs/qmljsscanner.h
index 9a6abb77021..8b3990fc5b0 100644
--- a/src/libs/qmljs/qmljsscanner.h
+++ b/src/libs/qmljs/qmljsscanner.h
@@ -41,11 +41,9 @@ namespace QmlJS {
 class QMLJS_EXPORT QmlJSScanner
 {
 public:
-
     struct Token {
-        int offset;
-        int length;
         enum Kind {
+            EndOfFile,
             Keyword,
             Identifier,
             String,
@@ -62,8 +60,13 @@ public:
             Colon,
             Comma,
             Dot
-        } kind;
+        };
+
+        int offset;
+        int length;
+        Kind kind;
 
+        inline Token(): offset(0), length(0), kind(EndOfFile) {}
         inline Token(int o, int l, Kind k): offset(o), length(l), kind(k) {}
         inline int begin() const { return offset; }
         inline int end() const { return offset + length; }
-- 
GitLab