Commit e5b9c76f authored by Erik Verbruggen's avatar Erik Verbruggen
Browse files

Added C-style comment folding.

This is a "back-port" for 57f2b3e4 from master
into 2.0.

Done-with: ckamm
parent 512e75a5
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......@@ -98,6 +99,13 @@ void Highlighter::highlightBlock(const QString &text)
break;
case Token::Comment:
if (m_inMultilineComment && text.midRef(token.end() - 2) == QLatin1String("*/")) {
onClosingParenthesis('-', token.end() - 1);
m_inMultilineComment = false;
} else if (!m_inMultilineComment && m_scanner.state() == Scanner::MultiLineComment) {
onOpeningParenthesis('+', token.offset);
m_inMultilineComment = true;
}
setFormat(token.offset, token.length, m_formats[CommentFormat]);
break;
......@@ -305,8 +313,9 @@ int Highlighter::onBlockStart()
int state = 0;
int previousState = previousBlockState();
if (previousState != -1) {
state = previousState & 0xff;
m_braceDepth = previousState >> 8;
m_inMultilineComment = previousState & 0x1;
state = (previousState >> 1) & 0xff;
m_braceDepth = (previousState >> 9);
}
return state;
......@@ -316,7 +325,7 @@ void Highlighter::onBlockEnd(int state, int firstNonSpace)
{
typedef TextEditor::TextBlockUserData TextEditorBlockData;
setCurrentBlockState((m_braceDepth << 8) | state);
setCurrentBlockState((m_braceDepth << 9) | (state << 1) | m_inMultilineComment);
// Set block data parentheses. Force creation of block data unless empty
TextEditorBlockData *blockData = 0;
......@@ -335,15 +344,22 @@ void Highlighter::onBlockEnd(int state, int firstNonSpace)
}
if (!m_currentBlockParentheses.isEmpty()) {
QTC_ASSERT(blockData, return);
int collapse = Parenthesis::collapseAtPos(m_currentBlockParentheses);
blockData->setParentheses(m_currentBlockParentheses);
QChar c;
int collapse = Parenthesis::collapseAtPos(m_currentBlockParentheses, &c);
if (collapse >= 0) {
if (collapse == firstNonSpace)
if (collapse == firstNonSpace && c != '+')
blockData->setCollapseMode(TextEditor::TextBlockUserData::CollapseThis);
else
blockData->setCollapseMode(TextEditor::TextBlockUserData::CollapseAfter);
}
if (Parenthesis::hasClosingCollapse(m_currentBlockParentheses))
blockData->setClosingCollapseMode(TextEditor::TextBlockUserData::NoClosingCollapse);
collapse = Parenthesis::closeCollapseAtPos(m_currentBlockParentheses, &c);
if (collapse >= 0) {
if (c != '-')
blockData->setClosingCollapseMode(TextEditor::TextBlockUserData::NoClosingCollapse);
else
blockData->setClosingCollapseMode(TextEditor::TextBlockUserData::ClosingCollapseAtEnd);
}
}
}
......
......@@ -94,6 +94,7 @@ private:
bool m_qmlEnabled;
int m_braceDepth;
bool m_inMultilineComment;
QmlJS::Scanner m_scanner;
Parentheses m_currentBlockParentheses;
......
......@@ -36,7 +36,7 @@ bool Parenthesis::hasClosingCollapse(const Parentheses &parentheses)
return closeCollapseAtPos(parentheses) >= 0;
}
int Parenthesis::closeCollapseAtPos(const Parentheses &parentheses)
int Parenthesis::closeCollapseAtPos(const Parentheses &parentheses, QChar *character)
{
int depth = 0;
for (int i = 0; i < parentheses.size(); ++i) {
......@@ -48,8 +48,11 @@ int Parenthesis::closeCollapseAtPos(const Parentheses &parentheses)
} else if (p.chr == QLatin1Char('}')
|| p.chr == QLatin1Char('-')
|| p.chr == QLatin1Char(']')) {
if (--depth < 0)
if (--depth < 0) {
if (character)
*character = p.chr;
return p.pos;
}
}
}
return -1;
......
......@@ -53,7 +53,7 @@ struct TEXTEDITOR_EXPORT Parenthesis
QChar chr;
int pos;
static int collapseAtPos(const Parentheses &parentheses, QChar *character = 0);
static int closeCollapseAtPos(const Parentheses &parentheses);
static int closeCollapseAtPos(const Parentheses &parentheses, QChar *character = 0);
static bool hasClosingCollapse(const Parentheses &parentheses);
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment