KompareDiff2

diffhunk.cpp
1/*
2 SPDX-FileCopyrightText: 2001-2004,2009 Otto Bruggeman <bruggie@gmail.com>
3 SPDX-FileCopyrightText: 2001-2003 John Firebaugh <jfirebaugh@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "diffhunk.h"
9#include "diffhunk_p.h"
10
11// lib
12#include <komparediff2_logging.h>
13
14using namespace KompareDiff2;
15
16DiffHunk::DiffHunk(int sourceLine, int destinationLine, const QString &function, Type type)
17 : d_ptr(new DiffHunkPrivate(sourceLine, destinationLine, function, type))
18{
19}
20
21DiffHunk::~DiffHunk() = default;
22
23DifferenceList DiffHunk::differences() const
24{
25 Q_D(const DiffHunk);
26
27 return d->differences;
28};
29
30QString DiffHunk::function() const
31{
32 Q_D(const DiffHunk);
33
34 return d->function;
35};
36
37int DiffHunk::sourceLineNumber() const
38{
39 Q_D(const DiffHunk);
40
41 return d->sourceLine;
42};
43
44int DiffHunk::destinationLineNumber() const
45{
46 Q_D(const DiffHunk);
47
48 return d->destinationLine;
49};
50
51DiffHunk::Type DiffHunk::type() const
52{
53 Q_D(const DiffHunk);
54
55 return d->type;
56}
57
58void DiffHunk::setType(Type type)
59{
61
62 d->type = type;
63}
64
65void DiffHunk::add(Difference *diff)
66{
68
69 d->differences.append(diff);
70}
71
72int DiffHunk::sourceLineCount() const
73{
74 Q_D(const DiffHunk);
75
76 int lineCount = 0;
77
78 for (const Difference *diff : d->differences) {
79 lineCount += diff->sourceLineCount();
80 }
81
82 return lineCount;
83}
84
85int DiffHunk::destinationLineCount() const
86{
87 Q_D(const DiffHunk);
88
89 int lineCount = 0;
90
91 for (const Difference *diff : d->differences) {
92 lineCount += diff->destinationLineCount();
93 }
94
95 return lineCount;
96}
97
98QString DiffHunk::recreateHunk() const
99{
100 Q_D(const DiffHunk);
101
102 QString hunk;
103 QString differences;
104
105 // recreate body
106 int slc = 0; // source line count
107 int dlc = 0; // destination line count
108 for (const Difference *diff : d->differences) {
109 switch (diff->type()) {
110 case Difference::Unchanged:
111 case Difference::Change:
112 slc += diff->sourceLineCount();
113 dlc += diff->destinationLineCount();
114 break;
115 case Difference::Insert:
116 dlc += diff->destinationLineCount();
117 break;
118 case Difference::Delete:
119 slc += diff->sourceLineCount();
120 break;
121 }
122 differences += diff->recreateDifference();
123 }
124
125 // recreate header
126 hunk += QStringLiteral("@@ -%1,%3 +%2,%4 @@").arg(d->sourceLine).arg(d->destinationLine).arg(slc).arg(dlc);
127
128 if (!d->function.isEmpty())
129 hunk += QLatin1Char(' ') + d->function;
130
131 hunk += QLatin1Char('\n');
132
133 hunk += differences;
134
135 qCDebug(KOMPAREDIFF2_LOG) << hunk;
136 return hunk;
137}
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
KompareDiff2 namespace.
QString arg(Args &&... args) const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:50:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.