Fixed the highlihting of bindings and types.

This commit is contained in:
Roberto Raggi
2010-01-29 10:22:18 +01:00
parent 2dd5bebd12
commit d78e64dbf7

View File

@@ -57,17 +57,28 @@ QScriptHighlighter::QScriptHighlighter(bool duiEnabled, QTextDocument *parent):
bool QScriptHighlighter::isDuiEnabled() const bool QScriptHighlighter::isDuiEnabled() const
{ return m_duiEnabled; } { return m_duiEnabled; }
static bool checkStartOfBinding(const Token &token)
{
switch (token.kind) {
case Token::Semicolon:
case Token::LeftBrace:
case Token::RightBrace:
case Token::LeftBracket:
case Token::RightBracket:
return true;
default:
return false;
} // end of switch
}
void QScriptHighlighter::highlightBlock(const QString &text) void QScriptHighlighter::highlightBlock(const QString &text)
{ {
const QList<Token> tokens = m_scanner(text, onBlockStart()); const QList<Token> tokens = m_scanner(text, onBlockStart());
QTextCharFormat emptyFormat; int index = 0;
int lastEnd = 0; while (index < tokens.size()) {
for (int i = 0; i < tokens.size(); ++i) { const Token &token = tokens.at(index);
const Token token = tokens.at(i);
if (token.offset != lastEnd)
setFormat(lastEnd, token.offset - lastEnd, m_formats[VisualWhitespace]);
switch (token.kind) { switch (token.kind) {
case Token::Keyword: case Token::Keyword:
@@ -110,43 +121,42 @@ void QScriptHighlighter::highlightBlock(const QString &text)
onClosingParenthesis(']', token.offset); onClosingParenthesis(']', token.offset);
break; break;
case Token::Identifier: case Token::Identifier: {
if (m_duiEnabled && (i + 1) < tokens.size() && tokens.at(i + 1).is(Token::Colon)) { if (index + 1 < tokens.size()) {
int j = i; if (tokens.at(index + 1).is(Token::LeftBrace) && text.at(token.offset).isUpper()) {
for (; j != -1; --j) { setFormat(token.offset, token.length, m_formats[TypeFormat]);
const Token &tok = tokens.at(j); } else if (index == 0 || checkStartOfBinding(tokens.at(index - 1))) {
if (tok.is(Token::Dot) || tok.is(Token::Identifier)) { const int start = index;
setFormat(tok.offset, tok.length, m_formats[LabelFormat]);
} else { ++index; // skip the identifier.
while (index + 1 < tokens.size() &&
tokens.at(index).is(Token::Dot) &&
tokens.at(index + 1).is(Token::Identifier)) {
index += 2;
}
if (index < tokens.size() && tokens.at(index).is(Token::Colon)) {
// it's a binding.
for (int i = start; i < index; ++i) {
const Token &tok = tokens.at(i);
setFormat(tok.offset, tok.length, m_formats[LabelFormat]);
}
break; break;
} else {
index = start;
} }
} }
} else {
const QChar c = text.at(token.offset);
if ((m_duiEnabled && c.isUpper()) || (!m_duiEnabled && c == QLatin1Char('Q')))
setFormat(token.offset, token.length, m_formats[TypeFormat]);
else
setFormat(token.offset, token.length, emptyFormat);
} }
break; } break;
case Token::Colon:
if (m_duiEnabled && i > 0 && tokens.at(i - 1).kind == Token::Identifier)
setFormat(token.offset, token.length, m_formats[LabelFormat]);
else
setFormat(token.offset, token.length, emptyFormat);
break;
case Token::Delimiter: case Token::Delimiter:
setFormat(token.offset, token.length, emptyFormat);
break; break;
default: default:
break; break;
} } // end swtich
lastEnd = token.end(); ++index;
} }
int firstNonSpace = 0; int firstNonSpace = 0;
@@ -156,11 +166,6 @@ void QScriptHighlighter::highlightBlock(const QString &text)
firstNonSpace = tk.offset; firstNonSpace = tk.offset;
} }
if (firstNonSpace > lastEnd)
setFormat(lastEnd, firstNonSpace - lastEnd, m_formats[VisualWhitespace]);
else if (text.length() > lastEnd)
setFormat(lastEnd, text.length() - lastEnd, m_formats[VisualWhitespace]);
setCurrentBlockState(m_scanner.endState()); setCurrentBlockState(m_scanner.endState());
onBlockEnd(m_scanner.endState(), firstNonSpace); onBlockEnd(m_scanner.endState(), firstNonSpace);
} }