Commit 8fb82ef7 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Merge qmljshighlighter.* with qmlhighlighter.*.

parent 3d34ce42
......@@ -6,8 +6,6 @@ contains(CONFIG, dll) {
include(parser/parser.pri)
DEFINES += QSCRIPTHIGHLIGHTER_BUILD_LIB
DEPENDPATH += $$PWD
INCLUDEPATH += $$PWD/..
......@@ -36,6 +34,6 @@ contains(QT_CONFIG, declarative) {
}
contains(QT, gui) {
SOURCES += $$PWD/qmljshighlighter.cpp $$PWD/qmljsindenter.cpp
HEADERS += $$PWD/qmljshighlighter.h $$PWD/qmljsindenter.h
SOURCES += $$PWD/qmljsindenter.cpp
HEADERS += $$PWD/qmljsindenter.h
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "qmlhighlighter.h"
#include <utils/qtcassert.h>
using namespace QmlJSEditor;
using namespace QmlJSEditor::Internal;
QmlHighlighter::QmlHighlighter(QTextDocument *parent) :
QmlJS::QScriptHighlighter(true, parent)
{
m_currentBlockParentheses.reserve(20);
m_braceDepth = 0;
}
int QmlHighlighter::onBlockStart()
{
m_currentBlockParentheses.clear();
m_braceDepth = 0;
int state = 0;
int previousState = previousBlockState();
if (previousState != -1) {
state = previousState & 0xff;
m_braceDepth = previousState >> 8;
}
return state;
}
void QmlHighlighter::onOpeningParenthesis(QChar parenthesis, int pos)
{
if (parenthesis == QLatin1Char('{')
|| parenthesis == QLatin1Char('['))
++m_braceDepth;
m_currentBlockParentheses.push_back(Parenthesis(Parenthesis::Opened, parenthesis, pos));
}
void QmlHighlighter::onClosingParenthesis(QChar parenthesis, int pos)
{
if (parenthesis == QLatin1Char('}')
|| parenthesis == QLatin1Char(']'))
--m_braceDepth;
m_currentBlockParentheses.push_back(Parenthesis(Parenthesis::Closed, parenthesis, pos));
}
void QmlHighlighter::onBlockEnd(int state, int firstNonSpace)
{
typedef TextEditor::TextBlockUserData TextEditorBlockData;
setCurrentBlockState((m_braceDepth << 8) | state);
// Set block data parentheses. Force creation of block data unless empty
TextEditorBlockData *blockData = 0;
if (QTextBlockUserData *userData = currentBlockUserData())
blockData = static_cast<TextEditorBlockData *>(userData);
if (!blockData && !m_currentBlockParentheses.empty()) {
blockData = new TextEditorBlockData;
setCurrentBlockUserData(blockData);
}
if (blockData) {
blockData->setParentheses(m_currentBlockParentheses);
blockData->setClosingCollapseMode(TextEditor::TextBlockUserData::NoClosingCollapse);
blockData->setCollapseMode(TextEditor::TextBlockUserData::NoCollapse);
}
if (!m_currentBlockParentheses.isEmpty()) {
QTC_ASSERT(blockData, return);
int collapse = Parenthesis::collapseAtPos(m_currentBlockParentheses);
if (collapse >= 0) {
if (collapse == firstNonSpace)
blockData->setCollapseMode(TextEditor::TextBlockUserData::CollapseThis);
else
blockData->setCollapseMode(TextEditor::TextBlockUserData::CollapseAfter);
}
if (Parenthesis::hasClosingCollapse(m_currentBlockParentheses))
blockData->setClosingCollapseMode(TextEditor::TextBlockUserData::NoClosingCollapse);
}
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef QMLSYNTAXHIGHLIGHTER_H
#define QMLSYNTAXHIGHLIGHTER_H
#include <qmljs/qmljshighlighter.h>
#include <texteditor/basetexteditor.h>
namespace QmlJSEditor {
namespace Internal {
// Highlighter for Scripts that stores
// the parentheses encountered in the block data
// for parentheses matching to work.
class QmlHighlighter : public QmlJS::QScriptHighlighter
{
Q_OBJECT
public:
QmlHighlighter(QTextDocument *parent = 0);
private:
virtual int onBlockStart();
virtual void onOpeningParenthesis(QChar parenthesis, int pos);
virtual void onClosingParenthesis(QChar parenthesis, int pos);
virtual void onBlockEnd(int state, int firstNonSpace);
typedef TextEditor::Parenthesis Parenthesis;
typedef TextEditor::Parentheses Parentheses;
Parentheses m_currentBlockParentheses;
int m_braceDepth;
};
} // namespace Internal
} // namespace QmlJSEditor
#endif // QMLSYNTAXHIGHLIGHTER_H
......@@ -29,7 +29,7 @@
#include "qmljseditor.h"
#include "qmljseditorconstants.h"
#include "qmlhighlighter.h"
#include "qmljshighlighter.h"
#include "qmljseditorplugin.h"
#include "qmlmodelmanager.h"
......@@ -581,7 +581,7 @@ QmlJSTextEditor::QmlJSTextEditor(QWidget *parent) :
connect(this, SIGNAL(textChanged()), this, SLOT(updateDocument()));
connect(this, SIGNAL(textChanged()), this, SLOT(updateUses()));
baseTextDocument()->setSyntaxHighlighter(new QmlHighlighter);
baseTextDocument()->setSyntaxHighlighter(new Highlighter(document()));
m_modelManager = ExtensionSystem::PluginManager::instance()->getObject<QmlModelManagerInterface>();
......@@ -810,29 +810,48 @@ void QmlJSTextEditor::renameIdUnderCursor()
void QmlJSTextEditor::setFontSettings(const TextEditor::FontSettings &fs)
{
TextEditor::BaseTextEditor::setFontSettings(fs);
QmlHighlighter *highlighter = qobject_cast<QmlHighlighter*>(baseTextDocument()->syntaxHighlighter());
Highlighter *highlighter = qobject_cast<Highlighter*>(baseTextDocument()->syntaxHighlighter());
if (!highlighter)
return;
/*
NumberFormat,
StringFormat,
TypeFormat,
KeywordFormat,
LabelFormat,
CommentFormat,
VisualWhitespace,
*/
static QVector<QString> categories;
if (categories.isEmpty()) {
categories << QLatin1String(TextEditor::Constants::C_NUMBER)
<< QLatin1String(TextEditor::Constants::C_STRING)
<< QLatin1String(TextEditor::Constants::C_TYPE)
<< QLatin1String(TextEditor::Constants::C_KEYWORD)
<< QLatin1String(TextEditor::Constants::C_PREPROCESSOR)
<< QLatin1String(TextEditor::Constants::C_LABEL)
<< QLatin1String(TextEditor::Constants::C_COMMENT)
<< QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE);
}
const QVector<QTextCharFormat> formats = fs.toTextCharFormats(categories);
highlighter->setFormats(formats.constBegin(), formats.constEnd());
highlighter->rehighlight();
m_occurrencesFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_OCCURRENCES));
m_occurrencesUnusedFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_OCCURRENCES_UNUSED));
m_occurrencesUnusedFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
m_occurrencesUnusedFormat.setUnderlineColor(m_occurrencesUnusedFormat.foreground().color());
m_occurrencesUnusedFormat.clearForeground();
m_occurrencesUnusedFormat.setToolTip(tr("Unused variable"));
m_occurrenceRenameFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_OCCURRENCES_RENAME));
// only set the background, we do not want to modify foreground properties set by the syntax highlighter or the link
m_occurrencesFormat.clearForeground();
highlighter->setFormats(fs.toTextCharFormats(categories));
highlighter->rehighlight();
m_occurrenceRenameFormat.clearForeground();
}
QString QmlJSTextEditor::wordUnderCursor() const
{
QTextCursor tc = textCursor();
......
......@@ -49,7 +49,7 @@ class QmlModelManagerInterface;
namespace Internal {
class QmlHighlighter;
class Highlighter;
class QmlJSTextEditor;
class QmlJSEditorEditable : public TextEditor::BaseTextEditorEditable
......@@ -180,6 +180,8 @@ private:
QComboBox *m_methodCombo;
QmlModelManagerInterface *m_modelManager;
QTextCharFormat m_occurrencesFormat;
QTextCharFormat m_occurrencesUnusedFormat;
QTextCharFormat m_occurrenceRenameFormat;
SemanticInfo m_semanticInfo;
};
......
......@@ -18,7 +18,7 @@ HEADERS += \
qmljseditorplugin.h \
qmlexpressionundercursor.h \
qmlfilewizard.h \
qmlhighlighter.h \
qmljshighlighter.h \
qmlhoverhandler.h \
qmlmodelmanager.h \
qmlmodelmanagerinterface.h
......@@ -31,7 +31,7 @@ SOURCES += \
qmljseditorplugin.cpp \
qmlexpressionundercursor.cpp \
qmlfilewizard.cpp \
qmlhighlighter.cpp \
qmljshighlighter.cpp \
qmlhoverhandler.cpp \
qmlmodelmanager.cpp \
qmlmodelmanagerinterface.cpp
......
......@@ -29,7 +29,7 @@
#include "qmljseditorplugin.h"
#include "qmlhighlighter.h"
#include "qmljshighlighter.h"
#include "qmljseditor.h"
#include "qmljseditorconstants.h"
#include "qmljseditorfactory.h"
......
......@@ -27,37 +27,41 @@
**
**************************************************************************/
#include <qmljs/qmljshighlighter.h>
#include "qmljshighlighter.h"
#include <QtCore/QSet>
#include <QtCore/QtAlgorithms>
#include <QtCore/QDebug>
#include <utils/qtcassert.h>
using namespace QmlJSEditor;
using namespace QmlJSEditor::Internal;
using namespace QmlJS;
QScriptHighlighter::QScriptHighlighter(bool duiEnabled, QTextDocument *parent):
QSyntaxHighlighter(parent),
m_duiEnabled(duiEnabled)
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent),
m_qmlEnabled(true)
{
m_currentBlockParentheses.reserve(20);
m_braceDepth = 0;
}
Highlighter::~Highlighter()
{
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);
}
bool QScriptHighlighter::isDuiEnabled() const
bool Highlighter::isQmlEnabled() const
{
return m_duiEnabled;
return m_qmlEnabled;
}
QTextCharFormat QScriptHighlighter::labelTextCharFormat() const
void Highlighter::setQmlEnabled(bool qmlEnabled)
{
m_qmlEnabled = qmlEnabled;
}
QTextCharFormat Highlighter::labelTextCharFormat() const
{
return m_formats[LabelFormat];
}
......@@ -77,7 +81,7 @@ static bool checkStartOfBinding(const Token &token)
} // end of switch
}
void QScriptHighlighter::highlightBlock(const QString &text)
void Highlighter::highlightBlock(const QString &text)
{
const QList<Token> tokens = m_scanner(text, onBlockStart());
......@@ -125,7 +129,7 @@ void QScriptHighlighter::highlightBlock(const QString &text)
case Token::Identifier: {
const QStringRef spell = text.midRef(token.offset, token.length);
if (m_duiEnabled && maybeQmlKeyword(spell)) {
if (m_qmlEnabled && maybeQmlKeyword(spell)) {
// check the previous token
if (index == 0 || tokens.at(index - 1).isNot(Token::Dot)) {
if (index + 1 == tokens.size() || tokens.at(index + 1).isNot(Token::Colon)) {
......@@ -133,7 +137,7 @@ void QScriptHighlighter::highlightBlock(const QString &text)
break;
}
}
} else if (m_duiEnabled && index > 0 && maybeQmlBuiltinType(spell)) {
} else if (m_qmlEnabled && index > 0 && maybeQmlBuiltinType(spell)) {
const Token &previousToken = tokens.at(index - 1);
if (previousToken.is(Token::Identifier) && text.at(previousToken.offset) == QLatin1Char('p')
&& text.midRef(previousToken.offset, previousToken.length) == QLatin1String("property")) {
......@@ -219,30 +223,7 @@ void QScriptHighlighter::highlightBlock(const QString &text)
onBlockEnd(m_scanner.state(), firstNonSpace);
}
void QScriptHighlighter::setFormats(const QVector<QTextCharFormat> &s)
{
Q_ASSERT(s.size() == NumFormats);
qCopy(s.constBegin(), s.constEnd(), m_formats);
}
int QScriptHighlighter::onBlockStart()
{
return currentBlockState();
}
void QScriptHighlighter::onBlockEnd(int, int)
{
}
void QScriptHighlighter::onOpeningParenthesis(QChar, int)
{
}
void QScriptHighlighter::onClosingParenthesis(QChar, int)
{
}
bool QScriptHighlighter::maybeQmlKeyword(const QStringRef &text) const
bool Highlighter::maybeQmlKeyword(const QStringRef &text) const
{
if (text.isEmpty())
return false;
......@@ -265,7 +246,7 @@ bool QScriptHighlighter::maybeQmlKeyword(const QStringRef &text) const
}
}
bool QScriptHighlighter::maybeQmlBuiltinType(const QStringRef &text) const
bool Highlighter::maybeQmlBuiltinType(const QStringRef &text) const
{
if (text.isEmpty())
return false;
......@@ -296,3 +277,68 @@ bool QScriptHighlighter::maybeQmlBuiltinType(const QStringRef &text) const
return false;
}
}
int Highlighter::onBlockStart()
{
m_currentBlockParentheses.clear();
m_braceDepth = 0;
int state = 0;
int previousState = previousBlockState();
if (previousState != -1) {
state = previousState & 0xff;
m_braceDepth = previousState >> 8;
}
return state;
}
void Highlighter::onBlockEnd(int state, int firstNonSpace)
{
typedef TextEditor::TextBlockUserData TextEditorBlockData;
setCurrentBlockState((m_braceDepth << 8) | state);
// Set block data parentheses. Force creation of block data unless empty
TextEditorBlockData *blockData = 0;
if (QTextBlockUserData *userData = currentBlockUserData())
blockData = static_cast<TextEditorBlockData *>(userData);
if (!blockData && !m_currentBlockParentheses.empty()) {
blockData = new TextEditorBlockData;
setCurrentBlockUserData(blockData);
}
if (blockData) {
blockData->setParentheses(m_currentBlockParentheses);
blockData->setClosingCollapseMode(TextEditor::TextBlockUserData::NoClosingCollapse);
blockData->setCollapseMode(TextEditor::TextBlockUserData::NoCollapse);
}
if (!m_currentBlockParentheses.isEmpty()) {
QTC_ASSERT(blockData, return);
int collapse = Parenthesis::collapseAtPos(m_currentBlockParentheses);
if (collapse >= 0) {
if (collapse == firstNonSpace)
blockData->setCollapseMode(TextEditor::TextBlockUserData::CollapseThis);
else
blockData->setCollapseMode(TextEditor::TextBlockUserData::CollapseAfter);
}
if (Parenthesis::hasClosingCollapse(m_currentBlockParentheses))
blockData->setClosingCollapseMode(TextEditor::TextBlockUserData::NoClosingCollapse);
}
}
void Highlighter::onOpeningParenthesis(QChar parenthesis, int pos)
{
if (parenthesis == QLatin1Char('{') || parenthesis == QLatin1Char('['))
++m_braceDepth;
m_currentBlockParentheses.push_back(Parenthesis(Parenthesis::Opened, parenthesis, pos));
}
void Highlighter::onClosingParenthesis(QChar parenthesis, int pos)
{
if (parenthesis == QLatin1Char('}') || parenthesis == QLatin1Char(']'))
--m_braceDepth;
m_currentBlockParentheses.push_back(Parenthesis(Parenthesis::Closed, parenthesis, pos));
}
......@@ -36,47 +36,71 @@
#include <QtCore/QSet>
#include <QtGui/QSyntaxHighlighter>
namespace QmlJS {
#include <texteditor/basetexteditor.h>
class QMLJS_EXPORT QScriptHighlighter : public QSyntaxHighlighter
namespace QmlJSEditor {
namespace Internal {
class Highlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
QScriptHighlighter(bool duiEnabled = false, QTextDocument *parent = 0);
virtual void highlightBlock(const QString &text);
enum { NumberFormat, StringFormat, TypeFormat,
KeywordFormat, PreProcessorFormat, LabelFormat, CommentFormat,
VisualWhitespace,
NumFormats };
bool isDuiEnabled() const;
public:
Highlighter(QTextDocument *parent = 0);
virtual ~Highlighter();
enum {
NumberFormat,
StringFormat,
TypeFormat,
KeywordFormat,
LabelFormat,
CommentFormat,
VisualWhitespace,
NumFormats
};
bool isQmlEnabled() const;
void setQmlEnabled(bool duiEnabled);
// MS VC 6 compatible, still.
void setFormats(const QVector<QTextCharFormat> &s);
// Set formats from a sequence of type QTextCharFormat
template <class InputIterator>
void setFormats(InputIterator begin, InputIterator end)
{
qCopy(begin, end, m_formats);
}
QTextCharFormat labelTextCharFormat() const;
protected:
virtual int onBlockStart();
virtual void onBlockEnd(int state, int firstNonSpace);
virtual void highlightBlock(const QString &text);
int onBlockStart();
void onBlockEnd(int state, int firstNonSpace);
// The functions are notified whenever parentheses are encountered.
// Custom behaviour can be added, for example storing info for indenting.
virtual void onOpeningParenthesis(QChar parenthesis, int pos);
virtual void onClosingParenthesis(QChar parenthesis, int pos);
void onOpeningParenthesis(QChar parenthesis, int pos);
void onClosingParenthesis(QChar parenthesis, int pos);
bool maybeQmlKeyword(const QStringRef &text) const;
bool maybeQmlBuiltinType(const QStringRef &text) const;
protected:
QmlJSScanner m_scanner;
private:
typedef TextEditor::Parenthesis Parenthesis;
typedef TextEditor::Parentheses Parentheses;
bool m_qmlEnabled;
int m_braceDepth;
QmlJS::QmlJSScanner m_scanner;
Parentheses m_currentBlockParentheses;
QTextCharFormat m_formats[NumFormats];
bool m_duiEnabled;
};
} // namespace QmlJS
} // end of namespace Internal
} // end of namespace QmlJSEditor
#endif // QSCRIPTSYNTAXHIGHLIGHTER_H
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