2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
#include <qmljs/qmljshighlighter.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include <QtCore/QSet>
|
|
|
|
|
#include <QtCore/QtAlgorithms>
|
2010-01-28 13:12:52 +01:00
|
|
|
#include <QtCore/QDebug>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-18 14:12:04 +01:00
|
|
|
using namespace QmlJS;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-09-28 14:49:39 +02:00
|
|
|
QScriptHighlighter::QScriptHighlighter(bool duiEnabled, QTextDocument *parent):
|
|
|
|
|
QSyntaxHighlighter(parent),
|
|
|
|
|
m_duiEnabled(duiEnabled)
|
|
|
|
|
{
|
2009-10-07 16:31:08 +02:00
|
|
|
QVector<QTextCharFormat> rc;
|
|
|
|
|
rc.resize(NumFormats);
|
|
|
|
|
rc[NumberFormat].setForeground(Qt::blue);
|
|
|
|
|
rc[StringFormat].setForeground(Qt::darkGreen);
|
|
|
|
|
rc[TypeFormat].setForeground(Qt::darkMagenta);
|
|
|
|
|
rc[KeywordFormat].setForeground(Qt::darkYellow);
|
|
|
|
|
rc[LabelFormat].setForeground(Qt::darkRed);
|
|
|
|
|
rc[CommentFormat].setForeground(Qt::red); rc[CommentFormat].setFontItalic(true);
|
|
|
|
|
rc[PreProcessorFormat].setForeground(Qt::darkBlue);
|
|
|
|
|
rc[VisualWhitespace].setForeground(Qt::lightGray); // for debug: rc[VisualWhitespace].setBackground(Qt::red);
|
|
|
|
|
setFormats(rc);
|
2009-09-28 14:49:39 +02:00
|
|
|
|
|
|
|
|
m_scanner.setKeywords(keywords());
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-22 15:21:04 +02:00
|
|
|
bool QScriptHighlighter::isDuiEnabled() const
|
|
|
|
|
{ return m_duiEnabled; }
|
|
|
|
|
|
2010-01-29 10:22:18 +01:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void QScriptHighlighter::highlightBlock(const QString &text)
|
|
|
|
|
{
|
2010-01-28 13:12:52 +01:00
|
|
|
const QList<Token> tokens = m_scanner(text, onBlockStart());
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-29 10:22:18 +01:00
|
|
|
int index = 0;
|
|
|
|
|
while (index < tokens.size()) {
|
|
|
|
|
const Token &token = tokens.at(index);
|
2009-10-07 16:31:08 +02:00
|
|
|
|
2009-09-28 14:49:39 +02:00
|
|
|
switch (token.kind) {
|
2010-01-19 12:54:17 +01:00
|
|
|
case Token::Keyword:
|
2009-09-28 14:49:39 +02:00
|
|
|
setFormat(token.offset, token.length, m_formats[KeywordFormat]);
|
|
|
|
|
break;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-19 12:54:17 +01:00
|
|
|
case Token::String:
|
2009-10-08 14:57:18 +02:00
|
|
|
highlightWhitespace(token, text, StringFormat);
|
2009-09-28 14:49:39 +02:00
|
|
|
break;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-19 12:54:17 +01:00
|
|
|
case Token::Comment:
|
2009-10-08 14:57:18 +02:00
|
|
|
highlightWhitespace(token, text, CommentFormat);
|
2009-09-28 14:49:39 +02:00
|
|
|
break;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-19 12:54:17 +01:00
|
|
|
case Token::Number:
|
2009-10-08 14:57:18 +02:00
|
|
|
highlightWhitespace(token, text, NumberFormat);
|
2009-09-28 14:49:39 +02:00
|
|
|
break;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-19 12:54:17 +01:00
|
|
|
case Token::LeftParenthesis:
|
2009-09-28 14:49:39 +02:00
|
|
|
onOpeningParenthesis('(', token.offset);
|
|
|
|
|
break;
|
2009-05-11 11:53:41 +02:00
|
|
|
|
2010-01-19 12:54:17 +01:00
|
|
|
case Token::RightParenthesis:
|
2009-09-28 14:49:39 +02:00
|
|
|
onClosingParenthesis(')', token.offset);
|
|
|
|
|
break;
|
2009-05-11 11:53:41 +02:00
|
|
|
|
2010-01-19 12:54:17 +01:00
|
|
|
case Token::LeftBrace:
|
2009-09-28 14:49:39 +02:00
|
|
|
onOpeningParenthesis('{', token.offset);
|
|
|
|
|
break;
|
2009-05-11 11:22:29 +02:00
|
|
|
|
2010-01-19 12:54:17 +01:00
|
|
|
case Token::RightBrace:
|
2009-09-28 14:49:39 +02:00
|
|
|
onClosingParenthesis('}', token.offset);
|
|
|
|
|
break;
|
2009-05-11 11:22:29 +02:00
|
|
|
|
2010-01-19 12:54:17 +01:00
|
|
|
case Token::LeftBracket:
|
2009-09-28 14:49:39 +02:00
|
|
|
onOpeningParenthesis('[', token.offset);
|
|
|
|
|
break;
|
2009-05-11 11:22:29 +02:00
|
|
|
|
2010-01-19 12:54:17 +01:00
|
|
|
case Token::RightBracket:
|
2009-09-28 14:49:39 +02:00
|
|
|
onClosingParenthesis(']', token.offset);
|
|
|
|
|
break;
|
2009-05-11 11:22:29 +02:00
|
|
|
|
2010-01-29 10:22:18 +01:00
|
|
|
case Token::Identifier: {
|
|
|
|
|
if (index + 1 < tokens.size()) {
|
|
|
|
|
if (tokens.at(index + 1).is(Token::LeftBrace) && text.at(token.offset).isUpper()) {
|
|
|
|
|
setFormat(token.offset, token.length, m_formats[TypeFormat]);
|
|
|
|
|
} else if (index == 0 || checkStartOfBinding(tokens.at(index - 1))) {
|
|
|
|
|
const int start = index;
|
|
|
|
|
|
|
|
|
|
++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]);
|
|
|
|
|
}
|
2010-01-14 16:48:06 +01:00
|
|
|
break;
|
2010-01-29 10:22:18 +01:00
|
|
|
} else {
|
|
|
|
|
index = start;
|
2010-01-14 16:48:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
2009-10-07 16:31:08 +02:00
|
|
|
}
|
2010-01-29 10:22:18 +01:00
|
|
|
} break;
|
2009-10-07 16:31:08 +02:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
case Token::Delimiter:
|
2009-09-28 14:49:39 +02:00
|
|
|
break;
|
2009-05-11 11:53:41 +02:00
|
|
|
|
2009-10-07 16:31:08 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
2010-01-29 10:22:18 +01:00
|
|
|
} // end swtich
|
2009-10-07 16:31:08 +02:00
|
|
|
|
2010-01-29 10:22:18 +01:00
|
|
|
++index;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
int firstNonSpace = 0;
|
|
|
|
|
|
|
|
|
|
if (! tokens.isEmpty()) {
|
|
|
|
|
const Token &tk = tokens.first();
|
|
|
|
|
firstNonSpace = tk.offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setCurrentBlockState(m_scanner.endState());
|
2010-01-28 14:17:19 +01:00
|
|
|
onBlockEnd(m_scanner.endState(), firstNonSpace);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QScriptHighlighter::setFormats(const QVector<QTextCharFormat> &s)
|
|
|
|
|
{
|
2009-10-07 16:31:08 +02:00
|
|
|
Q_ASSERT(s.size() == NumFormats);
|
2008-12-02 12:01:29 +01:00
|
|
|
qCopy(s.constBegin(), s.constEnd(), m_formats);
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-07 16:31:08 +02:00
|
|
|
QSet<QString> QScriptHighlighter::keywords()
|
|
|
|
|
{
|
|
|
|
|
QSet<QString> keywords;
|
|
|
|
|
|
|
|
|
|
keywords << QLatin1String("Infinity");
|
|
|
|
|
keywords << QLatin1String("NaN");
|
|
|
|
|
keywords << QLatin1String("abstract");
|
|
|
|
|
keywords << QLatin1String("boolean");
|
|
|
|
|
keywords << QLatin1String("break");
|
|
|
|
|
keywords << QLatin1String("byte");
|
|
|
|
|
keywords << QLatin1String("case");
|
|
|
|
|
keywords << QLatin1String("catch");
|
|
|
|
|
keywords << QLatin1String("char");
|
|
|
|
|
keywords << QLatin1String("class");
|
|
|
|
|
keywords << QLatin1String("const");
|
|
|
|
|
keywords << QLatin1String("constructor");
|
|
|
|
|
keywords << QLatin1String("continue");
|
|
|
|
|
keywords << QLatin1String("debugger");
|
|
|
|
|
keywords << QLatin1String("default");
|
|
|
|
|
keywords << QLatin1String("delete");
|
|
|
|
|
keywords << QLatin1String("do");
|
|
|
|
|
keywords << QLatin1String("double");
|
|
|
|
|
keywords << QLatin1String("else");
|
|
|
|
|
keywords << QLatin1String("enum");
|
|
|
|
|
keywords << QLatin1String("export");
|
|
|
|
|
keywords << QLatin1String("extends");
|
|
|
|
|
keywords << QLatin1String("false");
|
|
|
|
|
keywords << QLatin1String("final");
|
|
|
|
|
keywords << QLatin1String("finally");
|
|
|
|
|
keywords << QLatin1String("float");
|
|
|
|
|
keywords << QLatin1String("for");
|
|
|
|
|
keywords << QLatin1String("function");
|
|
|
|
|
keywords << QLatin1String("goto");
|
|
|
|
|
keywords << QLatin1String("if");
|
|
|
|
|
keywords << QLatin1String("implements");
|
|
|
|
|
keywords << QLatin1String("import");
|
|
|
|
|
keywords << QLatin1String("in");
|
|
|
|
|
keywords << QLatin1String("instanceof");
|
|
|
|
|
keywords << QLatin1String("int");
|
|
|
|
|
keywords << QLatin1String("interface");
|
|
|
|
|
keywords << QLatin1String("long");
|
|
|
|
|
keywords << QLatin1String("native");
|
|
|
|
|
keywords << QLatin1String("new");
|
|
|
|
|
keywords << QLatin1String("package");
|
|
|
|
|
keywords << QLatin1String("private");
|
|
|
|
|
keywords << QLatin1String("protected");
|
|
|
|
|
keywords << QLatin1String("public");
|
|
|
|
|
keywords << QLatin1String("return");
|
|
|
|
|
keywords << QLatin1String("short");
|
|
|
|
|
keywords << QLatin1String("static");
|
|
|
|
|
keywords << QLatin1String("super");
|
|
|
|
|
keywords << QLatin1String("switch");
|
|
|
|
|
keywords << QLatin1String("synchronized");
|
|
|
|
|
keywords << QLatin1String("this");
|
|
|
|
|
keywords << QLatin1String("throw");
|
|
|
|
|
keywords << QLatin1String("throws");
|
|
|
|
|
keywords << QLatin1String("transient");
|
|
|
|
|
keywords << QLatin1String("true");
|
|
|
|
|
keywords << QLatin1String("try");
|
|
|
|
|
keywords << QLatin1String("typeof");
|
|
|
|
|
keywords << QLatin1String("undefined");
|
|
|
|
|
keywords << QLatin1String("var");
|
|
|
|
|
keywords << QLatin1String("void");
|
|
|
|
|
keywords << QLatin1String("volatile");
|
|
|
|
|
keywords << QLatin1String("while");
|
|
|
|
|
keywords << QLatin1String("with");
|
|
|
|
|
|
|
|
|
|
return keywords;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
int QScriptHighlighter::onBlockStart()
|
|
|
|
|
{
|
2010-01-28 13:12:52 +01:00
|
|
|
return currentBlockState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QScriptHighlighter::onBlockEnd(int, int)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QScriptHighlighter::onOpeningParenthesis(QChar, int)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QScriptHighlighter::onClosingParenthesis(QChar, int)
|
|
|
|
|
{
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-10-08 14:57:18 +02:00
|
|
|
|
2010-01-19 12:54:17 +01:00
|
|
|
void QScriptHighlighter::highlightWhitespace(const Token &token, const QString &text, int nonWhitespaceFormat)
|
2009-10-08 14:57:18 +02:00
|
|
|
{
|
|
|
|
|
const QTextCharFormat normalFormat = m_formats[nonWhitespaceFormat];
|
|
|
|
|
const QTextCharFormat visualSpaceFormat = m_formats[VisualWhitespace];
|
|
|
|
|
|
|
|
|
|
const int end = token.end();
|
|
|
|
|
int index = token.offset;
|
|
|
|
|
|
|
|
|
|
while (index != end) {
|
|
|
|
|
const bool isSpace = text.at(index).isSpace();
|
|
|
|
|
const int start = index;
|
|
|
|
|
|
|
|
|
|
do { ++index; }
|
|
|
|
|
while (index != end && text.at(index).isSpace() == isSpace);
|
|
|
|
|
|
|
|
|
|
const int tokenLength = index - start;
|
|
|
|
|
setFormat(start, tokenLength, isSpace ? visualSpaceFormat : normalFormat);
|
|
|
|
|
}
|
|
|
|
|
}
|