Add common interface for text formats inside syntax highlighter

Change-Id: I87f64446161a57aea0896f68e4eafacef791969b
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
jkobus
2013-08-13 12:57:31 +02:00
committed by Jarek Kobus
parent 760aa0f8bc
commit e8801167aa
69 changed files with 489 additions and 531 deletions

View File

@@ -33,9 +33,8 @@ using namespace Bazaar::Internal;
using namespace Bazaar;
BazaarAnnotationHighlighter::BazaarAnnotationHighlighter(const ChangeNumbers &changeNumbers,
const QColor &bg,
QTextDocument *document)
: VcsBase::BaseAnnotationHighlighter(changeNumbers, bg, document),
: VcsBase::BaseAnnotationHighlighter(changeNumbers, document),
m_changeset(QLatin1String(Constants::ANNOTATE_CHANGESET_ID))
{
}

View File

@@ -38,7 +38,7 @@ namespace Internal {
class BazaarAnnotationHighlighter : public VcsBase::BaseAnnotationHighlighter
{
public:
explicit BazaarAnnotationHighlighter(const ChangeNumbers &changeNumbers, const QColor &bg,
explicit BazaarAnnotationHighlighter(const ChangeNumbers &changeNumbers,
QTextDocument *document = 0);
private:

View File

@@ -35,7 +35,6 @@
#include <QRegExp>
#include <QString>
#include <QTextCursor>
#include <QDebug>
#define BZR_CHANGE_PATTERN "[0-9]+"
@@ -107,8 +106,7 @@ QString BazaarEditor::changeUnderCursor(const QTextCursor &cursorIn) const
return QString();
}
VcsBase::BaseAnnotationHighlighter *BazaarEditor::createAnnotationHighlighter(const QSet<QString> &changes,
const QColor &bg) const
VcsBase::BaseAnnotationHighlighter *BazaarEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
{
return new BazaarAnnotationHighlighter(changes, bg);
return new BazaarAnnotationHighlighter(changes);
}

View File

@@ -46,7 +46,7 @@ public:
private:
QSet<QString> annotationChanges() const;
QString changeUnderCursor(const QTextCursor &cursor) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes, const QColor &bg) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const;
mutable QRegExp m_changesetId;
mutable QRegExp m_exactChangesetId;

View File

@@ -34,9 +34,8 @@ using namespace ClearCase;
using namespace ClearCase::Internal;
ClearCaseAnnotationHighlighter::ClearCaseAnnotationHighlighter(const ChangeNumbers &changeNumbers,
const QColor &bg,
QTextDocument *document) :
VcsBase::BaseAnnotationHighlighter(changeNumbers, bg, document),
VcsBase::BaseAnnotationHighlighter(changeNumbers, document),
m_separator(QLatin1Char('|'))
{
}

View File

@@ -41,7 +41,7 @@ class ClearCaseAnnotationHighlighter : public VcsBase::BaseAnnotationHighlighter
{
Q_OBJECT
public:
explicit ClearCaseAnnotationHighlighter(const ChangeNumbers &changeNumbers, const QColor &bg,
explicit ClearCaseAnnotationHighlighter(const ChangeNumbers &changeNumbers,
QTextDocument *document = 0);
private:

View File

@@ -36,7 +36,6 @@
#include <utils/qtcassert.h>
#include <vcsbase/diffhighlighter.h>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QTextBlock>
@@ -94,8 +93,7 @@ QString ClearCaseEditor::changeUnderCursor(const QTextCursor &c) const
return QString();
}
VcsBase::BaseAnnotationHighlighter *ClearCaseEditor::createAnnotationHighlighter(const QSet<QString> &changes,
const QColor &bg) const
VcsBase::BaseAnnotationHighlighter *ClearCaseEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
{
return new ClearCaseAnnotationHighlighter(changes, bg);
return new ClearCaseAnnotationHighlighter(changes);
}

View File

@@ -49,7 +49,7 @@ public:
private:
QSet<QString> annotationChanges() const;
QString changeUnderCursor(const QTextCursor &) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes, const QColor &bg) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const;
QRegExp m_versionNumberPattern;
};

View File

