File indexing completed on 2024-04-28 13:39:48

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 "difference.h"
0009 #include "differencestringpair.h"
0010 #include "levenshteintable.h"
0011 
0012 using namespace Diff2;
0013 
0014 Difference::Difference(int sourceLineNo, int destinationLineNo, int type) :
0015     QObject(),
0016     m_type(type),
0017     m_sourceLineNo(sourceLineNo),
0018     m_destinationLineNo(destinationLineNo),
0019     m_trackingDestinationLineNo(sourceLineNo),      // The whole patch starts as unapplied
0020     m_applied(false),
0021     m_conflicts(false),
0022     m_unsaved(false)
0023 {
0024 }
0025 
0026 Difference::~Difference()
0027 {
0028     qDeleteAll(m_sourceLines);
0029     qDeleteAll(m_destinationLines);
0030 }
0031 
0032 void Difference::addSourceLine(QString line)
0033 {
0034     m_sourceLines.append(new DifferenceString(line));
0035 }
0036 
0037 void Difference::addDestinationLine(QString line)
0038 {
0039     m_destinationLines.append(new DifferenceString(line));
0040 }
0041 
0042 int Difference::sourceLineCount() const
0043 {
0044     return m_sourceLines.count();
0045 }
0046 
0047 int Difference::destinationLineCount() const
0048 {
0049     return m_destinationLines.count();
0050 }
0051 
0052 int Difference::sourceLineEnd() const
0053 {
0054     return m_sourceLineNo + m_sourceLines.count();
0055 }
0056 
0057 int Difference::destinationLineEnd() const
0058 {
0059     return m_destinationLineNo + m_destinationLines.count();
0060 }
0061 
0062 int Difference::trackingDestinationLineEnd() const
0063 {
0064     return m_trackingDestinationLineNo + m_destinationLines.count();
0065 }
0066 
0067 void Difference::apply(bool apply)
0068 {
0069     if (apply != m_applied)
0070     {
0071         m_applied = apply;
0072         m_unsaved = !m_unsaved;
0073         Q_EMIT differenceApplied(this);
0074     }
0075 }
0076 
0077 void Difference::applyQuietly(bool apply)
0078 {
0079     if (m_applied != apply)
0080     {
0081         m_unsaved = !m_unsaved;
0082         m_applied = apply;
0083     }
0084 }
0085 
0086 void Difference::determineInlineDifferences()
0087 {
0088     if (m_type != Difference::Change)
0089         return;
0090 
0091     // Do nothing for now when the slc != dlc
0092     // One could try to find the closest matching destination string for any
0093     // of the source strings but this is compute intensive
0094     int slc = sourceLineCount();
0095 
0096     if (slc != destinationLineCount())
0097         return;
0098 
0099     LevenshteinTable<DifferenceStringPair> table;
0100 
0101     for (int i = 0; i < slc; ++i)
0102     {
0103         DifferenceString* sl = sourceLineAt(i);
0104         DifferenceString* dl = destinationLineAt(i);
0105         DifferenceStringPair* pair = new DifferenceStringPair(sl, dl);
0106 
0107         // return value 0 means something went wrong creating the table so don't bother finding markers
0108         if (table.createTable(pair) != 0)
0109             table.createListsOfMarkers();
0110     }
0111 }
0112 
0113 QString Difference::recreateDifference() const
0114 {
0115     QString difference;
0116 
0117     // source
0118     DifferenceStringListConstIterator stringIt = m_sourceLines.begin();
0119     DifferenceStringListConstIterator sEnd     = m_sourceLines.end();
0120 
0121     for (; stringIt != sEnd; ++stringIt)
0122     {
0123         switch (m_type)
0124         {
0125         case Change:
0126         case Delete:
0127             difference += QLatin1Char('-');
0128             break;
0129         default:
0130             // Insert but this is not possible in source
0131             // Unchanged will be handled in destination
0132             // since they are the same
0133 //             qCDebug(LIBKOMPAREDIFF2) << "Go away, nothing to do for you in source...";
0134             continue;
0135         }
0136         difference += (*stringIt)->string();
0137     }
0138 
0139     //destination
0140     stringIt = m_destinationLines.begin();
0141     sEnd     = m_destinationLines.end();
0142 
0143     for (; stringIt != sEnd; ++stringIt)
0144     {
0145         switch (m_type)
0146         {
0147         case Change:
0148         case Insert:
0149             difference += QLatin1Char('+');
0150             break;
0151         case Unchanged:
0152             difference += QLatin1Char(' ');
0153             break;
0154         default: // Delete but this is not possible in destination
0155 //             qCDebug(LIBKOMPAREDIFF2) << "Go away, nothing to do for you in destination...";
0156             continue;
0157         }
0158         difference += (*stringIt)->string();
0159     }
0160 
0161     return difference;
0162 }
0163 
0164 #include "moc_difference.cpp"