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
This commit is contained in:
Christian Kamm
2010-05-27 11:26:20 +02:00
parent 385034722c
commit 57f2b3e44d
4 changed files with 21 additions and 10 deletions

View File

@@ -77,7 +77,7 @@ const _Tp *end(const _Tp (&a)[N])
} }
Scanner::Scanner() Scanner::Scanner()
: _state(0), : _state(Normal),
_scanComments(true) _scanComments(true)
{ {
} }
@@ -122,11 +122,6 @@ static bool isNumberChar(QChar ch)
QList<Token> Scanner::operator()(const QString &text, int startState) QList<Token> Scanner::operator()(const QString &text, int startState)
{ {
enum {
Normal = 0,
MultiLineComment = 1
};
_state = startState; _state = startState;
QList<Token> tokens; QList<Token> tokens;

View File

@@ -77,13 +77,18 @@ public:
class QMLJS_EXPORT Scanner class QMLJS_EXPORT Scanner
{ {
public: public:
enum {
Normal = 0,
MultiLineComment = 1
};
Scanner(); Scanner();
virtual ~Scanner(); virtual ~Scanner();
bool scanComments() const; bool scanComments() const;
void setScanComments(bool scanComments); 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; int state() const;
bool isKeyword(const QString &text) const; bool isKeyword(const QString &text) const;

View File

@@ -40,7 +40,8 @@ using namespace QmlJS;
Highlighter::Highlighter(QTextDocument *parent) Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent), : QSyntaxHighlighter(parent),
m_qmlEnabled(true) m_qmlEnabled(true),
m_inMultilineComment(false)
{ {
m_currentBlockParentheses.reserve(20); m_currentBlockParentheses.reserve(20);
m_braceDepth = 0; m_braceDepth = 0;
@@ -99,6 +100,15 @@ void Highlighter::highlightBlock(const QString &text)
break; break;
case Token::Comment: 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]); setFormat(token.offset, token.length, m_formats[CommentFormat]);
break; break;
@@ -327,7 +337,7 @@ void Highlighter::onBlockEnd(int state)
void Highlighter::onOpeningParenthesis(QChar parenthesis, int pos, bool atStart) void Highlighter::onOpeningParenthesis(QChar parenthesis, int pos, bool atStart)
{ {
if (parenthesis == QLatin1Char('{') || parenthesis == QLatin1Char('[')) { if (parenthesis == QLatin1Char('{') || parenthesis == QLatin1Char('[') || parenthesis == QLatin1Char('+')) {
++m_braceDepth; ++m_braceDepth;
// if a folding block opens at the beginning of a line, treat the entire line // if a folding block opens at the beginning of a line, treat the entire line
// as if it were inside the folding block // 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) void Highlighter::onClosingParenthesis(QChar parenthesis, int pos, bool atEnd)
{ {
if (parenthesis == QLatin1Char('}') || parenthesis == QLatin1Char(']')) { if (parenthesis == QLatin1Char('}') || parenthesis == QLatin1Char(']') || parenthesis == QLatin1Char('-')) {
--m_braceDepth; --m_braceDepth;
if (atEnd) if (atEnd)
TextEditor::BaseTextDocumentLayout::userData(currentBlock())->setFoldingEndIncluded(true); TextEditor::BaseTextDocumentLayout::userData(currentBlock())->setFoldingEndIncluded(true);

View File

@@ -95,6 +95,7 @@ private:
bool m_qmlEnabled; bool m_qmlEnabled;
int m_braceDepth; int m_braceDepth;
int m_foldingIndent; int m_foldingIndent;
bool m_inMultilineComment;
QmlJS::Scanner m_scanner; QmlJS::Scanner m_scanner;
Parentheses m_currentBlockParentheses; Parentheses m_currentBlockParentheses;