Added C-style comment folding.

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

Done-with: ckamm
This commit is contained in:
Erik Verbruggen
2010-06-15 11:26:27 +02:00
parent 512e75a561
commit e5b9c76f27
6 changed files with 38 additions and 18 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;
@@ -98,6 +99,13 @@ void Highlighter::highlightBlock(const QString &text)
break; break;
case Token::Comment: 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]); setFormat(token.offset, token.length, m_formats[CommentFormat]);
break; break;
@@ -305,8 +313,9 @@ int Highlighter::onBlockStart()
int state = 0; int state = 0;
int previousState = previousBlockState(); int previousState = previousBlockState();
if (previousState != -1) { if (previousState != -1) {
state = previousState & 0xff; m_inMultilineComment = previousState & 0x1;
m_braceDepth = previousState >> 8; state = (previousState >> 1) & 0xff;
m_braceDepth = (previousState >> 9);
} }
return state; return state;
@@ -316,7 +325,7 @@ void Highlighter::onBlockEnd(int state, int firstNonSpace)
{ {
typedef TextEditor::TextBlockUserData TextEditorBlockData; 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 // Set block data parentheses. Force creation of block data unless empty
TextEditorBlockData *blockData = 0; TextEditorBlockData *blockData = 0;
@@ -335,15 +344,22 @@ void Highlighter::onBlockEnd(int state, int firstNonSpace)
} }
if (!m_currentBlockParentheses.isEmpty()) { if (!m_currentBlockParentheses.isEmpty()) {
QTC_ASSERT(blockData, return); 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 >= 0) {
if (collapse == firstNonSpace) if (collapse == firstNonSpace && c != '+')
blockData->setCollapseMode(TextEditor::TextBlockUserData::CollapseThis); blockData->setCollapseMode(TextEditor::TextBlockUserData::CollapseThis);
else else
blockData->setCollapseMode(TextEditor::TextBlockUserData::CollapseAfter); blockData->setCollapseMode(TextEditor::TextBlockUserData::CollapseAfter);
} }
if (Parenthesis::hasClosingCollapse(m_currentBlockParentheses)) collapse = Parenthesis::closeCollapseAtPos(m_currentBlockParentheses, &c);
if (collapse >= 0) {
if (c != '-')
blockData->setClosingCollapseMode(TextEditor::TextBlockUserData::NoClosingCollapse); blockData->setClosingCollapseMode(TextEditor::TextBlockUserData::NoClosingCollapse);
else
blockData->setClosingCollapseMode(TextEditor::TextBlockUserData::ClosingCollapseAtEnd);
}
} }
} }

View File

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

View File

@@ -36,7 +36,7 @@ bool Parenthesis::hasClosingCollapse(const Parentheses &parentheses)
return closeCollapseAtPos(parentheses) >= 0; return closeCollapseAtPos(parentheses) >= 0;
} }
int Parenthesis::closeCollapseAtPos(const Parentheses &parentheses) int Parenthesis::closeCollapseAtPos(const Parentheses &parentheses, QChar *character)
{ {
int depth = 0; int depth = 0;
for (int i = 0; i < parentheses.size(); ++i) { for (int i = 0; i < parentheses.size(); ++i) {
@@ -48,10 +48,13 @@ int Parenthesis::closeCollapseAtPos(const Parentheses &parentheses)
} else if (p.chr == QLatin1Char('}') } else if (p.chr == QLatin1Char('}')
|| p.chr == QLatin1Char('-') || p.chr == QLatin1Char('-')
|| p.chr == QLatin1Char(']')) { || p.chr == QLatin1Char(']')) {
if (--depth < 0) if (--depth < 0) {
if (character)
*character = p.chr;
return p.pos; return p.pos;
} }
} }
}
return -1; return -1;
} }

View File

@@ -53,7 +53,7 @@ struct TEXTEDITOR_EXPORT Parenthesis
QChar chr; QChar chr;
int pos; int pos;
static int collapseAtPos(const Parentheses &parentheses, QChar *character = 0); 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); static bool hasClosingCollapse(const Parentheses &parentheses);
}; };