From 57f2b3e44da5ca818d575b2c5861f736aa3db0bc Mon Sep 17 00:00:00 2001
From: Christian Kamm <christian.d.kamm@nokia.com>
Date: Thu, 27 May 2010 11:26:20 +0200
Subject: [PATCH] QmlJS: Allow folding of multi-line comments.

This also makes the editor auto-fold the license comment.

Task-number: QTCREATORBUG-1455
Reviewed-by: Erik Verbruggen
---
 src/libs/qmljs/qmljsscanner.cpp              |  7 +------
 src/libs/qmljs/qmljsscanner.h                |  7 ++++++-
 src/plugins/qmljseditor/qmljshighlighter.cpp | 16 +++++++++++++---
 src/plugins/qmljseditor/qmljshighlighter.h   |  1 +
 4 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/src/libs/qmljs/qmljsscanner.cpp b/src/libs/qmljs/qmljsscanner.cpp
index 79f81509f23..9a629db9a91 100644
--- a/src/libs/qmljs/qmljsscanner.cpp
+++ b/src/libs/qmljs/qmljsscanner.cpp
@@ -77,7 +77,7 @@ const _Tp *end(const _Tp (&a)[N])
 }
 
 Scanner::Scanner()
-    : _state(0),
+    : _state(Normal),
       _scanComments(true)
 {
 }
@@ -122,11 +122,6 @@ static bool isNumberChar(QChar ch)
 
 QList<Token> Scanner::operator()(const QString &text, int startState)
 {
-    enum {
-        Normal = 0,
-        MultiLineComment = 1
-    };
-
     _state = startState;
     QList<Token> tokens;
 
diff --git a/src/libs/qmljs/qmljsscanner.h b/src/libs/qmljs/qmljsscanner.h
index 9141842cd71..611024d4262 100644
--- a/src/libs/qmljs/qmljsscanner.h
+++ b/src/libs/qmljs/qmljsscanner.h
@@ -77,13 +77,18 @@ public:
 class QMLJS_EXPORT Scanner
 {
 public:
+    enum {
+        Normal = 0,
+        MultiLineComment = 1
+    };
+
     Scanner();
     virtual ~Scanner();
 
     bool scanComments() const;
     void setScanComments(bool scanComments);
 
-    QList<Token> operator()(const QString &text, int startState = 0);
+    QList<Token> operator()(const QString &text, int startState = Normal);
     int state() const;
 
     bool isKeyword(const QString &text) const;
diff --git a/src/plugins/qmljseditor/qmljshighlighter.cpp b/src/plugins/qmljseditor/qmljshighlighter.cpp
index 0e4cf833b0d..a6412fc9a98 100644
--- a/src/plugins/qmljseditor/qmljshighlighter.cpp
+++ b/src/plugins/qmljseditor/qmljshighlighter.cpp
@@ -40,7 +40,8 @@ using namespace QmlJS;
 
 Highlighter::Highlighter(QTextDocument *parent)
     : QSyntaxHighlighter(parent),
-      m_qmlEnabled(true)
+      m_qmlEnabled(true),
+      m_inMultilineComment(false)
 {
     m_currentBlockParentheses.reserve(20);
     m_braceDepth = 0;
@@ -99,6 +100,15 @@ void Highlighter::highlightBlock(const QString &text)
                 break;
 
             case Token::Comment:
+                if (m_inMultilineComment && text.midRef(token.end() - 2, 2) == QLatin1String("*/")) {
+                    onClosingParenthesis('-', token.end() - 1, index == tokens.size()-1);
+                    m_inMultilineComment = false;
+                } else if (!m_inMultilineComment
+                           && m_scanner.state() == Scanner::MultiLineComment
+                           && index == tokens.size() - 1) {
+                    onOpeningParenthesis('+', token.offset, index == 0);
+                    m_inMultilineComment = true;
+                }
                 setFormat(token.offset, token.length, m_formats[CommentFormat]);
                 break;
 
@@ -327,7 +337,7 @@ void Highlighter::onBlockEnd(int state)
 
 void Highlighter::onOpeningParenthesis(QChar parenthesis, int pos, bool atStart)
 {
-    if (parenthesis == QLatin1Char('{') || parenthesis == QLatin1Char('[')) {
+    if (parenthesis == QLatin1Char('{') || parenthesis == QLatin1Char('[') || parenthesis == QLatin1Char('+')) {
         ++m_braceDepth;
         // if a folding block opens at the beginning of a line, treat the entire line
         // as if it were inside the folding block
@@ -339,7 +349,7 @@ void Highlighter::onOpeningParenthesis(QChar parenthesis, int pos, bool atStart)
 
 void Highlighter::onClosingParenthesis(QChar parenthesis, int pos, bool atEnd)
 {
-    if (parenthesis == QLatin1Char('}') || parenthesis == QLatin1Char(']')) {
+    if (parenthesis == QLatin1Char('}') || parenthesis == QLatin1Char(']') || parenthesis == QLatin1Char('-')) {
         --m_braceDepth;
         if (atEnd)
             TextEditor::BaseTextDocumentLayout::userData(currentBlock())->setFoldingEndIncluded(true);
diff --git a/src/plugins/qmljseditor/qmljshighlighter.h b/src/plugins/qmljseditor/qmljshighlighter.h
index cf59f5d6135..69b1c684d02 100644
--- a/src/plugins/qmljseditor/qmljshighlighter.h
+++ b/src/plugins/qmljseditor/qmljshighlighter.h
@@ -95,6 +95,7 @@ private:
     bool m_qmlEnabled;
     int m_braceDepth;
     int m_foldingIndent;
+    bool m_inMultilineComment;
 
     QmlJS::Scanner m_scanner;
     Parentheses m_currentBlockParentheses;
-- 
GitLab