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: Christian Stenger <christian.stenger@digia.com>
This commit is contained in:
hjk
2014-08-27 13:38:02 +02:00
parent 2a5c602341
commit 251a1d2588
17 changed files with 58 additions and 131 deletions

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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

View File

@@ -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

View File

@@ -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);
}