@@ -40,13 +40,13 @@
#include <coreplugin/actionmanager/actionmanager.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditoractionhandler.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>
#include <QFileInfo>
#include <QSharedPointer>
#include <QTextBlock>
using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;
@@ -141,27 +141,6 @@ void CMakeEditorWidget::contextMenuEvent(QContextMenuEvent *e)
showDefaultContextMenu(e, Constants::M_CONTEXT);
}
void CMakeEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
{
TextEditor::BaseTextEditorWidget::setFontSettings(fs);
CMakeHighlighter *highlighter = qobject_cast<CMakeHighlighter*>(baseTextDocument()->syntaxHighlighter());
if (!highlighter)
return;
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_LABEL // variables
<< TextEditor::C_KEYWORD // functions
<< TextEditor::C_COMMENT
<< TextEditor::C_STRING
<< TextEditor::C_VISUAL_WHITESPACE;
}
const QVector<QTextCharFormat> formats = fs.toTextCharFormats(categories);
highlighter->setFormats(formats.constBegin(), formats.constEnd());
highlighter->rehighlight();
}
static bool isValidFileNameChar(const QChar &c)
{
if (c.isLetterOrNumber()

View File

@@ -85,7 +85,6 @@ protected:
public slots:
void unCommentSelection();
void setFontSettings(const TextEditor::FontSettings &);
private:
CMakeEditorFactory *m_factory;

View File

@@ -30,9 +30,7 @@
#include "cmakehighlighter.h"
#include <QRegExp>
#include <QColor>
#include <QTextDocument>
#include <QTextEdit>
using namespace CMakeProjectManager::Internal;
@@ -48,6 +46,15 @@ static bool isVariable(const QByteArray &word)
CMakeHighlighter::CMakeHighlighter(QTextDocument *document) :
TextEditor::SyntaxHighlighter(document)
{
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_LABEL // variables
<< TextEditor::C_KEYWORD // functions
<< TextEditor::C_COMMENT
<< TextEditor::C_STRING
<< TextEditor::C_VISUAL_WHITESPACE;
}
setTextFormatCategories(categories);
}
@@ -62,12 +69,12 @@ void CMakeHighlighter::highlightBlock(const QString &text)
for (i=0; i < text.length(); i++) {
char c = text.at(i).toLatin1();
if (inCommentMode) {
setFormat(i, 1, m_formats[CMakeCommentFormat]);
setFormat(i, 1, formatForCategory(CMakeCommentFormat));
} else {
if (c == '#') {
if (!inStringMode) {
inCommentMode = true;
setFormat(i, 1, m_formats[CMakeCommentFormat]);
setFormat(i, 1, formatForCategory(CMakeCommentFormat));
buf.clear();
} else {
buf += c;
@@ -75,7 +82,7 @@ void CMakeHighlighter::highlightBlock(const QString &text)
} else if (c == '(') {
if (!inStringMode) {
if (!buf.isEmpty())
setFormat(i - buf.length(), buf.length(), m_formats[CMakeFunctionFormat]);
setFormat(i - buf.length(), buf.length(), formatForCategory(CMakeFunctionFormat));
buf.clear();
} else {
buf += c;
@@ -88,10 +95,10 @@ void CMakeHighlighter::highlightBlock(const QString &text)
} else if (c == '\"') {
buf += c;
if (inStringMode) {
setFormat(i + 1 - buf.length(), buf.length(), m_formats[CMakeStringFormat]);
setFormat(i + 1 - buf.length(), buf.length(), formatForCategory(CMakeStringFormat));
buf.clear();
} else {
setFormat(i, 1, m_formats[CMakeStringFormat]);
setFormat(i, 1, formatForCategory(CMakeStringFormat));
}
inStringMode = !inStringMode;
} else if (c == '\\') {
@@ -105,14 +112,14 @@ void CMakeHighlighter::highlightBlock(const QString &text)
}
} else if (c == '$') {
if (inStringMode)
setFormat(i - buf.length(), buf.length(), m_formats[CMakeStringFormat]);
setFormat(i - buf.length(), buf.length(), formatForCategory(CMakeStringFormat));
buf.clear();
buf += c;
setFormat(i, 1, emptyFormat);
} else if (c == '}') {
buf += c;
if (isVariable(buf)) {
setFormat(i + 1 - buf.length(), buf.length(), m_formats[CMakeVariableFormat]);
setFormat(i + 1 - buf.length(), buf.length(), formatForCategory(CMakeVariableFormat));
buf.clear();
}
} else {
@@ -123,12 +130,12 @@ void CMakeHighlighter::highlightBlock(const QString &text)
}
if (inStringMode) {
setFormat(i - buf.length(), buf.length(), m_formats[CMakeStringFormat]);
setFormat(i - buf.length(), buf.length(), formatForCategory(CMakeStringFormat));
setCurrentBlockState(1);
} else {
setCurrentBlockState(0);
}
applyFormatToSpaces(text, m_formats[CMakeVisualWhiteSpaceFormat]);
applyFormatToSpaces(text, formatForCategory(CMakeVisualWhiteSpaceFormat));
}

View File

@@ -31,9 +31,6 @@
#define CMAKEHIGHLIGHTER_H
#include <texteditor/syntaxhighlighter.h>
#include <QtAlgorithms>
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
namespace CMakeProjectManager {
namespace Internal {
@@ -50,21 +47,11 @@ public:
CMakeFunctionFormat,
CMakeCommentFormat,
CMakeStringFormat,
CMakeVisualWhiteSpaceFormat,
NumCMakeFormats
CMakeVisualWhiteSpaceFormat
};
CMakeHighlighter(QTextDocument *document = 0);
virtual void highlightBlock(const QString &text);
// Set formats from a sequence of type QTextCharFormat
template <class InputIterator>
void setFormats(InputIterator begin, InputIterator end) {
qCopy(begin, end, m_formats);
}
private:
QTextCharFormat m_formats[NumCMakeFormats];
};
} // namespace Internal

View File

@@ -1887,14 +1887,10 @@ const Utils::CommentDefinition *CPPEditor::commentDefinition() const
void CPPEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
{
TextEditor::BaseTextEditorWidget::setFontSettings(fs);
CppHighlighter *highlighter
= qobject_cast<CppHighlighter*>(baseTextDocument()->syntaxHighlighter());
TextEditor::SyntaxHighlighter *highlighter = baseTextDocument()->syntaxHighlighter();
if (!highlighter)
return;
const QVector<QTextCharFormat> formats = fs.toTextCharFormats(highlighterFormatCategories());
highlighter->setFormats(formats.constBegin(), formats.constEnd());
m_occurrencesFormat = fs.toTextCharFormat(TextEditor::C_OCCURRENCES);
m_occurrencesUnusedFormat = fs.toTextCharFormat(TextEditor::C_OCCURRENCES_UNUSED);
m_occurrencesUnusedFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);

View File

@@ -28,6 +28,7 @@
****************************************************************************/
#include "cpphighlighter.h"
#include "cppeditorenums.h"
#include <cpptools/cppdoxygen.h>
#include <cpptools/cpptoolsreuse.h>
@@ -37,7 +38,6 @@
#include <cplusplus/Lexer.h>
#include <QTextDocument>
#include <QDebug>
using namespace CppEditor::Internal;
using namespace TextEditor;
@@ -46,6 +46,21 @@ using namespace CPlusPlus;
CppHighlighter::CppHighlighter(QTextDocument *document) :
TextEditor::SyntaxHighlighter(document)
{
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_NUMBER
<< TextEditor::C_STRING
<< TextEditor::C_TYPE
<< TextEditor::C_KEYWORD
<< TextEditor::C_OPERATOR
<< TextEditor::C_PREPROCESSOR
<< TextEditor::C_LABEL
<< TextEditor::C_COMMENT
<< TextEditor::C_DOXYGEN_COMMENT
<< TextEditor::C_DOXYGEN_TAG
<< TextEditor::C_VISUAL_WHITESPACE;
}
setTextFormatCategories(categories);
}
void CppHighlighter::highlightBlock(const QString &text)
@@ -80,11 +95,11 @@ void CppHighlighter::highlightBlock(const QString &text)
BaseTextDocumentLayout::clearParentheses(currentBlock());
if (text.length()) {// the empty line can still contain whitespace
if (initialState == Lexer::State_MultiLineComment)
highlightLine(text, 0, text.length(), m_formats[CppCommentFormat]);
highlightLine(text, 0, text.length(), formatForCategory(CppCommentFormat));
else if (initialState == Lexer::State_MultiLineDoxyComment)
highlightLine(text, 0, text.length(), m_formats[CppDoxygenCommentFormat]);
highlightLine(text, 0, text.length(), formatForCategory(CppDoxygenCommentFormat));
else
setFormat(0, text.length(), m_formats[CppVisualWhitespace]);
setFormat(0, text.length(), formatForCategory(CppVisualWhitespace));
}
BaseTextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent);
return;
@@ -109,7 +124,7 @@ void CppHighlighter::highlightBlock(const QString &text)
}
if (previousTokenEnd != tk.begin())
setFormat(previousTokenEnd, tk.begin() - previousTokenEnd, m_formats[CppVisualWhitespace]);
setFormat(previousTokenEnd, tk.begin() - previousTokenEnd, formatForCategory(CppVisualWhitespace));
if (tk.is(T_LPAREN) || tk.is(T_LBRACE) || tk.is(T_LBRACKET)) {
const QChar c = text.at(tk.begin());
@@ -148,11 +163,11 @@ void CppHighlighter::highlightBlock(const QString &text)
continue;
if (i == 0 && tk.is(T_POUND)) {
highlightLine(text, tk.begin(), tk.length(), m_formats[CppPreprocessorFormat]);
highlightLine(text, tk.begin(), tk.length(), formatForCategory(CppPreprocessorFormat));
expectPreprocessorKeyword = true;
} else if (highlightCurrentWordAsPreprocessor &&
(tk.isKeyword() || tk.is(T_IDENTIFIER)) && isPPKeyword(text.midRef(tk.begin(), tk.length()))) {
setFormat(tk.begin(), tk.length(), m_formats[CppPreprocessorFormat]);
setFormat(tk.begin(), tk.length(), formatForCategory(CppPreprocessorFormat));
const QStringRef ppKeyword = text.midRef(tk.begin(), tk.length());
if (ppKeyword == QLatin1String("error")
|| ppKeyword == QLatin1String("warning")
@@ -161,13 +176,13 @@ void CppHighlighter::highlightBlock(const QString &text)
}
} else if (tk.is(T_NUMERIC_LITERAL)) {
setFormat(tk.begin(), tk.length(), m_formats[CppNumberFormat]);
setFormat(tk.begin(), tk.length(), formatForCategory(CppNumberFormat));
} else if (tk.isStringLiteral() || tk.isCharLiteral()) {
highlightLine(text, tk.begin(), tk.length(), m_formats[CppStringFormat]);
highlightLine(text, tk.begin(), tk.length(), formatForCategory(CppStringFormat));
} else if (tk.isComment()) {
const int startPosition = initialState ? previousTokenEnd : tk.begin();
if (tk.is(T_COMMENT) || tk.is(T_CPP_COMMENT))
highlightLine(text, startPosition, tk.end() - startPosition, m_formats[CppCommentFormat]);
highlightLine(text, startPosition, tk.end() - startPosition, formatForCategory(CppCommentFormat));
else // a doxygen comment
highlightDoxygenComment(text, startPosition, tk.end() - startPosition);
@@ -191,11 +206,11 @@ void CppHighlighter::highlightBlock(const QString &text)
}
} else if (tk.isKeyword() || CppTools::isQtKeyword(text.midRef(tk.begin(), tk.length())) || tk.isObjCAtKeyword()) {
setFormat(tk.begin(), tk.length(), m_formats[CppKeywordFormat]);
setFormat(tk.begin(), tk.length(), formatForCategory(CppKeywordFormat));
} else if (tk.isOperator()) {
setFormat(tk.begin(), tk.length(), m_formats[CppOperatorFormat]);
setFormat(tk.begin(), tk.length(), formatForCategory(CppOperatorFormat));
} else if (i == 0 && tokens.size() > 1 && tk.is(T_IDENTIFIER) && tokens.at(1).is(T_COLON)) {
setFormat(tk.begin(), tk.length(), m_formats[CppLabelFormat]);
setFormat(tk.begin(), tk.length(), formatForCategory(CppLabelFormat));
} else if (tk.is(T_IDENTIFIER)) {
highlightWord(text.midRef(tk.begin(), tk.length()), tk.begin(), tk.length());
}
@@ -204,7 +219,7 @@ void CppHighlighter::highlightBlock(const QString &text)
// mark the trailing white spaces
const int lastTokenEnd = tokens.last().end();
if (text.length() > lastTokenEnd)
highlightLine(text, lastTokenEnd, text.length() - lastTokenEnd, m_formats[CppVisualWhitespace]);
highlightLine(text, lastTokenEnd, text.length() - lastTokenEnd, formatForCategory(CppVisualWhitespace));
if (!initialState && state && !tokens.isEmpty()) {
parentheses.append(Parenthesis(Parenthesis::Opened, QLatin1Char('+'),
@@ -326,7 +341,7 @@ bool CppHighlighter::isPPKeyword(const QStringRef &text) const
void CppHighlighter::highlightLine(const QString &text, int position, int length,
const QTextCharFormat &format)
{
QTextCharFormat visualSpaceFormat = m_formats[CppVisualWhitespace];
QTextCharFormat visualSpaceFormat = formatForCategory(CppVisualWhitespace);
visualSpaceFormat.setBackground(format.background());
const int end = position + length;
@@ -360,7 +375,7 @@ void CppHighlighter::highlightWord(QStringRef word, int position, int length)
return;
}
setFormat(position, length, m_formats[CppTypeFormat]);
setFormat(position, length, formatForCategory(CppTypeFormat));
}
}
}
@@ -372,8 +387,8 @@ void CppHighlighter::highlightDoxygenComment(const QString &text, int position,
const QChar *uc = text.unicode();
const QChar *it = uc + position;
const QTextCharFormat &format = m_formats[CppDoxygenCommentFormat];
const QTextCharFormat &kwFormat = m_formats[CppDoxygenTagFormat];
const QTextCharFormat &format = formatForCategory(CppDoxygenCommentFormat);
const QTextCharFormat &kwFormat = formatForCategory(CppDoxygenTagFormat);
while (!it->isNull()) {
if (it->unicode() == QLatin1Char('\\') ||

View File

@@ -30,8 +30,6 @@
#ifndef CPPHIGHLIGHTER_H
#define CPPHIGHLIGHTER_H
#include "cppeditorenums.h"
#include <texteditor/syntaxhighlighter.h>
#include <QTextCharFormat>
@@ -51,12 +49,6 @@ public:
virtual void highlightBlock(const QString &text);
// Set formats from a sequence of type QTextCharFormat
template <class InputIterator>
void setFormats(InputIterator begin, InputIterator end) {
qCopy(begin, end, m_formats);
}
private:
void highlightWord(QStringRef word, int position, int length);
void highlightLine(const QString &line, int position, int length,
@@ -66,8 +58,6 @@ private:
int length);
bool isPPKeyword(const QStringRef &text) const;
QTextCharFormat m_formats[NumCppFormats];
};
} // namespace Internal

View File

@@ -36,8 +36,6 @@
#include <cpptools/cppqtstyleindenter.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/fontsettings.h>
#include <texteditor/snippets/snippeteditor.h>
#include <QLatin1String>
@@ -65,12 +63,7 @@ QString CppSnippetProvider::displayName() const
void CppSnippetProvider::decorateEditor(TextEditor::SnippetEditorWidget *editor) const
{
CppHighlighter *highlighter = new CppHighlighter;
const TextEditor::FontSettings &fs = TextEditor::TextEditorSettings::instance()->fontSettings();
const QVector<QTextCharFormat> &formats =
fs.toTextCharFormats(CPPEditorWidget::highlighterFormatCategories());
highlighter->setFormats(formats.constBegin(), formats.constEnd());
editor->setSyntaxHighlighter(highlighter);
editor->setSyntaxHighlighter(new CppHighlighter);
editor->setIndenter(new CppTools::CppQtStyleIndenter);
editor->setAutoCompleter(new CppAutoCompleter);
}

View File

@@ -33,9 +33,8 @@ using namespace Cvs;
using namespace Cvs::Internal;
CvsAnnotationHighlighter::CvsAnnotationHighlighter(const ChangeNumbers &changeNumbers,
const QColor &bg,
QTextDocument *document) :
VcsBase::BaseAnnotationHighlighter(changeNumbers, bg, document),
VcsBase::BaseAnnotationHighlighter(changeNumbers, document),
m_blank(QLatin1Char(' '))
{
}

View File

@@ -41,7 +41,7 @@ class CvsAnnotationHighlighter : public VcsBase::BaseAnnotationHighlighter
Q_OBJECT
public:
explicit CvsAnnotationHighlighter(const ChangeNumbers &changeNumbers, const QColor &bg,
explicit CvsAnnotationHighlighter(const ChangeNumbers &changeNumbers,
QTextDocument *document = 0);
private:

View File

@@ -123,10 +123,9 @@ QString CvsEditor::changeUnderCursor(const QTextCursor &c) const
return QString();
}
VcsBase::BaseAnnotationHighlighter *CvsEditor::createAnnotationHighlighter(const QSet<QString> &changes,
const QColor &bg) const
VcsBase::BaseAnnotationHighlighter *CvsEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
{
return new CvsAnnotationHighlighter(changes, bg);
return new CvsAnnotationHighlighter(changes);
}
QStringList CvsEditor::annotationPreviousVersions(const QString &revision) const

View File

@@ -48,7 +48,7 @@ public:
private:
QSet<QString> annotationChanges() const;
QString changeUnderCursor(const QTextCursor &) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes, const QColor &bg) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const;
QStringList annotationPreviousVersions(const QString &revision) const;
mutable QRegExp m_revisionAnnotationPattern;

View File

