1#include "FileDiffHighlighter.h"
2
3#include <GitQlientStyles.h>
4#include <QTextDocument>
5
6FileDiffHighlighter::FileDiffHighlighter(QTextDocument *document)
7 : QSyntaxHighlighter(document)
8{
9}
10
11void FileDiffHighlighter::highlightBlock(const QString &text)
12{
13 setCurrentBlockState(previousBlockState() + 1);
14
15 QTextBlockFormat myFormat;
16 QTextCharFormat format;
17 const auto currentLine = currentBlock().blockNumber() + 1;
18
19 if (!mFileDiffInfo.isEmpty())
20 {
21 for (const auto &diff : qAsConst(mFileDiffInfo))
22 {
23 if (diff.startLine <= currentLine && currentLine <= diff.endLine)
24 {
25 if (diff.addition)
26 {
27 myFormat.setBackground(GitQlientStyles::getGreen());
28 // myFormat.setForeground(GitQlientStyles::getGreen());
29 }
30 else
31 myFormat.setBackground(GitQlientStyles::getRed());
32 }
33 }
34 }
35 else if (!text.isEmpty())
36 {
37 switch (text.at(0).toLatin1())
38 {
39 case '@':
40 myFormat.setBackground(GitQlientStyles::getOrange());
41 format.setFontWeight(QFont::ExtraBold);
42 break;
43 case '+':
44 myFormat.setBackground(GitQlientStyles::getGreen());
45 break;
46 case '-':
47 myFormat.setBackground(GitQlientStyles::getRed());
48 break;
49 default:
50 break;
51 }
52 }
53
54 if (myFormat.isValid())
55 {
56 QTextCursor(currentBlock()).setBlockFormat(myFormat);
57 setFormat(0, currentBlock().length(), format);
58 }
59}
60