File indexing completed on 2024-05-05 05:44:50

0001 /***************************************************************************
0002  *   Copyright (C) 2007 by Rajko Albrecht                                  *
0003  *   ral@alwins-world.de                                                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 
0021 #include "diffsyntax.h"
0022 
0023 #include <QFontDatabase>
0024 #include <QRegularExpression>
0025 
0026 void DiffSyntax::highlightBlock(const QString &aText)
0027 {
0028     static const QRegularExpression a(QLatin1String("^\\w+:\\s.*$")); // filename (Index: foo/bar.txt)
0029     QTextCharFormat format;
0030     format.setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
0031     bool bIsModifiedLine = false;
0032 
0033     if (previousBlockState() == 1) {
0034         setCurrentBlockState(2);
0035     } else if (previousBlockState() == 2) {
0036         if (a.match(aText).capturedStart() != 0) {
0037             setCurrentBlockState(2);
0038         }
0039     }
0040 
0041     if (a.match(aText).hasMatch()) { // filename (Index: foo/bar.txt)
0042         format.setForeground(QColor(0x66, 0x00, 0x33));
0043         if (previousBlockState() == 1 || previousBlockState() == 2) {
0044             format.setFontWeight(QFont::Bold);
0045         } else {
0046             format.setFontItalic(true);
0047         }
0048     } else if (aText.startsWith(QLatin1String("_____"))) {
0049         setCurrentBlockState(1);
0050         format.setForeground(QColor(0x1D, 0x1D, 0x8F));
0051     } else if (aText.startsWith(QLatin1Char('+'))) { // added line in new file
0052         format.setForeground(QColor(0x00, 0x8B, 0x00));
0053         if (aText.startsWith(QLatin1String("+++"))) { // new file name
0054             format.setFontWeight(QFont::Bold);
0055         } else {
0056             bIsModifiedLine = true;
0057         }
0058     } else if (aText.startsWith(QLatin1Char('-'))) { // removed line in old file
0059         format.setForeground(QColor(0xCD, 0x33, 0x33));
0060         if (aText.startsWith(QLatin1String("---"))) { // old file name
0061             format.setFontWeight(QFont::Bold);
0062         } else {
0063             bIsModifiedLine = true;
0064         }
0065     } else if (aText.startsWith(QLatin1String("@@"))) { // line numbers
0066         format.setForeground(QColor(0x1D, 0x1D, 0x8F));
0067     }
0068     if (previousBlockState() == 2 && currentBlockState() == 2) {
0069         if (aText.startsWith(QLatin1String("   +"))) {
0070             format.setForeground(QColor(0x00, 0x8B, 0x00));
0071         } else if (aText.startsWith(QLatin1String("   -"))) {
0072             format.setForeground(QColor(0xCD, 0x33, 0x33));
0073         }
0074     }
0075     setFormat(0, aText.length(), format);
0076     // highlight trailing spaces
0077     if (bIsModifiedLine && aText.endsWith(QLatin1Char(' '))) {
0078         static const QRegularExpression hlTrailingSpaceRx(QLatin1String("[^\\s]")); // search the last non-space
0079         const int idx = aText.lastIndexOf(hlTrailingSpaceRx);
0080         format.setBackground(format.foreground());
0081         setFormat(idx + 1, aText.length() - idx - 1, format); // only spaces in this range
0082     }
0083 }
0084 
0085 #include "moc_diffsyntax.cpp"