@@ -35,9 +35,8 @@ namespace Git {
namespace Internal {
GitAnnotationHighlighter::GitAnnotationHighlighter(const ChangeNumbers &changeNumbers,
const QColor &bg,
QTextDocument *document) :
VcsBase::BaseAnnotationHighlighter(changeNumbers, bg, document),
VcsBase::BaseAnnotationHighlighter(changeNumbers, document),
m_blank(QLatin1Char(' '))
{
}

View File

@@ -40,7 +40,7 @@ class GitAnnotationHighlighter : public VcsBase::BaseAnnotationHighlighter
{
Q_OBJECT
public:
explicit GitAnnotationHighlighter(const ChangeNumbers &changeNumbers, const QColor &bg,
explicit GitAnnotationHighlighter(const ChangeNumbers &changeNumbers,
QTextDocument *document = 0);
private:

View File

@@ -107,10 +107,9 @@ QString GitEditor::changeUnderCursor(const QTextCursor &c) const
return QString();
}
VcsBase::BaseAnnotationHighlighter *GitEditor::createAnnotationHighlighter(const QSet<QString> &changes,
const QColor &bg) const
VcsBase::BaseAnnotationHighlighter *GitEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
{
return new GitAnnotationHighlighter(changes, bg);
return new GitAnnotationHighlighter(changes);
}
/* Remove the date specification from annotation, which is tabular:

View File

@@ -63,7 +63,7 @@ private:
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
QSet<QString> annotationChanges() const;
QString changeUnderCursor(const QTextCursor &) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes, const QColor &bg) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const;
QString decorateVersion(const QString &revision) const;
QStringList annotationPreviousVersions(const QString &revision) const;
bool isValidRevision(const QString &revision) const;

View File

@@ -27,9 +27,7 @@
**
****************************************************************************/
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>
#include <utils/qtcassert.h>
@@ -40,13 +38,6 @@ namespace Internal {
static const char CHANGE_PATTERN[] = "\\b[a-f0-9]{7,40}\\b";
// Retrieve the comment char format from the text editor.
static QTextCharFormat commentFormat()
{
const TextEditor::FontSettings settings = TextEditor::TextEditorSettings::instance()->fontSettings();
return settings.toTextCharFormat(TextEditor::C_COMMENT);
}
GitSubmitHighlighter::GitSubmitHighlighter(QTextEdit * parent) :
TextEditor::SyntaxHighlighter(parent)
{
@@ -61,7 +52,11 @@ GitSubmitHighlighter::GitSubmitHighlighter(TextEditor::BaseTextDocument *parent)
void GitSubmitHighlighter::initialize()
{
m_commentFormat = commentFormat();
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty())
categories << TextEditor::C_COMMENT;
setTextFormatCategories(categories);
m_keywordPattern.setPattern(QLatin1String("^[\\w-]+:"));
m_hashChar = QLatin1Char('#');
QTC_CHECK(m_keywordPattern.isValid());
@@ -77,7 +72,7 @@ void GitSubmitHighlighter::highlightBlock(const QString &text)
setCurrentBlockState(state);
return;
} else if (text.startsWith(m_hashChar)) {
setFormat(0, text.size(), m_commentFormat);
setFormat(0, text.size(), formatForCategory(Format_Comment));
setCurrentBlockState(state);
return;
} else if (state == None) {
@@ -107,11 +102,10 @@ void GitSubmitHighlighter::highlightBlock(const QString &text)
}
GitRebaseHighlighter::RebaseAction::RebaseAction(const QString &regexp,
const TextEditor::FontSettings &settings,
TextEditor::TextStyle category)
: exp(regexp)
const Format formatCategory)
: exp(regexp),
formatCategory(formatCategory)
{
format = settings.toTextCharFormat(category);
}
GitRebaseHighlighter::GitRebaseHighlighter(TextEditor::BaseTextDocument *parent) :
@@ -119,26 +113,36 @@ GitRebaseHighlighter::GitRebaseHighlighter(TextEditor::BaseTextDocument *parent)
m_hashChar(QLatin1Char('#')),
m_changeNumberPattern(QLatin1String(CHANGE_PATTERN))
{
const TextEditor::FontSettings settings = TextEditor::TextEditorSettings::instance()->fontSettings();
m_commentFormat = settings.toTextCharFormat(TextEditor::C_COMMENT);
m_changeFormat = settings.toTextCharFormat(TextEditor::C_DOXYGEN_COMMENT);
m_descFormat = settings.toTextCharFormat(TextEditor::C_STRING);
m_actions << RebaseAction(QLatin1String("^(p|pick)\\b"), settings, TextEditor::C_KEYWORD);
m_actions << RebaseAction(QLatin1String("^(r|reword)\\b"), settings, TextEditor::C_FIELD);
m_actions << RebaseAction(QLatin1String("^(e|edit)\\b"), settings, TextEditor::C_TYPE);
m_actions << RebaseAction(QLatin1String("^(s|squash)\\b"), settings, TextEditor::C_ENUMERATION);
m_actions << RebaseAction(QLatin1String("^(f|fixup)\\b"), settings, TextEditor::C_NUMBER);
m_actions << RebaseAction(QLatin1String("^(x|exec)\\b"), settings, TextEditor::C_LABEL);
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_COMMENT
<< TextEditor::C_DOXYGEN_COMMENT
<< TextEditor::C_STRING
<< TextEditor::C_KEYWORD
<< TextEditor::C_FIELD
<< TextEditor::C_TYPE
<< TextEditor::C_ENUMERATION
<< TextEditor::C_NUMBER
<< TextEditor::C_LABEL;
}
setTextFormatCategories(categories);
m_actions << RebaseAction(QLatin1String("^(p|pick)\\b"), Format_Pick);
m_actions << RebaseAction(QLatin1String("^(r|reword)\\b"), Format_Reword);
m_actions << RebaseAction(QLatin1String("^(e|edit)\\b"), Format_Edit);
m_actions << RebaseAction(QLatin1String("^(s|squash)\\b"), Format_Squash);
m_actions << RebaseAction(QLatin1String("^(f|fixup)\\b"), Format_Fixup);
m_actions << RebaseAction(QLatin1String("^(x|exec)\\b"), Format_Exec);
}
void GitRebaseHighlighter::highlightBlock(const QString &text)
{
if (text.startsWith(m_hashChar)) {
setFormat(0, text.size(), m_commentFormat);
setFormat(0, text.size(), formatForCategory(Format_Comment));
int changeIndex = 0;
while ((changeIndex = m_changeNumberPattern.indexIn(text, changeIndex)) != -1) {
const int changeLen = m_changeNumberPattern.matchedLength();
setFormat(changeIndex, changeLen, m_changeFormat);
setFormat(changeIndex, changeLen, formatForCategory(Format_Change));
changeIndex += changeLen;
}
return;
@@ -147,13 +151,13 @@ void GitRebaseHighlighter::highlightBlock(const QString &text)
foreach (const RebaseAction &action, m_actions) {
if (action.exp.indexIn(text) != -1) {
const int len = action.exp.matchedLength();
setFormat(0, len, action.format);
setFormat(0, len, formatForCategory(action.formatCategory));
const int changeIndex = m_changeNumberPattern.indexIn(text, len);
if (changeIndex != -1) {
const int changeLen = m_changeNumberPattern.matchedLength();
const int descStart = changeIndex + changeLen + 1;
setFormat(changeIndex, changeLen, m_changeFormat);
setFormat(descStart, text.size() - descStart, m_descFormat);
setFormat(changeIndex, changeLen, formatForCategory(Format_Change));
setFormat(descStart, text.size() - descStart, formatForCategory(Format_Description));
}
break;
}

View File

@@ -31,7 +31,6 @@
#define GITHIGHLIGHTERS_H
#include <texteditor/syntaxhighlighter.h>
#include <texteditor/texteditorconstants.h>
namespace TextEditor {
class FontSettings;
@@ -40,6 +39,18 @@ class FontSettings;
namespace Git {
namespace Internal {
enum Format {
Format_Comment,
Format_Change,
Format_Description,
Format_Pick,
Format_Reword,
Format_Edit,
Format_Squash,
Format_Fixup,
Format_Exec
};
// Highlighter for git submit messages. Make the first line bold, indicates
// comments as such (retrieving the format from the text editor) and marks up
// keywords (words in front of a colon as in 'Task: <bla>').
@@ -50,10 +61,10 @@ public:
explicit GitSubmitHighlighter(TextEditor::BaseTextDocument *parent);
void highlightBlock(const QString &text);
void initialize();
private:
void initialize();
enum State { None = -1, Header, Other };
QTextCharFormat m_commentFormat;
QRegExp m_keywordPattern;
QChar m_hashChar;
};
@@ -71,13 +82,9 @@ private:
{
public:
mutable QRegExp exp;
QTextCharFormat format;
RebaseAction(const QString &regexp, const TextEditor::FontSettings &settings,
TextEditor::TextStyle category);
Format formatCategory;
RebaseAction(const QString &regexp, const Format formatCategory);
};
QTextCharFormat m_commentFormat;
QTextCharFormat m_changeFormat;
QTextCharFormat m_descFormat;
const QChar m_hashChar;
QRegExp m_changeNumberPattern;
QList<RebaseAction> m_actions;

View File

@@ -51,8 +51,6 @@
#include <coreplugin/mimedatabase.h>
#include <extensionsystem/pluginmanager.h>
#include <texteditor/basetextdocument.h>
#include <texteditor/fontsettings.h>
#include <texteditor/tabsettings.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/syntaxhighlighter.h>
@@ -64,7 +62,7 @@
#include <QFileInfo>
#include <QSignalMapper>
#include <QTimer>
#include <QDebug>
#include <QTextBlock>
#include <QMenu>
#include <QComboBox>
@@ -209,42 +207,6 @@ bool GLSLEditorEditable::open(QString *errorString, const QString &fileName, con
return b;
}
void GLSLTextEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
{
TextEditor::BaseTextEditorWidget::setFontSettings(fs);
Highlighter *highlighter = qobject_cast<Highlighter*>(baseTextDocument()->syntaxHighlighter());
if (!highlighter)
return;
/*
NumberFormat,
StringFormat,
TypeFormat,
KeywordFormat,
LabelFormat,
CommentFormat,
VisualWhitespace,
*/
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_NUMBER
<< TextEditor::C_STRING
<< TextEditor::C_TYPE
<< TextEditor::C_KEYWORD
<< TextEditor::C_OPERATOR
<< TextEditor::C_PREPROCESSOR
<< TextEditor::C_LABEL
<< TextEditor::C_COMMENT
<< TextEditor::C_DOXYGEN_COMMENT
<< TextEditor::C_DOXYGEN_TAG
<< TextEditor::C_VISUAL_WHITESPACE
<< TextEditor::C_REMOVED_LINE;
}
highlighter->setFormats(fs.toTextCharFormats(categories));
highlighter->rehighlight();
}
QString GLSLTextEditorWidget::wordUnderCursor() const
{
QTextCursor tc = textCursor();

View File

@@ -109,9 +109,6 @@ public:
TextEditor::IAssistInterface *createAssistInterface(TextEditor::AssistKind assistKind,
TextEditor::AssistReason reason) const;
public slots:
virtual void setFontSettings(const TextEditor::FontSettings &);
private slots:
void updateDocument();
void updateDocumentNow();

View File

@@ -42,6 +42,22 @@ using namespace TextEditor;
Highlighter::Highlighter(BaseTextDocument *parent)
: TextEditor::SyntaxHighlighter(parent)
{
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_NUMBER
<< TextEditor::C_STRING
<< TextEditor::C_TYPE
<< TextEditor::C_KEYWORD
<< TextEditor::C_OPERATOR
<< TextEditor::C_PREPROCESSOR
<< TextEditor::C_LABEL
<< TextEditor::C_COMMENT
<< TextEditor::C_DOXYGEN_COMMENT
<< TextEditor::C_DOXYGEN_TAG
<< TextEditor::C_VISUAL_WHITESPACE
<< TextEditor::C_REMOVED_LINE;
}
setTextFormatCategories(categories);
}
Highlighter::~Highlighter()
@@ -49,11 +65,6 @@ Highlighter::~Highlighter()
}
void Highlighter::setFormats(const QVector<QTextCharFormat> &formats)
{
qCopy(formats.begin(), formats.end(), m_formats);
}
void Highlighter::highlightBlock(const QString &text)
{
const int previousState = previousBlockState();
@@ -96,7 +107,7 @@ void Highlighter::highlightBlock(const QString &text)
setCurrentBlockState(previousState);
BaseTextDocumentLayout::clearParentheses(currentBlock());
if (text.length()) // the empty line can still contain whitespace
setFormat(0, text.length(), m_formats[GLSLVisualWhitespace]);
setFormat(0, text.length(), formatForCategory(GLSLVisualWhitespace));
BaseTextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent);
return;
}
@@ -120,7 +131,7 @@ void Highlighter::highlightBlock(const QString &text)
if (previousTokenEnd != tk.begin()) {
setFormat(previousTokenEnd, tk.begin() - previousTokenEnd,
m_formats[GLSLVisualWhitespace]);
formatForCategory(GLSLVisualWhitespace));
}
if (tk.is(GLSL::Parser::T_LEFT_PAREN) || tk.is(GLSL::Parser::T_LEFT_BRACE) || tk.is(GLSL::Parser::T_LEFT_BRACKET)) {
@@ -157,17 +168,17 @@ void Highlighter::highlightBlock(const QString &text)
highlightAsPreprocessor = false;
if (false /* && i == 0 && tk.is(GLSL::Parser::T_POUND)*/) {
highlightLine(text, tk.begin(), tk.length, m_formats[GLSLPreprocessorFormat]);
highlightLine(text, tk.begin(), tk.length, formatForCategory(GLSLPreprocessorFormat));
highlightAsPreprocessor = true;
} else if (highlightCurrentWordAsPreprocessor && isPPKeyword(text.midRef(tk.begin(), tk.length))) {
setFormat(tk.begin(), tk.length, m_formats[GLSLPreprocessorFormat]);
setFormat(tk.begin(), tk.length, formatForCategory(GLSLPreprocessorFormat));
} else if (tk.is(GLSL::Parser::T_NUMBER)) {
setFormat(tk.begin(), tk.length, m_formats[GLSLNumberFormat]);
setFormat(tk.begin(), tk.length, formatForCategory(GLSLNumberFormat));
} else if (tk.is(GLSL::Parser::T_COMMENT)) {
highlightLine(text, tk.begin(), tk.length, m_formats[GLSLCommentFormat]);
highlightLine(text, tk.begin(), tk.length, formatForCategory(GLSLCommentFormat));
// we need to insert a close comment parenthesis, if
// - the line starts in a C Comment (initalState != 0)
@@ -190,9 +201,9 @@ void Highlighter::highlightBlock(const QString &text)
} else if (tk.is(GLSL::Parser::T_IDENTIFIER)) {
int kind = lex.findKeyword(data.constData() + tk.position, tk.length);
if (kind == GLSL::Parser::T_RESERVED)
setFormat(tk.position, tk.length, m_formats[GLSLReservedKeyword]);
setFormat(tk.position, tk.length, formatForCategory(GLSLReservedKeyword));
else if (kind != GLSL::Parser::T_IDENTIFIER)
setFormat(tk.position, tk.length, m_formats[GLSLKeywordFormat]);
setFormat(tk.position, tk.length, formatForCategory(GLSLKeywordFormat));
}
}
@@ -245,7 +256,7 @@ void Highlighter::highlightBlock(const QString &text)
void Highlighter::highlightLine(const QString &text, int position, int length,
const QTextCharFormat &format)
{
const QTextCharFormat visualSpaceFormat = m_formats[GLSLVisualWhitespace];
const QTextCharFormat visualSpaceFormat = formatForCategory(GLSLVisualWhitespace);
const int end = position + length;
int index = position;
@@ -363,15 +374,15 @@ void Highlighter::highlightBlock(const QString &text)
const GLSL::Token &tk = tokens.at(i);
if (tk.is(GLSL::Parser::T_NUMBER)) {
setFormat(tk.position, tk.length, m_formats[GLSLNumberFormat]);
setFormat(tk.position, tk.length, formatForCategory(GLSLNumberFormat);
} else if (tk.is(GLSL::Parser::T_COMMENT)) {
setFormat(tk.position, tk.length, Qt::darkGreen); // ### FIXME: m_formats[GLSLCommentFormat]);
setFormat(tk.position, tk.length, Qt::darkGreen); // ### FIXME: formatForCategory(GLSLCommentFormat);
} else if (tk.is(GLSL::Parser::T_IDENTIFIER)) {
int kind = lex.findKeyword(data.constData() + tk.position, tk.length);
if (kind == GLSL::Parser::T_RESERVED)
setFormat(tk.position, tk.length, m_formats[GLSLReservedKeyword]);
setFormat(tk.position, tk.length, formatForCategory(GLSLReservedKeyword);
else if (kind != GLSL::Parser::T_IDENTIFIER)
setFormat(tk.position, tk.length, m_formats[GLSLKeywordFormat]);
setFormat(tk.position, tk.length, formatForCategory(GLSLKeywordFormat);
} else if (tk.is(GLSL::Parser::T_LEFT_PAREN) || tk.is(GLSL::Parser::T_LEFT_BRACE) || tk.is(GLSL::Parser::T_LEFT_BRACKET)) {
const QChar c = text.at(tk.begin());
parentheses.append(Parenthesis(Parenthesis::Opened, c, tk.begin()));

View File

@@ -60,15 +60,10 @@ public:
explicit Highlighter(TextEditor::BaseTextDocument *parent);
virtual ~Highlighter();
void setFormats(const QVector<QTextCharFormat> &formats);
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:
QTextCharFormat m_formats[NumGLSLFormats];
};
} // namespace Internal

View File

@@ -34,9 +34,8 @@ using namespace Mercurial::Internal;
using namespace Mercurial;
MercurialAnnotationHighlighter::MercurialAnnotationHighlighter(const ChangeNumbers &changeNumbers,
const QColor &bg,
QTextDocument *document)
: VcsBase::BaseAnnotationHighlighter(changeNumbers, bg, document),
: VcsBase::BaseAnnotationHighlighter(changeNumbers, document),
changeset(QLatin1String(Constants::CHANGESETID12))
{
}

View File

@@ -39,7 +39,7 @@ namespace Internal {
class MercurialAnnotationHighlighter : public VcsBase::BaseAnnotationHighlighter
{
public:
explicit MercurialAnnotationHighlighter(const ChangeNumbers &changeNumbers, const QColor &bg,
explicit MercurialAnnotationHighlighter(const ChangeNumbers &changeNumbers,
QTextDocument *document = 0);
private:

View File

@@ -89,10 +89,9 @@ QString MercurialEditor::changeUnderCursor(const QTextCursor &cursorIn) const
return QString();
}
VcsBase::BaseAnnotationHighlighter *MercurialEditor::createAnnotationHighlighter(const QSet<QString> &changes,
const QColor &bg) const
VcsBase::BaseAnnotationHighlighter *MercurialEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
{
return new MercurialAnnotationHighlighter(changes, bg);
return new MercurialAnnotationHighlighter(changes);
}
QString MercurialEditor::decorateVersion(const QString &revision) const

View File

@@ -46,7 +46,7 @@ public:
private:
QSet<QString> annotationChanges() const;
QString changeUnderCursor(const QTextCursor &cursor) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes, const QColor &bg) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const;
QString decorateVersion(const QString &revision) const;
QStringList annotationPreviousVersions(const QString &revision) const;

View File

@@ -33,9 +33,8 @@ namespace Perforce {
namespace Internal {
PerforceAnnotationHighlighter::PerforceAnnotationHighlighter(const ChangeNumbers &changeNumbers,
const QColor &bg,
QTextDocument *document) :
VcsBase::BaseAnnotationHighlighter(changeNumbers, bg, document),
VcsBase::BaseAnnotationHighlighter(changeNumbers, document),
m_colon(QLatin1Char(':'))
{
}

View File

@@ -41,7 +41,6 @@ class PerforceAnnotationHighlighter : public VcsBase::BaseAnnotationHighlighter
Q_OBJECT
public:
explicit PerforceAnnotationHighlighter(const ChangeNumbers &changeNumbers,
const QColor &bg,
QTextDocument *document = 0);
private:

View File

@@ -109,10 +109,9 @@ QString PerforceEditor::changeUnderCursor(const QTextCursor &c) const
return m_changeNumberPattern.exactMatch(change) ? change : QString();
}
VcsBase::BaseAnnotationHighlighter *PerforceEditor::createAnnotationHighlighter(const QSet<QString> &changes,
const QColor &bg) const
VcsBase::BaseAnnotationHighlighter *PerforceEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
{
return new PerforceAnnotationHighlighter(changes, bg);
return new PerforceAnnotationHighlighter(changes);
}
QString PerforceEditor::findDiffFile(const QString &f) const

View File

@@ -50,7 +50,7 @@ public:
private:
QSet<QString> annotationChanges() const;
QString changeUnderCursor(const QTextCursor &) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes, const QColor &bg) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const;
QString findDiffFile(const QString &f) const;
QStringList annotationPreviousVersions(const QString &v) const;

View File

@@ -40,9 +40,7 @@
#include "pythonhighlighter.h"
#include "lexical/pythonscanner.h"
#include <texteditor/basetextdocumentlayout.h>
#include <texteditor/basetextdocument.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h>
namespace PythonEditor {
@@ -67,30 +65,26 @@ using namespace PythonEditor::Internal;
* @endcode
*/
/// @return List that maps enum Format values to TextEditor plugin formats
QVector<TextEditor::TextStyle> initFormatCategories()
{
QVector<TextEditor::TextStyle> categories(Format_FormatsAmount);
categories[Format_Number] = TextEditor::C_NUMBER;
categories[Format_String] = TextEditor::C_STRING;
categories[Format_Keyword] = TextEditor::C_KEYWORD;
categories[Format_Type] = TextEditor::C_TYPE;
categories[Format_ClassField] = TextEditor::C_FIELD;
categories[Format_MagicAttr] = TextEditor::C_JS_SCOPE_VAR;
categories[Format_Operator] = TextEditor::C_OPERATOR;
categories[Format_Comment] = TextEditor::C_COMMENT;
categories[Format_Doxygen] = TextEditor::C_DOXYGEN_COMMENT;
categories[Format_Whitespace] = TextEditor::C_VISUAL_WHITESPACE;
categories[Format_Identifier] = TextEditor::C_TEXT;
categories[Format_ImportedModule] = TextEditor::C_STRING;
return categories;
}
/// New instance created when opening any document in editor
PythonHighlighter::PythonHighlighter(TextEditor::BaseTextDocument *parent) :
TextEditor::SyntaxHighlighter(parent)
{
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_NUMBER
<< TextEditor::C_STRING
<< TextEditor::C_KEYWORD
<< TextEditor::C_TYPE
<< TextEditor::C_FIELD
<< TextEditor::C_JS_SCOPE_VAR
<< TextEditor::C_OPERATOR
<< TextEditor::C_COMMENT
<< TextEditor::C_DOXYGEN_COMMENT
<< TextEditor::C_TEXT
<< TextEditor::C_VISUAL_WHITESPACE
<< TextEditor::C_STRING;
}
setTextFormatCategories(categories);
}
/// Instance destroyed when one of documents closed from editor
@@ -98,18 +92,6 @@ PythonHighlighter::~PythonHighlighter()
{
}
/**
QtCreator has own fonts&color settings. Highlighter wants get access to
this settings before highlightBlock() called first time.
Settings provided by PyEditor::EditorWidget class.
*/
void PythonHighlighter::setFontSettings(const TextEditor::FontSettings &fs)
{
QVector<TextEditor::TextStyle> categories = initFormatCategories();
m_formats = fs.toTextCharFormats(categories);
rehighlight();
}
/**
* @brief Highlighter::highlightBlock highlights single line of Python code
* @param text is single line without EOLN symbol. Access to all block data
@@ -155,13 +137,13 @@ int PythonHighlighter::highlightLine(const QString &text, int initialState)
if (format == Format_Keyword) {
QString value = scanner.value(tk);
if (isImportKeyword(value) && hasOnlyWhitespace) {
setFormat(tk.begin(), tk.length(), m_formats[format]);
setFormat(tk.begin(), tk.length(), formatForCategory(format));
highlightImport(scanner);
break;
}
}
setFormat(tk.begin(), tk.length(), m_formats[format]);
setFormat(tk.begin(), tk.length(), formatForCategory(format));
if (format != Format_Whitespace)
hasOnlyWhitespace = false;
}
@@ -178,7 +160,7 @@ void PythonHighlighter::highlightImport(Scanner &scanner)
Format format = tk.format();
if (tk.format() == Format_Identifier)
format = Format_ImportedModule;
setFormat(tk.begin(), tk.length(), m_formats[format]);
setFormat(tk.begin(), tk.length(), formatForCategory(format));
}
}

View File

@@ -32,8 +32,6 @@
#include "../pythoneditor_global.h"
#include <texteditor/syntaxhighlighter.h>
#include <texteditor/fontsettings.h>
#include <QMap>
namespace PythonEditor {
@@ -46,15 +44,12 @@ public:
explicit PythonHighlighter(TextEditor::BaseTextDocument *parent);
virtual ~PythonHighlighter();
void setFontSettings(const TextEditor::FontSettings &fs);
protected:
virtual void highlightBlock(const QString &text);
private:
int highlightLine(const QString &text, int initialState);
void highlightImport(Internal::Scanner &scanner);
QVector<QTextCharFormat> m_formats;
};
} // namespace PythonEditor

View File

@@ -980,12 +980,6 @@ void QmlJSTextEditorWidget::updateFileName()
void QmlJSTextEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
{
TextEditor::BaseTextEditorWidget::setFontSettings(fs);
Highlighter *highlighter = qobject_cast<Highlighter*>(baseTextDocument()->syntaxHighlighter());
if (!highlighter)
return;
highlighter->setFormats(fs.toTextCharFormats(highlighterFormatCategories()));
highlighter->rehighlight();
m_occurrencesFormat = fs.toTextCharFormat(TextEditor::C_OCCURRENCES);
m_occurrencesUnusedFormat = fs.toTextCharFormat(TextEditor::C_OCCURRENCES_UNUSED);

View File

@@ -41,11 +41,22 @@ using namespace QmlJS;
Highlighter::Highlighter(QTextDocument *parent)
: TextEditor::SyntaxHighlighter(parent),
m_qmlEnabled(true),
m_braceDepth(0),
m_foldingIndent(0),
m_inMultilineComment(false)
{
m_currentBlockParentheses.reserve(20);
m_braceDepth = 0;
m_foldingIndent = 0;
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_NUMBER
<< TextEditor::C_STRING
<< TextEditor::C_TYPE
<< TextEditor::C_KEYWORD
<< TextEditor::C_FIELD
<< TextEditor::C_COMMENT
<< TextEditor::C_VISUAL_WHITESPACE;
}
setTextFormatCategories(categories);
}
Highlighter::~Highlighter()
@@ -62,12 +73,6 @@ void Highlighter::setQmlEnabled(bool qmlEnabled)
m_qmlEnabled = qmlEnabled;
}
void Highlighter::setFormats(const QVector<QTextCharFormat> &formats)
{
QTC_ASSERT(formats.size() == NumFormats, return);
qCopy(formats.begin(), formats.end(), m_formats);
}
void Highlighter::highlightBlock(const QString &text)
{
const QList<Token> tokens = m_scanner(text, onBlockStart());
@@ -78,11 +83,11 @@ void Highlighter::highlightBlock(const QString &text)
switch (token.kind) {
case Token::Keyword:
setFormat(token.offset, token.length, m_formats[KeywordFormat]);
setFormat(token.offset, token.length, formatForCategory(KeywordFormat));
break;
case Token::String:
setFormat(token.offset, token.length, m_formats[StringFormat]);
setFormat(token.offset, token.length, formatForCategory(StringFormat));
break;
case Token::Comment:
@@ -95,11 +100,11 @@ void Highlighter::highlightBlock(const QString &text)
onOpeningParenthesis(QLatin1Char('+'), token.offset, index == 0);
m_inMultilineComment = true;
}
setFormat(token.offset, token.length, m_formats[CommentFormat]);
setFormat(token.offset, token.length, formatForCategory(CommentFormat));
break;
case Token::RegExp:
setFormat(token.offset, token.length, m_formats[StringFormat]);
setFormat(token.offset, token.length, formatForCategory(StringFormat));
break;
case Token::LeftParenthesis:
@@ -136,7 +141,7 @@ void Highlighter::highlightBlock(const QString &text)
// 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)) {
setFormat(token.offset, token.length, m_formats[KeywordFormat]);
setFormat(token.offset, token.length, formatForCategory(KeywordFormat));
break;
}
}
@@ -144,7 +149,7 @@ void Highlighter::highlightBlock(const QString &text)
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")) {
setFormat(token.offset, token.length, m_formats[KeywordFormat]);
setFormat(token.offset, token.length, formatForCategory(KeywordFormat));
break;
}
}
@@ -163,7 +168,7 @@ void Highlighter::highlightBlock(const QString &text)
int previousTokenEnd = 0;
for (int index = 0; index < tokens.size(); ++index) {
const Token &token = tokens.at(index);
setFormat(previousTokenEnd, token.begin() - previousTokenEnd, m_formats[VisualWhitespace]);
setFormat(previousTokenEnd, token.begin() - previousTokenEnd, formatForCategory(VisualWhitespace));
switch (token.kind) {
case Token::Comment:
@@ -177,7 +182,7 @@ void Highlighter::highlightBlock(const QString &text)
do {
++i;
} while (i < e && text.at(i).isSpace());
setFormat(start, i - start, m_formats[VisualWhitespace]);
setFormat(start, i - start, formatForCategory(VisualWhitespace));
} else {
++i;
}
@@ -191,7 +196,7 @@ void Highlighter::highlightBlock(const QString &text)
previousTokenEnd = token.end();
}
setFormat(previousTokenEnd, text.length() - previousTokenEnd, m_formats[VisualWhitespace]);
setFormat(previousTokenEnd, text.length() - previousTokenEnd, formatForCategory(VisualWhitespace));
setCurrentBlockState(m_scanner.state());
onBlockEnd(m_scanner.state());

View File

@@ -34,13 +34,8 @@
#include <qmljs/qmljsscanner.h>
#include <QVector>
#include <QSet>
#include <QSyntaxHighlighter>
#include <texteditor/basetextdocumentlayout.h>
#include <texteditor/syntaxhighlighter.h>
#include <texteditor/texteditorconstants.h>
namespace QmlJSEditor {
@@ -65,7 +60,6 @@ public:
bool isQmlEnabled() const;
void setQmlEnabled(bool duiEnabled);
void setFormats(const QVector<QTextCharFormat> &formats);
protected:
virtual void highlightBlock(const QString &text);
@@ -92,8 +86,6 @@ private:
QmlJS::Scanner m_scanner;
Parentheses m_currentBlockParentheses;
QTextCharFormat m_formats[NumFormats];
};
} // namespace QmlJSEditor

View File

@@ -34,7 +34,6 @@
#include "qmljseditorconstants.h"
#include <texteditor/texteditorsettings.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/snippets/snippeteditor.h>
@@ -65,10 +64,7 @@ QString QmlJSSnippetProvider::displayName() const
void QmlJSSnippetProvider::decorateEditor(TextEditor::SnippetEditorWidget *editor) const
{
Highlighter *highlighter = new Highlighter;
const TextEditor::FontSettings &fs = TextEditor::TextEditorSettings::instance()->fontSettings();
highlighter->setFormats(fs.toTextCharFormats(QmlJSTextEditorWidget::highlighterFormatCategories()));
editor->setSyntaxHighlighter(highlighter);
editor->setSyntaxHighlighter(new Highlighter);
editor->setIndenter(new Indenter);
editor->setAutoCompleter(new AutoCompleter);
}

View File

@@ -190,25 +190,6 @@ void ProFileEditorWidget::contextMenuEvent(QContextMenuEvent *e)
showDefaultContextMenu(e, Constants::M_CONTEXT);
}
void ProFileEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
{
TextEditor::BaseTextEditorWidget::setFontSettings(fs);
ProFileHighlighter *highlighter = qobject_cast<ProFileHighlighter*>(baseTextDocument()->syntaxHighlighter());
if (!highlighter)
return;
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_TYPE
<< TextEditor::C_KEYWORD
<< TextEditor::C_COMMENT
<< TextEditor::C_VISUAL_WHITESPACE;
}
highlighter->setFormats(fs.toTextCharFormats(categories));
highlighter->rehighlight();
}
//
// ProFileDocument
//

View File

@@ -75,9 +75,6 @@ protected:
TextEditor::BaseTextEditor *createEditor();
void contextMenuEvent(QContextMenuEvent *);
public slots:
virtual void setFontSettings(const TextEditor::FontSettings &);
private:
ProFileEditorFactory *m_factory;
TextEditor::TextEditorActionHandler *m_ah;

View File

@@ -43,6 +43,15 @@ ProFileHighlighter::ProFileHighlighter(QTextDocument *document) :
ProFileCompletionAssistProvider *pcap
= ExtensionSystem::PluginManager::getObject<ProFileCompletionAssistProvider>();
m_keywords = TextEditor::Keywords(pcap->variables(), pcap->functions(), QMap<QString, QStringList>());
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_TYPE
<< TextEditor::C_KEYWORD
<< TextEditor::C_COMMENT
<< TextEditor::C_VISUAL_WHITESPACE;
}
setTextFormatCategories(categories);
}
void ProFileHighlighter::highlightBlock(const QString &text)
@@ -58,26 +67,26 @@ void ProFileHighlighter::highlightBlock(const QString &text)
for (;;) {
const QChar c = text.at(i);
if (inCommentMode) {
setFormat(i, 1, m_formats[ProfileCommentFormat]);
setFormat(i, 1, formatForCategory(ProfileCommentFormat));
} else {
if (c.isLetter() || c == QLatin1Char('_') || c == QLatin1Char('.') || c.isDigit()) {
buf += c;
setFormat(i - buf.length()+1, buf.length(), emptyFormat);
if (!buf.isEmpty() && m_keywords.isFunction(buf))
setFormat(i - buf.length()+1, buf.length(), m_formats[ProfileFunctionFormat]);
setFormat(i - buf.length()+1, buf.length(), formatForCategory(ProfileFunctionFormat));
else if (!buf.isEmpty() && m_keywords.isVariable(buf))
setFormat(i - buf.length()+1, buf.length(), m_formats[ProfileVariableFormat]);
setFormat(i - buf.length()+1, buf.length(), formatForCategory(ProfileVariableFormat));
} else if (c == QLatin1Char('(')) {
if (!buf.isEmpty() && m_keywords.isFunction(buf))
setFormat(i - buf.length(), buf.length(), m_formats[ProfileFunctionFormat]);
setFormat(i - buf.length(), buf.length(), formatForCategory(ProfileFunctionFormat));
buf.clear();
} else if (c == QLatin1Char('#')) {
inCommentMode = true;
setFormat(i, 1, m_formats[ProfileCommentFormat]);
setFormat(i, 1, formatForCategory(ProfileCommentFormat));
buf.clear();
} else {
if (!buf.isEmpty() && m_keywords.isVariable(buf))
setFormat(i - buf.length(), buf.length(), m_formats[ProfileVariableFormat]);
setFormat(i - buf.length(), buf.length(), formatForCategory(ProfileVariableFormat));
buf.clear();
}
}
@@ -86,5 +95,5 @@ void ProFileHighlighter::highlightBlock(const QString &text)
break;
}
applyFormatToSpaces(text, m_formats[ProfileVisualWhitespaceFormat]);
applyFormatToSpaces(text, formatForCategory(ProfileVisualWhitespaceFormat));
}

View File

@@ -51,14 +51,7 @@ public:
ProFileHighlighter(QTextDocument *document = 0);
void highlightBlock(const QString &text);
// Set formats from a sequence of type QTextCharFormat
void setFormats(const QVector<QTextCharFormat> &formats)
{
qCopy(formats.begin(), formats.end(), m_formats);
}
private:
QTextCharFormat m_formats[NumProfileFormats];
TextEditor::Keywords m_keywords;
};

View File

@@ -33,9 +33,8 @@ using namespace Subversion;
using namespace Subversion::Internal;
SubversionAnnotationHighlighter::SubversionAnnotationHighlighter(const ChangeNumbers &changeNumbers,
const QColor &bg,
QTextDocument *document) :
VcsBase::BaseAnnotationHighlighter(changeNumbers, bg, document),
VcsBase::BaseAnnotationHighlighter(changeNumbers, document),
m_blank(QLatin1Char(' '))
{
}

View File

@@ -40,7 +40,7 @@ class SubversionAnnotationHighlighter : public VcsBase::BaseAnnotationHighlighte
{
Q_OBJECT
public:
explicit SubversionAnnotationHighlighter(const ChangeNumbers &changeNumbers, const QColor &bg,
explicit SubversionAnnotationHighlighter(const ChangeNumbers &changeNumbers,
QTextDocument *document = 0);
private:

View File

@@ -109,10 +109,9 @@ QString SubversionEditor::changeUnderCursor(const QTextCursor &c) const
return QString();
}
VcsBase::BaseAnnotationHighlighter *SubversionEditor::createAnnotationHighlighter(const QSet<QString> &changes,
const QColor &bg) const
VcsBase::BaseAnnotationHighlighter *SubversionEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
{
return new SubversionAnnotationHighlighter(changes, bg);
return new SubversionAnnotationHighlighter(changes);
}
QStringList SubversionEditor::annotationPreviousVersions(const QString &v) const

View File

@@ -48,7 +48,7 @@ public:
private:
QSet<QString> annotationChanges() const;
QString changeUnderCursor(const QTextCursor &) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes, const QColor &bg) const;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const;
QStringList annotationPreviousVersions(const QString &) const;
mutable QRegExp m_changeNumberPattern;

View File

@@ -5645,6 +5645,12 @@ void BaseTextEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
slotUpdateExtraAreaWidth(); // Adjust to new font width
updateCurrentLineHighlight(); // Make sure it takes the new color
SyntaxHighlighter *highlighter = baseTextDocument()->syntaxHighlighter();
if (highlighter) {
highlighter->setFontSettings(fs);
highlighter->rehighlight();
}
}
void BaseTextEditorWidget::setTabSettings(const TabSettings &ts)

View File

@@ -60,7 +60,28 @@ Highlighter::Highlighter(QTextDocument *parent) :
m_persistentObservableStatesCounter(PersistentsStart),
m_dynamicContextsCounter(0),
m_isBroken(false)
{}
{
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_TEXT
<< TextEditor::C_VISUAL_WHITESPACE
<< TextEditor::C_KEYWORD
<< TextEditor::C_TYPE
<< TextEditor::C_COMMENT
<< TextEditor::C_NUMBER
<< TextEditor::C_NUMBER
<< TextEditor::C_NUMBER
<< TextEditor::C_STRING
<< TextEditor::C_STRING
<< TextEditor::C_TEXT
<< TextEditor::C_TEXT
<< TextEditor::C_TEXT
<< TextEditor::C_TEXT
<< TextEditor::C_TEXT;
}
setTextFormatCategories(categories);
}
Highlighter::~Highlighter()
{}
@@ -82,19 +103,14 @@ Highlighter::KateFormatMap::KateFormatMap()
m_ids.insert(QLatin1String("dsChar"), Highlighter::Char);
m_ids.insert(QLatin1String("dsString"), Highlighter::String);
m_ids.insert(QLatin1String("dsComment"), Highlighter::Comment);
m_ids.insert(QLatin1String("dsOthers"), Highlighter::Others);
m_ids.insert(QLatin1String("dsAlert"), Highlighter::Alert);
m_ids.insert(QLatin1String("dsError"), Highlighter::Error);
m_ids.insert(QLatin1String("dsFunction"), Highlighter::Function);
m_ids.insert(QLatin1String("dsRegionMarker"), Highlighter::RegionMarker);
m_ids.insert(QLatin1String("dsError"), Highlighter::Error);
m_ids.insert(QLatin1String("dsOthers"), Highlighter::Others);
}
void Highlighter::configureFormat(TextFormatId id, const QTextCharFormat &format)
{
m_creatorFormats[id] = format;
}
void Highlighter::setDefaultContext(const QSharedPointer<Context> &defaultContext)
void Highlighter::setDefaultContext(const QSharedPointer<Context> &defaultContext)
{
m_defaultContext = defaultContext;
m_persistentObservableStates.insert(m_defaultContext->name(), Default);
@@ -140,7 +156,7 @@ void Highlighter::highlightBlock(const QString &text)
}
}
applyFormatToSpaces(text, m_creatorFormats.value(VisualWhitespace));
applyFormatToSpaces(text, formatForCategory(VisualWhitespace));
}
void Highlighter::setupDataForBlock(const QString &text)
@@ -388,34 +404,30 @@ void Highlighter::applyFormat(int offset,
return;
}
TextFormatId formatId = m_kateFormats.m_ids.value(itemData->style());
TextFormatId formatId = m_kateFormats.m_ids.value(itemData->style(), Normal);
if (formatId != Normal) {
QHash<TextFormatId, QTextCharFormat>::const_iterator cit =
m_creatorFormats.constFind(formatId);
if (cit != m_creatorFormats.constEnd()) {
QTextCharFormat format = cit.value();
if (itemData->isCustomized()) {
// Please notice that the following are applied every time for item data which have
// customizations. The configureFormats method could be used to provide a "one time"
// configuration, but it would probably require to traverse all item data from all
// definitions available/loaded (either to set the values or for some "notifying"
// strategy). This is because the highlighter does not really know on which
// definition(s) it is working. Since not many item data specify customizations I
// think this approach would fit better. If there are other ideas...
if (itemData->color().isValid())
format.setForeground(itemData->color());
if (itemData->isItalicSpecified())
format.setFontItalic(itemData->isItalic());
if (itemData->isBoldSpecified())
format.setFontWeight(toFontWeight(itemData->isBold()));
if (itemData->isUnderlinedSpecified())
format.setFontUnderline(itemData->isUnderlined());
if (itemData->isStrikeOutSpecified())
format.setFontStrikeOut(itemData->isStrikeOut());
}
setFormat(offset, count, format);
QTextCharFormat format = formatForCategory(formatId);
if (itemData->isCustomized()) {
// Please notice that the following are applied every time for item data which have
// customizations. The configureFormats method could be used to provide a "one time"
// configuration, but it would probably require to traverse all item data from all
// definitions available/loaded (either to set the values or for some "notifying"
// strategy). This is because the highlighter does not really know on which
// definition(s) it is working. Since not many item data specify customizations I
// think this approach would fit better. If there are other ideas...
if (itemData->color().isValid())
format.setForeground(itemData->color());
if (itemData->isItalicSpecified())
format.setFontItalic(itemData->isItalic());
if (itemData->isBoldSpecified())
format.setFontWeight(toFontWeight(itemData->isBold()));
if (itemData->isUnderlinedSpecified())
format.setFontUnderline(itemData->isUnderlined());
if (itemData->isStrikeOutSpecified())
format.setFontStrikeOut(itemData->isStrikeOut());
}
setFormat(offset, count, format);
}
}

View File

@@ -65,12 +65,12 @@ public:
VisualWhitespace,
Keyword,
DataType,
Comment,
Decimal,
BaseN,
Float,
Char,
String,
Comment,
Alert,
Error,
Function,
@@ -78,7 +78,6 @@ public:
Others
};
void configureFormat(TextFormatId id, const QTextCharFormat &format);
void setTabSettings(const TabSettings &ts);
void setDefaultContext(const QSharedPointer<Context> &defaultContext);
@@ -134,7 +133,6 @@ private:
QHash<QString, TextFormatId> m_ids;
};
static const KateFormatMap m_kateFormats;
QHash<TextFormatId, QTextCharFormat> m_creatorFormats;
struct BlockData : TextBlockUserData
{

View File

@@ -38,15 +38,12 @@
#include "manager.h"
#include "context.h"
#include "normalindenter.h"
#include "fontsettings.h"
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <QSharedPointer>
#include <QDebug>
using namespace Core;
using namespace TextEditor::Internal;
@@ -94,30 +91,6 @@ void PlainTextEditorWidget::unCommentSelection()
Utils::unCommentSelection(this, m_commentDefinition);
}
void PlainTextEditorWidget::setFontSettings(const FontSettings &fs)
{
BaseTextEditorWidget::setFontSettings(fs);
if (baseTextDocument()->syntaxHighlighter()) {
Highlighter *highlighter =
static_cast<Highlighter *>(baseTextDocument()->syntaxHighlighter());
highlighter->configureFormat(Highlighter::VisualWhitespace, fs.toTextCharFormat(C_VISUAL_WHITESPACE));
highlighter->configureFormat(Highlighter::Keyword, fs.toTextCharFormat(C_KEYWORD));
highlighter->configureFormat(Highlighter::DataType, fs.toTextCharFormat(C_TYPE));
highlighter->configureFormat(Highlighter::Comment, fs.toTextCharFormat(C_COMMENT));
// Using C_NUMBER for all kinds of numbers.
highlighter->configureFormat(Highlighter::Decimal, fs.toTextCharFormat(C_NUMBER));
highlighter->configureFormat(Highlighter::BaseN, fs.toTextCharFormat(C_NUMBER));
highlighter->configureFormat(Highlighter::Float, fs.toTextCharFormat(C_NUMBER));
// Using C_STRING for strings and chars.
highlighter->configureFormat(Highlighter::Char, fs.toTextCharFormat(C_STRING));
highlighter->configureFormat(Highlighter::String, fs.toTextCharFormat(C_STRING));
highlighter->rehighlight();
}
}
void PlainTextEditorWidget::setTabSettings(const TextEditor::TabSettings &ts)
{
BaseTextEditorWidget::setTabSettings(ts);

View File

@@ -67,7 +67,6 @@ public:
public slots:
virtual void unCommentSelection();
virtual void setFontSettings(const FontSettings &fs);
virtual void setTabSettings(const TextEditor::TabSettings &);
private slots:

View File

@@ -30,6 +30,10 @@
#include "syntaxhighlighter.h"
#include "basetextdocument.h"
#include "basetextdocumentlayout.h"
#include "texteditorsettings.h"
#include "fontsettings.h"
#include <utils/qtcassert.h>
#include <qtimer.h>
@@ -70,12 +74,15 @@ public:
}
void applyFormatChanges(int from, int charsRemoved, int charsAdded);
void updateFormatsForCategories(const TextEditor::FontSettings &fontSettings);
QVector<QTextCharFormat> formatChanges;
QTextBlock currentBlock;
bool rehighlightPending;
bool inReformatBlocks;
BaseTextDocumentLayout::FoldValidator foldValidator;
QVector<QTextCharFormat> formats;
QVector<TextEditor::TextStyle> formatCategories;
};
static bool adjustRange(QTextLayout::FormatRange &range, int from, int charsRemoved, int charsAdded) {
@@ -784,4 +791,31 @@ QList<QColor> SyntaxHighlighter::generateColors(int n, const QColor &background)
return result;
}
void SyntaxHighlighter::setFontSettings(const TextEditor::FontSettings &fontSettings)
{
Q_D(SyntaxHighlighter);
d->updateFormatsForCategories(fontSettings);
}
void SyntaxHighlighter::setTextFormatCategories(const QVector<TextEditor::TextStyle> &categories)
{
Q_D(SyntaxHighlighter);
d->formatCategories = categories;
d->updateFormatsForCategories(TextEditorSettings::instance()->fontSettings());
}
QTextCharFormat SyntaxHighlighter::formatForCategory(int category) const
{
Q_D(const SyntaxHighlighter);
QTC_ASSERT(d->formats.size() > category, return QTextCharFormat());
return d->formats.at(category);
}
void SyntaxHighlighterPrivate::updateFormatsForCategories(const TextEditor::FontSettings &fontSettings)
{
formats = fontSettings.toTextCharFormats(formatCategories);
}
#include "moc_syntaxhighlighter.cpp"

View File

@@ -31,7 +31,7 @@
#define TEXTEDITOR_SYNTAXHIGHLIGHTER_H
#include "texteditor_global.h"
#include <texteditor/texteditorconstants.h>
#include <QObject>
#include <QTextLayout>
@@ -48,6 +48,7 @@ QT_END_NAMESPACE
namespace TextEditor {
class BaseTextDocument;
class FontSettings;
class SyntaxHighlighterPrivate;
class TEXTEDITOR_EXPORT SyntaxHighlighter : public QObject
@@ -68,11 +69,15 @@ public:
static QList<QColor> generateColors(int n, const QColor &background);
// Don't call in constructors of derived classes
virtual void setFontSettings(const TextEditor::FontSettings &fontSettings);
public Q_SLOTS:
void rehighlight();
void rehighlightBlock(const QTextBlock &block);
protected:
void setTextFormatCategories(const QVector<TextEditor::TextStyle> &categories);
QTextCharFormat formatForCategory(int categoryIndex) const;
virtual void highlightBlock(const QString &text) = 0;
void setFormat(int start, int count, const QTextCharFormat &format);

View File

@@ -28,6 +28,7 @@
****************************************************************************/
#include "baseannotationhighlighter.h"
#include <texteditor/fontsettings.h>
#include <QDebug>
#include <QColor>
@@ -51,34 +52,63 @@ typedef QMap<QString, QTextCharFormat> ChangeNumberFormatMap;
*/
namespace VcsBase {
namespace Internal {
class BaseAnnotationHighlighterPrivate
{
BaseAnnotationHighlighter *q_ptr;
Q_DECLARE_PUBLIC(BaseAnnotationHighlighter)
public:
enum Formats {
BackgroundFormat // C_TEXT
};
BaseAnnotationHighlighterPrivate();
void updateOtherFormats();
ChangeNumberFormatMap m_changeNumberMap;
QColor m_background;
};
} // namespace Internal
BaseAnnotationHighlighterPrivate::BaseAnnotationHighlighterPrivate()
: q_ptr(0)
{
}
void BaseAnnotationHighlighterPrivate::updateOtherFormats()
{
Q_Q(BaseAnnotationHighlighter);
m_background = q->formatForCategory(BackgroundFormat).brushProperty(QTextFormat::BackgroundBrush).color();
q->setChangeNumbers(m_changeNumberMap.keys().toSet());
}
BaseAnnotationHighlighter::BaseAnnotationHighlighter(const ChangeNumbers &changeNumbers,
const QColor &bg,
QTextDocument *document) :
TextEditor::SyntaxHighlighter(document),
d(new Internal::BaseAnnotationHighlighterPrivate)
d_ptr(new BaseAnnotationHighlighterPrivate())
{
d->m_background = bg;
d_ptr->q_ptr = this;
Q_D(BaseAnnotationHighlighter);
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty())
categories << TextEditor::C_TEXT;
setTextFormatCategories(categories);
d->updateOtherFormats();
setChangeNumbers(changeNumbers);
}
BaseAnnotationHighlighter::~BaseAnnotationHighlighter()
{
delete d;
}
void BaseAnnotationHighlighter::setChangeNumbers(const ChangeNumbers &changeNumbers)
{
Q_D(BaseAnnotationHighlighter);
d->m_changeNumberMap.clear();
if (!changeNumbers.isEmpty()) {
// Assign a color gradient to annotation change numbers. Give
@@ -99,6 +129,7 @@ void BaseAnnotationHighlighter::setChangeNumbers(const ChangeNumbers &changeNumb
void BaseAnnotationHighlighter::highlightBlock(const QString &text)
{
Q_D(BaseAnnotationHighlighter);
if (text.isEmpty() || d->m_changeNumberMap.empty())
return;
const QString change = changeNumber(text);
@@ -107,10 +138,11 @@ void BaseAnnotationHighlighter::highlightBlock(const QString &text)
setFormat(0, text.length(), it.value());
}
void BaseAnnotationHighlighter::setBackgroundColor(const QColor &color)
void BaseAnnotationHighlighter::setFontSettings(const TextEditor::FontSettings &fontSettings)
{
d->m_background = color;
setChangeNumbers(d->m_changeNumberMap.keys().toSet());
Q_D(BaseAnnotationHighlighter);
SyntaxHighlighter::setFontSettings(fontSettings);
d->updateOtherFormats();
}
} // namespace VcsBase

View File

@@ -35,17 +35,16 @@
#include <texteditor/syntaxhighlighter.h>
namespace VcsBase {
namespace Internal {
class BaseAnnotationHighlighterPrivate;
} // namespace Internal
class VCSBASE_EXPORT BaseAnnotationHighlighter : public TextEditor::SyntaxHighlighter
{
Q_OBJECT
Q_DECLARE_PRIVATE(BaseAnnotationHighlighter)
public:
typedef QSet<QString> ChangeNumbers;
explicit BaseAnnotationHighlighter(const ChangeNumbers &changeNumbers, const QColor &bg,
explicit BaseAnnotationHighlighter(const ChangeNumbers &changeNumbers,
QTextDocument *document = 0);
virtual ~BaseAnnotationHighlighter();
@@ -53,13 +52,13 @@ public:
virtual void highlightBlock(const QString &text);
void setBackgroundColor(const QColor &color);
virtual void setFontSettings(const TextEditor::FontSettings &fontSettings);
private:
// Implement this to return the change number of a line
virtual QString changeNumber(const QString &block) const = 0;
Internal::BaseAnnotationHighlighterPrivate *const d;
QScopedPointer<BaseAnnotationHighlighterPrivate> d_ptr;
};
} // namespace VcsBase

View File

@@ -72,8 +72,7 @@ enum DiffFormats {
DiffInFormat,
DiffOutFormat,
DiffFileFormat,
DiffLocationFormat,
NumDiffFormats
DiffLocationFormat
};
enum FoldingState {
@@ -83,61 +82,92 @@ enum FoldingState {
Location
};
}; // namespace Internal;
static inline QTextCharFormat invertedColorFormat(const QTextCharFormat &in)
{
QTextCharFormat rc = in;
rc.setForeground(in.background());
rc.setBackground(in.foreground());
return rc;
}
// --- DiffHighlighterPrivate
class DiffHighlighterPrivate
{
DiffHighlighter *q_ptr;
Q_DECLARE_PUBLIC(DiffHighlighter)
public:
DiffHighlighterPrivate(const QRegExp &filePattern);
DiffFormats analyzeLine(const QString &block) const;
Internal::DiffFormats analyzeLine(const QString &block) const;
void updateOtherFormats();
mutable QRegExp m_filePattern;
const QString m_locationIndicator;
const QChar m_diffInIndicator;
const QChar m_diffOutIndicator;
QTextCharFormat m_formats[NumDiffFormats];
QTextCharFormat m_addedTrailingWhiteSpaceFormat;
FoldingState m_foldingState;
Internal::FoldingState m_foldingState;
};
DiffHighlighterPrivate::DiffHighlighterPrivate(const QRegExp &filePattern) :
q_ptr(0),
m_filePattern(filePattern),
m_locationIndicator(QLatin1String("@@")),
m_diffInIndicator(QLatin1Char('+')),
m_diffOutIndicator(QLatin1Char('-')),
m_foldingState(StartOfFile)
m_foldingState(Internal::StartOfFile)
{
QTC_CHECK(filePattern.isValid());
}
DiffFormats DiffHighlighterPrivate::analyzeLine(const QString &text) const
Internal::DiffFormats DiffHighlighterPrivate::analyzeLine(const QString &text) const
{
// Do not match on git "--- a/" as a deleted line, check
// file first
if (m_filePattern.indexIn(text) == 0)
return DiffFileFormat;
return Internal::DiffFileFormat;
if (text.startsWith(m_diffInIndicator))
return DiffInFormat;
return Internal::DiffInFormat;
if (text.startsWith(m_diffOutIndicator))
return DiffOutFormat;
return Internal::DiffOutFormat;
if (text.startsWith(m_locationIndicator))
return DiffLocationFormat;
return DiffTextFormat;
return Internal::DiffLocationFormat;
return Internal::DiffTextFormat;
}
} // namespace Internal
void DiffHighlighterPrivate::updateOtherFormats()
{
Q_Q(DiffHighlighter);
m_addedTrailingWhiteSpaceFormat =
invertedColorFormat(q->formatForCategory(Internal::DiffInFormat));
}
// --- DiffHighlighter
DiffHighlighter::DiffHighlighter(const QRegExp &filePattern) :
TextEditor::SyntaxHighlighter(static_cast<QTextDocument *>(0)),
d(new Internal::DiffHighlighterPrivate(filePattern))
d_ptr(new DiffHighlighterPrivate(filePattern))
{
d_ptr->q_ptr = this;
Q_D(DiffHighlighter);
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_TEXT
<< TextEditor::C_ADDED_LINE
<< TextEditor::C_REMOVED_LINE
<< TextEditor::C_DIFF_FILE
<< TextEditor::C_DIFF_LOCATION;
}
setTextFormatCategories(categories);
d->updateOtherFormats();
}
DiffHighlighter::~DiffHighlighter()
{
delete d;
}
// Check trailing spaces
@@ -157,6 +187,7 @@ static inline int trimmedLength(const QString &in)
*/
void DiffHighlighter::highlightBlock(const QString &text)
{
Q_D(DiffHighlighter);
if (text.isEmpty())
return;
@@ -168,13 +199,13 @@ void DiffHighlighter::highlightBlock(const QString &text)
case Internal::DiffInFormat: {
// Mark trailing whitespace.
const int trimmedLen = trimmedLength(text);
setFormat(0, trimmedLen, d->m_formats[format]);
setFormat(0, trimmedLen, formatForCategory(format));
if (trimmedLen != length)
setFormat(trimmedLen, length - trimmedLen, d->m_addedTrailingWhiteSpaceFormat);
}
break;
default:
setFormat(0, length, d->m_formats[format]);
setFormat(0, length, formatForCategory(format));
break;
}
@@ -234,24 +265,11 @@ void DiffHighlighter::highlightBlock(const QString &text)
}
}
static inline QTextCharFormat invertedColorFormat(const QTextCharFormat &in)
void DiffHighlighter::setFontSettings(const TextEditor::FontSettings &fontSettings)
{
QTextCharFormat rc = in;
rc.setForeground(in.background());
rc.setBackground(in.foreground());
return rc;
}
void DiffHighlighter::setFormats(const QVector<QTextCharFormat> &s)
{
if (s.size() == Internal::NumDiffFormats) {
qCopy(s.constBegin(), s.constEnd(), d->m_formats);
// Display trailing blanks with colors swapped
d->m_addedTrailingWhiteSpaceFormat =
invertedColorFormat(d->m_formats[Internal::DiffInFormat]);
} else {
qWarning("%s: insufficient setting size: %d", Q_FUNC_INFO, s.size());
}
Q_D(DiffHighlighter);
SyntaxHighlighter::setFontSettings(fontSettings);
d->updateOtherFormats();
}
} // namespace VcsBase

View File

@@ -44,23 +44,22 @@ namespace TextEditor { class FontSettingsPage; }
namespace VcsBase {
namespace Internal { class DiffHighlighterPrivate; }
class DiffHighlighterPrivate;
class VCSBASE_EXPORT DiffHighlighter : public TextEditor::SyntaxHighlighter
{
Q_OBJECT
Q_DECLARE_PRIVATE(DiffHighlighter)
public:
explicit DiffHighlighter(const QRegExp &filePattern);
~DiffHighlighter();
void highlightBlock(const QString &text);
// Set formats from a sequence of type QTextCharFormat
void setFormats(const QVector<QTextCharFormat> &s);
virtual void setFontSettings(const TextEditor::FontSettings &fontSettings);
private:
Internal::DiffHighlighterPrivate *const d;
QScopedPointer<DiffHighlighterPrivate> d_ptr;
};
} // namespace VcsBase

View File

@@ -41,7 +41,6 @@
#include <projectexplorer/session.h>
#include <texteditor/basetextdocument.h>
#include <texteditor/basetextdocumentlayout.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorsettings.h>
#include <utils/qtcassert.h>
@@ -571,8 +570,6 @@ public:
bool m_mouseDragging;
QList<AbstractTextCursorHandler *> m_textCursorHandlers;
QColor m_backgroundColor;
private:
QComboBox *m_entriesComboBox;
};
@@ -1048,7 +1045,7 @@ void VcsBaseEditorWidget::slotActivateAnnotation()
ah->setChangeNumbers(changes);
ah->rehighlight();
} else {
baseTextDocument()->setSyntaxHighlighter(createAnnotationHighlighter(changes, d->m_backgroundColor));
baseTextDocument()->setSyntaxHighlighter(createAnnotationHighlighter(changes));
}
}
@@ -1186,33 +1183,6 @@ void VcsBaseEditorWidget::reportCommandFinished(bool ok, int exitCode, const QVa
setPlainText(tr("Failed to retrieve data."));
}
void VcsBaseEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
{
TextEditor::BaseTextEditorWidget::setFontSettings(fs);
d->m_backgroundColor = fs.toTextCharFormat(TextEditor::C_TEXT)
.brushProperty(QTextFormat::BackgroundBrush).color();
if (d->m_parameters->type == AnnotateOutput) {
if (BaseAnnotationHighlighter *highlighter = qobject_cast<BaseAnnotationHighlighter *>(baseTextDocument()->syntaxHighlighter())) {
highlighter->setBackgroundColor(d->m_backgroundColor);
highlighter->rehighlight();
}
} else if (hasDiff()) {
if (DiffHighlighter *highlighter = qobject_cast<DiffHighlighter*>(baseTextDocument()->syntaxHighlighter())) {
static QVector<TextEditor::TextStyle> categories;
if (categories.isEmpty()) {
categories << TextEditor::C_TEXT
<< TextEditor::C_ADDED_LINE
<< TextEditor::C_REMOVED_LINE
<< TextEditor::C_DIFF_FILE
<< TextEditor::C_DIFF_LOCATION;
}
highlighter->setFormats(fs.toTextCharFormats(categories));
highlighter->rehighlight();
}
}
}
const VcsBaseEditorParameters *VcsBaseEditorWidget::findType(const VcsBaseEditorParameters *array,
int arraySize,
EditorContentType et)

View File

@@ -217,9 +217,6 @@ protected:
void mouseDoubleClickEvent(QMouseEvent *e);
void keyPressEvent(QKeyEvent *);
public slots:
void setFontSettings(const TextEditor::FontSettings &);
private slots:
void slotActivateAnnotation();
void slotPopulateDiffBrowser();
@@ -244,8 +241,7 @@ protected:
// Implement to identify a change number at the cursor position
virtual QString changeUnderCursor(const QTextCursor &) const = 0;
// Factory functions for highlighters
virtual BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes,
const QColor &bg) const = 0;
virtual BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const = 0;
// Returns a local file name from the diff file specification
// (text cursor at position above change hunk)
QString fileNameFromDiffSpecification(const QTextBlock &inBlock) const;

View File

@@ -8,6 +8,7 @@ SOURCES += \
tst_highlighterengine.cpp \
highlightermock.cpp \
formats.cpp \
syntaxhighlighter.cpp \
$$GENERICHIGHLIGHTERDIR/highlighter.cpp \
$$GENERICHIGHLIGHTERDIR/context.cpp \
$$GENERICHIGHLIGHTERDIR/dynamicrule.cpp \

View File

@@ -0,0 +1,56 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "syntaxhighlighter.h"
#include "highlighter.h"
#include "formats.h"
using namespace TextEditor;
using namespace Internal;
QTextCharFormat SyntaxHighlighter::formatForCategory(int categoryIndex) const
{
switch (categoryIndex) {
case Highlighter::Keyword: return Formats::instance().keywordFormat();
case Highlighter::DataType: return Formats::instance().dataTypeFormat();
case Highlighter::Decimal: return Formats::instance().decimalFormat();
case Highlighter::BaseN: return Formats::instance().baseNFormat();
case Highlighter::Float: return Formats::instance().floatFormat();
case Highlighter::Char: return Formats::instance().charFormat();
case Highlighter::String: return Formats::instance().stringFormat();
case Highlighter::Comment: return Formats::instance().commentFormat();
case Highlighter::Alert: return Formats::instance().alertFormat();
case Highlighter::Error: return Formats::instance().errorFormat();
case Highlighter::Function: return Formats::instance().functionFormat();
case Highlighter::RegionMarker: return Formats::instance().regionMarketFormat();
case Highlighter::Others: return Formats::instance().othersFormat();
default: return QTextCharFormat();
}
}

View File

@@ -35,6 +35,7 @@
// base instead of the real TextEditor::SyntaxHighlighter should not affect it.
#include <QSyntaxHighlighter>
#include <texteditor/texteditorconstants.h>
namespace TextEditor {
@@ -47,6 +48,10 @@ public:
protected:
void applyFormatToSpaces(const QString &, const QTextCharFormat &)
{}
void setTextFormatCategories(const QVector<TextEditor::TextStyle> &)
{}
QTextCharFormat formatForCategory(int categoryIndex) const;
};
}

