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

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