File indexing completed on 2024-04-14 05:37:12

0001 /*
0002     SPDX-FileCopyrightText: 2001-2004,2009 Otto Bruggeman <bruggie@gmail.com>
0003     SPDX-FileCopyrightText: 2001-2003 John Firebaugh <jfirebaugh@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "diffhunk.h"
0009 #include "diffhunk_p.h"
0010 
0011 // lib
0012 #include <komparediff2_logging.h>
0013 
0014 using namespace KompareDiff2;
0015 
0016 DiffHunk::DiffHunk(int sourceLine, int destinationLine, const QString &function, Type type)
0017     : d_ptr(new DiffHunkPrivate(sourceLine, destinationLine, function, type))
0018 {
0019 }
0020 
0021 DiffHunk::~DiffHunk() = default;
0022 
0023 DifferenceList DiffHunk::differences() const
0024 {
0025     Q_D(const DiffHunk);
0026 
0027     return d->differences;
0028 };
0029 
0030 QString DiffHunk::function() const
0031 {
0032     Q_D(const DiffHunk);
0033 
0034     return d->function;
0035 };
0036 
0037 int DiffHunk::sourceLineNumber() const
0038 {
0039     Q_D(const DiffHunk);
0040 
0041     return d->sourceLine;
0042 };
0043 
0044 int DiffHunk::destinationLineNumber() const
0045 {
0046     Q_D(const DiffHunk);
0047 
0048     return d->destinationLine;
0049 };
0050 
0051 DiffHunk::Type DiffHunk::type() const
0052 {
0053     Q_D(const DiffHunk);
0054 
0055     return d->type;
0056 }
0057 
0058 void DiffHunk::setType(Type type)
0059 {
0060     Q_D(DiffHunk);
0061 
0062     d->type = type;
0063 }
0064 
0065 void DiffHunk::add(Difference *diff)
0066 {
0067     Q_D(DiffHunk);
0068 
0069     d->differences.append(diff);
0070 }
0071 
0072 int DiffHunk::sourceLineCount() const
0073 {
0074     Q_D(const DiffHunk);
0075 
0076     int lineCount = 0;
0077 
0078     for (const Difference *diff : d->differences) {
0079         lineCount += diff->sourceLineCount();
0080     }
0081 
0082     return lineCount;
0083 }
0084 
0085 int DiffHunk::destinationLineCount() const
0086 {
0087     Q_D(const DiffHunk);
0088 
0089     int lineCount = 0;
0090 
0091     for (const Difference *diff : d->differences) {
0092         lineCount += diff->destinationLineCount();
0093     }
0094 
0095     return lineCount;
0096 }
0097 
0098 QString DiffHunk::recreateHunk() const
0099 {
0100     Q_D(const DiffHunk);
0101 
0102     QString hunk;
0103     QString differences;
0104 
0105     // recreate body
0106     int slc = 0; // source line count
0107     int dlc = 0; // destination line count
0108     for (const Difference *diff : d->differences) {
0109         switch (diff->type()) {
0110         case Difference::Unchanged:
0111         case Difference::Change:
0112             slc += diff->sourceLineCount();
0113             dlc += diff->destinationLineCount();
0114             break;
0115         case Difference::Insert:
0116             dlc += diff->destinationLineCount();
0117             break;
0118         case Difference::Delete:
0119             slc += diff->sourceLineCount();
0120             break;
0121         }
0122         differences += diff->recreateDifference();
0123     }
0124 
0125     // recreate header
0126     hunk += QStringLiteral("@@ -%1,%3 +%2,%4 @@").arg(d->sourceLine).arg(d->destinationLine).arg(slc).arg(dlc);
0127 
0128     if (!d->function.isEmpty())
0129         hunk += QLatin1Char(' ') + d->function;
0130 
0131     hunk += QLatin1Char('\n');
0132 
0133     hunk += differences;
0134 
0135     qCDebug(KOMPAREDIFF2_LOG) << hunk;
0136     return hunk;
0137 }