View File

@@ -57,7 +57,7 @@ class tst_HighlighterEngine : public QObject
public:
tst_HighlighterEngine();
private slots:
private slots:
void initTestCase();
void init();
@@ -130,20 +130,6 @@ void tst_HighlighterEngine::initTestCase()
m_highlighterMock.reset(new HighlighterMock());
m_highlighterMock->setDefaultContext(m_definition->initialContext());
m_highlighterMock->setDocument(m_text.document());
m_highlighterMock->configureFormat(Highlighter::Keyword, Formats::instance().keywordFormat());
m_highlighterMock->configureFormat(Highlighter::DataType, Formats::instance().dataTypeFormat());
m_highlighterMock->configureFormat(Highlighter::Decimal, Formats::instance().decimalFormat());
m_highlighterMock->configureFormat(Highlighter::BaseN, Formats::instance().baseNFormat());
m_highlighterMock->configureFormat(Highlighter::Float, Formats::instance().floatFormat());
m_highlighterMock->configureFormat(Highlighter::Char, Formats::instance().charFormat());
m_highlighterMock->configureFormat(Highlighter::String, Formats::instance().stringFormat());
m_highlighterMock->configureFormat(Highlighter::Comment, Formats::instance().commentFormat());
m_highlighterMock->configureFormat(Highlighter::Alert, Formats::instance().alertFormat());
m_highlighterMock->configureFormat(Highlighter::Error, Formats::instance().errorFormat());
m_highlighterMock->configureFormat(Highlighter::Function, Formats::instance().functionFormat());
m_highlighterMock->configureFormat(Highlighter::RegionMarker,
Formats::instance().regionMarketFormat());
m_highlighterMock->configureFormat(Highlighter::Others, Formats::instance().othersFormat());
}
void tst_HighlighterEngine::init()
@@ -605,7 +591,7 @@ void tst_HighlighterEngine::testSimpleLine_data()
HighlightSequence seqi(seqd);
seqi.add(9, 17, Formats::instance().commentFormat());
HighlightSequence seqj(seqd);
seqj.add(9, 11, Formats::instance().commentFormat());
seqj.add(9, 11, Formats::instance().commentFormat());
HighlightSequence seqk(0, 3);
HighlightSequence seql(0, 3, Formats::instance().keywordFormat());
HighlightSequence seqm(0, 2);