Commit 251a1d25 authored by hjk's avatar hjk

TextEditor: Clean up/streamline SyntaxHighlighter setup

No need for the third construction way if that's not
really used by the factories anyway.

Change-Id: Id3b34da5b0320babae9bef96a79bbaa52e0db06d
Reviewed-by: default avatarChristian Stenger <christian.stenger@digia.com>
parent 2a5c6023
......@@ -290,9 +290,9 @@ void GitEditorWidget::init()
VcsBase::VcsBaseEditorWidget::init();
Core::Id editorId = editor()->document()->id();
if (editorId == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID)
new GitSubmitHighlighter(textDocument());
textDocument()->setSyntaxHighlighter(new GitSubmitHighlighter);
else if (editorId == Git::Constants::GIT_REBASE_EDITOR_ID)
new GitRebaseHighlighter(textDocument());
textDocument()->setSyntaxHighlighter(new GitRebaseHighlighter);
}
void GitEditorWidget::addDiffActions(QMenu *menu, const VcsBase::DiffChunk &chunk)
......
......@@ -40,17 +40,6 @@ static const char CHANGE_PATTERN[] = "\\b[a-f0-9]{7,40}\\b";
GitSubmitHighlighter::GitSubmitHighlighter(QTextEdit * parent) :
TextEditor::SyntaxHighlighter(parent)
{
initialize();
}
GitSubmitHighlighter::GitSubmitHighlighter(TextEditor::BaseTextDocument *parent) :
TextEditor::SyntaxHighlighter(parent)
{
initialize();
}
void GitSubmitHighlighter::initialize()
{
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty())
......@@ -108,7 +97,7 @@ GitRebaseHighlighter::RebaseAction::RebaseAction(const QString &regexp,
{
}
GitRebaseHighlighter::GitRebaseHighlighter(TextEditor::BaseTextDocument *parent) :
GitRebaseHighlighter::GitRebaseHighlighter(QTextDocument *parent) :
TextEditor::SyntaxHighlighter(parent),
m_hashChar(QLatin1Char('#')),
m_changeNumberPattern(QLatin1String(CHANGE_PATTERN))
......
......@@ -32,8 +32,6 @@
#include <texteditor/syntaxhighlighter.h>
namespace TextEditor { class FontSettings; }
namespace Git {
namespace Internal {
......@@ -55,13 +53,10 @@ enum Format {
class GitSubmitHighlighter : public TextEditor::SyntaxHighlighter
{
public:
explicit GitSubmitHighlighter(QTextEdit *parent);
explicit GitSubmitHighlighter(TextEditor::BaseTextDocument *parent);
explicit GitSubmitHighlighter(QTextEdit *parent = 0);
void highlightBlock(const QString &text);
private:
void initialize();
enum State { None = -1, Header, Other };
QRegExp m_keywordPattern;
QChar m_hashChar;
......@@ -72,7 +67,7 @@ private:
class GitRebaseHighlighter : public TextEditor::SyntaxHighlighter
{
public:
explicit GitRebaseHighlighter(TextEditor::BaseTextDocument *parent);
explicit GitRebaseHighlighter(QTextDocument *parent = 0);
void highlightBlock(const QString &text);
private:
......
......@@ -374,7 +374,7 @@ GlslEditorFactory::GlslEditorFactory()
setEditorWidgetCreator([]() { return new GlslEditorWidget; });
setEditorCreator([]() { return new GlslEditor; });
setIndenterCreator([]() { return new GlslIndenter; });
setSyntaxHighlighterCreator([]() { return new Highlighter; });
setSyntaxHighlighterCreator([]() { return new GlslHighlighter; });
setEditorActionHandlers(Constants::C_GLSLEDITOR_ID,
TextEditorActionHandler::Format
......
......@@ -212,7 +212,7 @@ bool GlslEditorPlugin::initialize(const QStringList & /*arguments*/, QString *er
addAutoReleasedObject(wizard);
auto hf = new HighlighterFactory;
hf->setProductType<Highlighter>();
hf->setProductType<GlslHighlighter>();
hf->setId(Constants::C_GLSLEDITOR_ID);
hf->addMimeType(Constants::GLSL_MIMETYPE);
hf->addMimeType(Constants::GLSL_MIMETYPE_VERT);
......
......@@ -35,23 +35,12 @@
#include <QDebug>
using namespace GlslEditor;
using namespace GlslEditor::Internal;
using namespace TextEditor;
Highlighter::Highlighter(QTextDocument *parent)
: TextEditor::SyntaxHighlighter(parent)
{
init();
}
Highlighter::Highlighter(BaseTextDocument *parent)
: TextEditor::SyntaxHighlighter(parent)
{
init();
}
namespace GlslEditor {
namespace Internal {
void Highlighter::init()
GlslHighlighter::GlslHighlighter()
{
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
......@@ -71,12 +60,7 @@ void Highlighter::init()
setTextFormatCategories(categories);
}
Highlighter::~Highlighter()
{
}
void Highlighter::highlightBlock(const QString &text)
void GlslHighlighter::highlightBlock(const QString &text)
{
const int previousState = previousBlockState();
int state = 0, initialBraceDepth = 0;
......@@ -265,7 +249,7 @@ void Highlighter::highlightBlock(const QString &text)
setCurrentBlockState((braceDepth << 8) | lex.state());
}
void Highlighter::highlightLine(const QString &text, int position, int length,
void GlslHighlighter::highlightLine(const QString &text, int position, int length,
const QTextCharFormat &format)
{
const QTextCharFormat visualSpaceFormat = formatForCategory(GLSLVisualWhitespace);
......@@ -288,7 +272,7 @@ void Highlighter::highlightLine(const QString &text, int position, int length,
}
}
bool Highlighter::isPPKeyword(const QStringRef &text) const
bool GlslHighlighter::isPPKeyword(const QStringRef &text) const
{
switch (text.length())
{
......@@ -427,3 +411,6 @@ void Highlighter::highlightBlock(const QString &text)
setCurrentBlockState((braceDepth << 8) | lex.state());
}
#endif
} // namespace Internal
} // namespace GlslEditor
......@@ -26,6 +26,7 @@
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef GLSLHIGHLIGHTER_H
#define GLSLHIGHLIGHTER_H
......@@ -34,9 +35,7 @@
namespace GlslEditor {
namespace Internal {
class GlslEditorWidget;
class Highlighter : public TextEditor::SyntaxHighlighter
class GlslHighlighter : public TextEditor::SyntaxHighlighter
{
Q_OBJECT
......@@ -57,17 +56,12 @@ public:
NumGLSLFormats
};
explicit Highlighter(QTextDocument *parent = 0);
explicit Highlighter(TextEditor::BaseTextDocument *parent);
virtual ~Highlighter();
GlslHighlighter();
protected:
void highlightBlock(const QString &text);
void highlightLine(const QString &text, int position, int length, const QTextCharFormat &format);
bool isPPKeyword(const QStringRef &text) const;
private:
void init();
};
} // namespace Internal
......
......@@ -44,8 +44,7 @@
#include <texteditor/texteditorconstants.h>
namespace PythonEditor {
using namespace PythonEditor::Internal;
namespace Internal {
/**
* @class PyEditor::Highlighter
......@@ -65,20 +64,7 @@ using namespace PythonEditor::Internal;
* @endcode
*/
PythonHighlighter::PythonHighlighter(QTextDocument *parent) :
TextEditor::SyntaxHighlighter(parent)
{
init();
}
/// New instance created when opening any document in editor
PythonHighlighter::PythonHighlighter(TextEditor::BaseTextDocument *parent) :
TextEditor::SyntaxHighlighter(parent)
{
init();
}
void PythonHighlighter::init()
PythonHighlighter::PythonHighlighter()
{
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
......@@ -98,11 +84,6 @@ void PythonHighlighter::init()
setTextFormatCategories(categories);
}
/// Instance destroyed when one of documents closed from editor
PythonHighlighter::~PythonHighlighter()
{
}
/**
* @brief Highlighter::highlightBlock highlights single line of Python code
* @param text is single line without EOLN symbol. Access to all block data
......@@ -123,11 +104,9 @@ void PythonHighlighter::highlightBlock(const QString &text)
/**
* @return True if this keyword is acceptable at start of import line
*/
static inline
bool isImportKeyword(const QString &keyword)
static bool isImportKeyword(const QString &keyword)
{
return (keyword == QLatin1String("import")
|| keyword == QLatin1String("from"));
return keyword == QLatin1String("import") || keyword == QLatin1String("from");
}
/**
......@@ -175,4 +154,5 @@ void PythonHighlighter::highlightImport(Scanner &scanner)
}
}
} // namespace Internal
} // namespace PythonEditor
......@@ -33,26 +33,24 @@
#include <texteditor/syntaxhighlighter.h>
namespace PythonEditor {
namespace Internal {
namespace Internal { class Scanner; }
class Scanner;
class PythonHighlighter : public TextEditor::SyntaxHighlighter
{
Q_OBJECT
public:
explicit PythonHighlighter(QTextDocument *parent = 0);
explicit PythonHighlighter(TextEditor::BaseTextDocument *parent);
virtual ~PythonHighlighter();
PythonHighlighter();
protected:
virtual void highlightBlock(const QString &text);
void highlightBlock(const QString &text);
private:
int highlightLine(const QString &text, int initialState);
void highlightImport(Internal::Scanner &scanner);
void init();
};
} // namespace Internal
} // namespace PythonEditor
#endif // PYTHONHIGHLIGHTER_H
......@@ -538,7 +538,7 @@ QmlJSEditorDocument::QmlJSEditorDocument()
setId(Constants::C_QMLJSEDITOR_ID);
connect(this, SIGNAL(tabSettingsChanged()),
d, SLOT(invalidateFormatterCache()));
setSyntaxHighlighter(new Highlighter(document()));
setSyntaxHighlighter(new QmlJSHighlighter(document()));
setIndenter(new Internal::Indenter);
}
......
......@@ -113,7 +113,7 @@ bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *e
addAutoReleasedObject(new QmlJSSnippetProvider);
auto hf = new TextEditor::HighlighterFactory;
hf->setProductType<Highlighter>();
hf->setProductType<QmlJSHighlighter>();
hf->setId(Constants::C_QMLJSEDITOR_ID);
hf->addMimeType(QmlJSTools::Constants::QML_MIMETYPE);
hf->addMimeType(QmlJSTools::Constants::QMLPROJECT_MIMETYPE);
......
......@@ -33,11 +33,13 @@
#include <utils/qtcassert.h>
using namespace QmlJSEditor;
using namespace QmlJS;
using namespace TextEditor;
Highlighter::Highlighter(QTextDocument *parent)
: TextEditor::SyntaxHighlighter(parent),
namespace QmlJSEditor {
QmlJSHighlighter::QmlJSHighlighter(QTextDocument *parent)
: SyntaxHighlighter(parent),
m_qmlEnabled(true),
m_braceDepth(0),
m_foldingIndent(0),
......@@ -57,21 +59,21 @@ Highlighter::Highlighter(QTextDocument *parent)
setTextFormatCategories(categories);
}
Highlighter::~Highlighter()
QmlJSHighlighter::~QmlJSHighlighter()
{
}
bool Highlighter::isQmlEnabled() const
bool QmlJSHighlighter::isQmlEnabled() const
{
return m_qmlEnabled;
}
void Highlighter::setQmlEnabled(bool qmlEnabled)
void QmlJSHighlighter::setQmlEnabled(bool qmlEnabled)
{
m_qmlEnabled = qmlEnabled;
}
void Highlighter::highlightBlock(const QString &text)
void QmlJSHighlighter::highlightBlock(const QString &text)
{
const QList<Token> tokens = m_scanner(text, onBlockStart());
......@@ -200,7 +202,7 @@ void Highlighter::highlightBlock(const QString &text)
onBlockEnd(m_scanner.state());
}
bool Highlighter::maybeQmlKeyword(const QStringRef &text) const
bool QmlJSHighlighter::maybeQmlKeyword(const QStringRef &text) const
{
if (text.isEmpty())
return false;
......@@ -224,7 +226,7 @@ bool Highlighter::maybeQmlKeyword(const QStringRef &text) const
return false;
}
bool Highlighter::maybeQmlBuiltinType(const QStringRef &text) const
bool QmlJSHighlighter::maybeQmlBuiltinType(const QStringRef &text) const
{
if (text.isEmpty())
return false;
......@@ -281,13 +283,13 @@ bool Highlighter::maybeQmlBuiltinType(const QStringRef &text) const
return false;
}
int Highlighter::onBlockStart()
int QmlJSHighlighter::onBlockStart()
{
m_currentBlockParentheses.clear();
m_braceDepth = 0;
m_foldingIndent = 0;
m_inMultilineComment = false;
if (TextEditor::TextBlockUserData *userData = TextEditor::BaseTextDocumentLayout::testUserData(currentBlock())) {
if (TextBlockUserData *userData = BaseTextDocumentLayout::testUserData(currentBlock())) {
userData->setFoldingIndent(0);
userData->setFoldingStartIncluded(false);
userData->setFoldingEndIncluded(false);
......@@ -305,34 +307,35 @@ int Highlighter::onBlockStart()
return state;
}
void Highlighter::onBlockEnd(int state)
void QmlJSHighlighter::onBlockEnd(int state)
{
setCurrentBlockState((m_braceDepth << 8) | state);
TextEditor::BaseTextDocumentLayout::setParentheses(currentBlock(), m_currentBlockParentheses);
TextEditor::BaseTextDocumentLayout::setFoldingIndent(currentBlock(), m_foldingIndent);
BaseTextDocumentLayout::setParentheses(currentBlock(), m_currentBlockParentheses);
BaseTextDocumentLayout::setFoldingIndent(currentBlock(), m_foldingIndent);
}
void Highlighter::onOpeningParenthesis(QChar parenthesis, int pos, bool atStart)
void QmlJSHighlighter::onOpeningParenthesis(QChar parenthesis, int pos, bool atStart)
{
if (parenthesis == QLatin1Char('{') || parenthesis == QLatin1Char('[') || parenthesis == QLatin1Char('+')) {
++m_braceDepth;
// if a folding block opens at the beginning of a line, treat the entire line
// as if it were inside the folding block
if (atStart)
TextEditor::BaseTextDocumentLayout::userData(currentBlock())->setFoldingStartIncluded(true);
BaseTextDocumentLayout::userData(currentBlock())->setFoldingStartIncluded(true);
}
m_currentBlockParentheses.push_back(Parenthesis(Parenthesis::Opened, parenthesis, pos));
}
void Highlighter::onClosingParenthesis(QChar parenthesis, int pos, bool atEnd)
void QmlJSHighlighter::onClosingParenthesis(QChar parenthesis, int pos, bool atEnd)
{
if (parenthesis == QLatin1Char('}') || parenthesis == QLatin1Char(']') || parenthesis == QLatin1Char('-')) {
--m_braceDepth;
if (atEnd)
TextEditor::BaseTextDocumentLayout::userData(currentBlock())->setFoldingEndIncluded(true);
BaseTextDocumentLayout::userData(currentBlock())->setFoldingEndIncluded(true);
else
m_foldingIndent = qMin(m_braceDepth, m_foldingIndent); // folding indent is the minimum brace depth of a block
}
m_currentBlockParentheses.push_back(Parenthesis(Parenthesis::Closed, parenthesis, pos));
}
} // namespace QmlJSEditor
......@@ -39,13 +39,13 @@
namespace QmlJSEditor {
class QMLJSEDITOR_EXPORT Highlighter : public TextEditor::SyntaxHighlighter
class QMLJSEDITOR_EXPORT QmlJSHighlighter : public TextEditor::SyntaxHighlighter
{
Q_OBJECT
public:
Highlighter(QTextDocument *parent = 0);
virtual ~Highlighter();
QmlJSHighlighter(QTextDocument *parent = 0);
virtual ~QmlJSHighlighter();
enum {
NumberFormat,
......@@ -76,16 +76,13 @@ protected:
bool maybeQmlBuiltinType(const QStringRef &text) const;
private:
typedef TextEditor::Parenthesis Parenthesis;
typedef TextEditor::Parentheses Parentheses;
bool m_qmlEnabled;
int m_braceDepth;
int m_foldingIndent;
bool m_inMultilineComment;
QmlJS::Scanner m_scanner;
Parentheses m_currentBlockParentheses;
TextEditor::Parentheses m_currentBlockParentheses;
};
} // namespace QmlJSEditor
......
......@@ -64,7 +64,7 @@ QString QmlJSSnippetProvider::displayName() const
void QmlJSSnippetProvider::decorateEditor(TextEditor::SnippetEditorWidget *editor) const
{
editor->textDocument()->setSyntaxHighlighter(new Highlighter);
editor->textDocument()->setSyntaxHighlighter(new QmlJSHighlighter);
editor->textDocument()->setIndenter(new Indenter);
editor->setAutoCompleter(new AutoCompleter);
}
......@@ -282,13 +282,6 @@ SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent)
setDocument(parent);
}
SyntaxHighlighter::SyntaxHighlighter(BaseTextDocument *parent)
: d_ptr(new SyntaxHighlighterPrivate)
{
d_ptr->q_ptr = this;
parent->setSyntaxHighlighter(this); // Extra logic (including setting the parent).
}
/*!
Constructs a SyntaxHighlighter and installs it on \a parent's
QTextDocument. The specified QTextEdit also becomes the owner of
......
......@@ -47,7 +47,6 @@ QT_END_NAMESPACE
namespace TextEditor {
class BaseTextDocument;
class FontSettings;
class SyntaxHighlighterPrivate;
......@@ -56,9 +55,8 @@ class TEXTEDITOR_EXPORT SyntaxHighlighter : public QObject
Q_OBJECT
Q_DECLARE_PRIVATE(SyntaxHighlighter)
public:
SyntaxHighlighter(QObject *parent);
SyntaxHighlighter(QObject *parent = 0);
SyntaxHighlighter(QTextDocument *parent);
SyntaxHighlighter(BaseTextDocument *parent);
SyntaxHighlighter(QTextEdit *parent);
virtual ~SyntaxHighlighter();
......
......@@ -34,13 +34,6 @@
#include <QList>
namespace TextEditor {
namespace Internal {
class Context;
class HighlightDefinition;
}
}
struct HighlightSequence
{
HighlightSequence() {}
......
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