diff.h 13.4 KB
Newer Older
Joachim Eibl's avatar
Joachim Eibl committed
1
/***************************************************************************
2 3
 *   Copyright (C) 2003-2007 by Joachim Eibl <joachim.eibl at gmx.de>      *
 *   Copyright (C) 2018 Michael Reeves reeves.87@gmail.com                 *
Joachim Eibl's avatar
Joachim Eibl committed
4 5 6 7 8 9 10 11 12 13 14
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef DIFF_H
#define DIFF_H

Joachim Eibl's avatar
Joachim Eibl committed
15
#include <QPainter>
16
#include <QList>
17

Joachim Eibl's avatar
Joachim Eibl committed
18 19
#include "common.h"
#include "fileaccess.h"
20
#include "options.h"
21
#include "gnudiff_diff.h"
22
#include "SourceData.h"
Joachim Eibl's avatar
0.9.93  
Joachim Eibl committed
23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
enum e_MergeDetails
{
   eDefault,
   eNoChange,
   eBChanged,
   eCChanged,
   eBCChanged,         // conflict
   eBCChangedAndEqual, // possible conflict
   eBDeleted,
   eCDeleted,
   eBCDeleted,         // possible conflict

   eBChanged_CDeleted, // conflict
   eCChanged_BDeleted, // conflict
   eBAdded,
   eCAdded,
   eBCAdded,           // conflict
   eBCAddedAndEqual    // possible conflict
};

Joachim Eibl's avatar
Joachim Eibl committed
44 45
// Each range with matching elements is followed by a range with differences on either side.
// Then again range of matching elements should follow.
Michael Reeves's avatar
Michael Reeves committed
46
class Diff
Joachim Eibl's avatar
Joachim Eibl committed
47
{
Michael Reeves's avatar
Michael Reeves committed
48 49 50 51 52 53 54 55 56 57 58 59
  public:
    LineRef nofEquals;

    qint64 diff1;
    qint64 diff2;

    Diff(LineRef eq, qint64 d1, qint64 d2)
    {
        nofEquals = eq;
        diff1 = d1;
        diff2 = d2;
    }
Joachim Eibl's avatar
Joachim Eibl committed
60 61 62 63
};

typedef std::list<Diff> DiffList;

Michael Reeves's avatar
Michael Reeves committed
64
class LineData
Joachim Eibl's avatar
Joachim Eibl committed
65
{
66 67 68 69 70 71 72 73 74 75 76 77 78 79
   private:
    const QChar* pLine = nullptr;
    const QChar* pFirstNonWhiteChar = nullptr;
    int mSize = 0;
   public:
    bool bContainsPureComment = false;
    inline int size() const { return mSize; }
    inline void setSize(const int newSize) { mSize = newSize; }

    inline void setFirstNonWhiteChar(const QChar* firstNonWhiteChar) { pFirstNonWhiteChar = firstNonWhiteChar;}
    inline const QChar* getFirstNonWhiteChar() const { return pFirstNonWhiteChar; }

    inline const QChar* getLine() const { return pLine; }
    inline void setLine(const QChar* line) { pLine = line;}
Michael Reeves's avatar
Michael Reeves committed
80 81
    int width(int tabSize) const; // Calcs width considering tabs.
    //int occurrences;
82
    bool whiteLine() const { return pFirstNonWhiteChar - pLine == mSize; }
Joachim Eibl's avatar
Joachim Eibl committed
83 84 85 86 87
};

class Diff3LineList;
class Diff3LineVector;

Michael Reeves's avatar
Michael Reeves committed
88
class DiffBufferInfo
Joachim Eibl's avatar
Joachim Eibl committed
89
{
Michael Reeves's avatar
Michael Reeves committed
90 91 92 93 94 95 96 97 98 99 100
  public:
    const LineData* m_pLineDataA;
    const LineData* m_pLineDataB;
    const LineData* m_pLineDataC;
    LineRef m_sizeA;
    LineRef m_sizeB;
    LineRef m_sizeC;
    const Diff3LineList* m_pDiff3LineList;
    const Diff3LineVector* m_pDiff3LineVector;
    void init(Diff3LineList* d3ll, const Diff3LineVector* d3lv,
              const LineData* pldA, LineRef sizeA, const LineData* pldB, LineRef sizeB, const LineData* pldC, LineRef sizeC);
Joachim Eibl's avatar
Joachim Eibl committed
101 102
};

Michael Reeves's avatar
Michael Reeves committed
103
class Diff3Line
Joachim Eibl's avatar
Joachim Eibl committed
104
{
105
  private:
Michael Reeves's avatar
Michael Reeves committed
106 107 108
    LineRef lineA = -1;
    LineRef lineB = -1;
    LineRef lineC = -1;
109
  public:
Michael Reeves's avatar
Michael Reeves committed
110

Michael Reeves's avatar
Michael Reeves committed
111 112 113
    bool bAEqC = false; // These are true if equal or only white-space changes exist.
    bool bBEqC = false;
    bool bAEqB = false;
Michael Reeves's avatar
Michael Reeves committed
114

Michael Reeves's avatar
Michael Reeves committed
115 116 117
    bool bWhiteLineA  = false;
    bool bWhiteLineB  = false;
    bool bWhiteLineC  = false;
Michael Reeves's avatar
Michael Reeves committed
118

Michael Reeves's avatar
Michael Reeves committed
119 120 121
    DiffList* pFineAB = nullptr; // These are 0 only if completely equal or if either source doesn't exist.
    DiffList* pFineBC = nullptr;
    DiffList* pFineCA = nullptr;
Michael Reeves's avatar
Michael Reeves committed
122

Michael Reeves's avatar
Michael Reeves committed
123 124
    int linesNeededForDisplay = 1;    // Due to wordwrap
    int sumLinesNeededForDisplay = 0; // For fast conversion to m_diff3WrapLineVector
Michael Reeves's avatar
Michael Reeves committed
125

Michael Reeves's avatar
Michael Reeves committed
126
    DiffBufferInfo* m_pDiffBufferInfo = nullptr; // For convenience
Michael Reeves's avatar
Michael Reeves committed
127 128 129 130 131 132 133 134 135 136 137

    ~Diff3Line()
    {
        if(pFineAB != nullptr) delete pFineAB;
        if(pFineBC != nullptr) delete pFineBC;
        if(pFineCA != nullptr) delete pFineCA;
        pFineAB = nullptr;
        pFineBC = nullptr;
        pFineCA = nullptr;
    }

138 139 140 141 142 143 144 145
    LineRef getLineA() const { return lineA; }
    LineRef getLineB() const { return lineB; }
    LineRef getLineC() const { return lineC; }

    void setLineA(const LineRef& line) { lineA = line; }
    void setLineB(const LineRef& line) { lineB = line; }
    void setLineC(const LineRef& line) { lineC = line; }

Michael Reeves's avatar
Michael Reeves committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    bool operator==(const Diff3Line& d3l) const
    {
        return lineA == d3l.lineA && lineB == d3l.lineB && lineC == d3l.lineC && bAEqB == d3l.bAEqB && bAEqC == d3l.bAEqC && bBEqC == d3l.bBEqC;
    }

    const LineData* getLineData(int src) const
    {
        Q_ASSERT(m_pDiffBufferInfo != nullptr);
        if(src == 1 && lineA >= 0) return &m_pDiffBufferInfo->m_pLineDataA[lineA];
        if(src == 2 && lineB >= 0) return &m_pDiffBufferInfo->m_pLineDataB[lineB];
        if(src == 3 && lineC >= 0) return &m_pDiffBufferInfo->m_pLineDataC[lineC];
        return nullptr;
    }
    QString getString(int src) const
    {
        const LineData* pld = getLineData(src);
        if(pld)
163
            return QString(pld->getLine(), pld->size());
Michael Reeves's avatar
Michael Reeves committed
164 165 166 167 168 169 170 171 172 173
        else
            return QString();
    }
    LineRef getLineInFile(int src) const
    {
        if(src == 1) return lineA;
        if(src == 2) return lineB;
        if(src == 3) return lineC;
        return -1;
    }
174 175

    bool fineDiff(const int selector, const LineData* v1, const LineData* v2);
176 177
    void mergeOneLine(e_MergeDetails& mergeDetails, bool& bConflict, bool& bLineRemoved, int& src, bool bTwoInputs) const;

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  private:
    void setFineDiff(const int selector, DiffList* pDiffList)
    {
        Q_ASSERT(selector == 1 || selector == 2 || selector == 3);
        if(selector == 1)
        {
            if(pFineAB != nullptr)
                delete pFineAB;
            pFineAB = pDiffList;
        }
        else if(selector == 2)
        {
            if(pFineBC != nullptr)
                delete pFineBC;
            pFineBC = pDiffList;
        }
        else if(selector == 3)
        {
            if(pFineCA)
                delete pFineCA;
            pFineCA = pDiffList;
        }
    }
Joachim Eibl's avatar
Joachim Eibl committed
201 202
};

203
class Diff3LineList : public QList<Diff3Line>
Joachim Eibl's avatar
Joachim Eibl committed
204
{
205 206 207 208
  public:
    bool fineDiff(const int selector, const LineData* v1, const LineData* v2);
    void calcDiff3LineVector(Diff3LineVector& d3lv);
    void calcWhiteDiff3Lines(const LineData* pldA, const LineData* pldB, const LineData* pldC);
Joachim Eibl's avatar
Joachim Eibl committed
209
};
210

211
class Diff3LineVector : public QVector<Diff3Line*>
Joachim Eibl's avatar
Joachim Eibl committed
212 213
{
};
Joachim Eibl's avatar
0.9.86  
Joachim Eibl committed
214 215 216

class Diff3WrapLine
{
Michael Reeves's avatar
Michael Reeves committed
217 218 219 220 221
  public:
    Diff3Line* pD3L;
    int diff3LineIndex;
    int wrapLineOffset;
    int wrapLineLength;
Joachim Eibl's avatar
0.9.86  
Joachim Eibl committed
222 223
};

224
typedef QVector<Diff3WrapLine> Diff3WrapLineVector;
Joachim Eibl's avatar
0.9.86  
Joachim Eibl committed
225

Joachim Eibl's avatar
Joachim Eibl committed
226 227
class TotalDiffStatus
{
Michael Reeves's avatar
Michael Reeves committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  public:
    inline void reset()
    {
        bBinaryAEqC = false;
        bBinaryBEqC = false;
        bBinaryAEqB = false;
        bTextAEqC = false;
        bTextBEqC = false;
        bTextAEqB = false;
        nofUnsolvedConflicts = 0;
        nofSolvedConflicts = 0;
        nofWhitespaceConflicts = 0;
    }

    inline int getUnsolvedConflicts() const { return nofUnsolvedConflicts; }
    inline void setUnsolvedConflicts(const int unsolved) { nofUnsolvedConflicts = unsolved; }

    inline int getSolvedConflicts() const { return nofSolvedConflicts; }
    inline void setSolvedConflicts(const int solved) { nofSolvedConflicts = solved; }

    inline int getWhitespaceConflicts() const { return nofWhitespaceConflicts; }
    inline void setWhitespaceConflicts(const int wintespace) { nofWhitespaceConflicts = wintespace; }

    inline int getNonWhitespaceConflicts() { return getUnsolvedConflicts() + getSolvedConflicts() - getWhitespaceConflicts(); }

    bool isBinaryEqualAC() const { return bBinaryAEqC; }
    bool isBinaryEqualBC() const { return bBinaryBEqC; }
    bool isBinaryEqualAB() const { return bBinaryAEqB; }

    bool bBinaryAEqC = false;
    bool bBinaryBEqC = false;
    bool bBinaryAEqB = false;

    bool bTextAEqC = false;
    bool bTextBEqC = false;
    bool bTextAEqB = false;

  private:
Michael Reeves's avatar
Michael Reeves committed
266 267 268
    int nofUnsolvedConflicts = 0;
    int nofSolvedConflicts = 0;
    int nofWhitespaceConflicts = 0;
Joachim Eibl's avatar
Joachim Eibl committed
269 270
};

271
class ManualDiffHelpList; // A list of corresponding ranges
Joachim Eibl's avatar
Joachim Eibl committed
272 273 274
// Three corresponding ranges. (Minimum size of a valid range is one line.)
class ManualDiffHelpEntry
{
275
  private:
Michael Reeves's avatar
Michael Reeves committed
276 277 278 279 280 281
    LineRef lineA1 = -1;
    LineRef lineA2 = -1;
    LineRef lineB1 = -1;
    LineRef lineB2 = -1;
    LineRef lineC1 = -1;
    LineRef lineC2 = -1;
Michael Reeves's avatar
Michael Reeves committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
  public:
    LineRef& firstLine(int winIdx)
    {
        return winIdx == 1 ? lineA1 : (winIdx == 2 ? lineB1 : lineC1);
    }
    LineRef& lastLine(int winIdx)
    {
        return winIdx == 1 ? lineA2 : (winIdx == 2 ? lineB2 : lineC2);
    }
    bool isLineInRange(LineRef line, int winIdx)
    {
        return line >= 0 && line >= firstLine(winIdx) && line <= lastLine(winIdx);
    }
    bool operator==(const ManualDiffHelpEntry& r) const
    {
        return lineA1 == r.lineA1 && lineB1 == r.lineB1 && lineC1 == r.lineC1 &&
               lineA2 == r.lineA2 && lineB2 == r.lineB2 && lineC2 == r.lineC2;
    }
300 301

    int calcManualDiffFirstDiff3LineIdx(const Diff3LineVector& d3lv);
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

    void getRangeForUI(const int winIdx, int *rangeLine1, int *rangeLine2) const {
        if(winIdx == 1) {
            *rangeLine1 = lineA1;
            *rangeLine2 = lineA2;
        }
        if(winIdx == 2) {
            *rangeLine1 = lineB1;
            *rangeLine2 = lineB2;
        }
        if(winIdx == 3) {
            *rangeLine1 = lineC1;
            *rangeLine2 = lineC2;
        }
    }

318 319
    inline int getLine1(const int winIdx) const { return winIdx == 1 ? lineA1 : winIdx == 2 ? lineB1 : lineC1;}
    inline int getLine2(const int winIdx) const { return winIdx == 1 ? lineA2 : winIdx == 2 ? lineB2 : lineC2;}
320
    bool isValidMove(int line1, int line2, int winIdx1, int winIdx2) const;
Joachim Eibl's avatar
Joachim Eibl committed
321 322 323
};

// A list of corresponding ranges
324 325 326 327
class ManualDiffHelpList: public std::list<ManualDiffHelpEntry>
{
    public:
        bool isValidMove(int line1, int line2, int winIdx1, int winIdx2) const;
328
        void insertEntry(int winIdx, LineRef firstLine, LineRef lastLine);
329
};
330

Joachim Eibl's avatar
0.9.80  
Joachim Eibl committed
331
void calcDiff3LineListUsingAB(
Michael Reeves's avatar
Michael Reeves committed
332 333
    const DiffList* pDiffListAB,
    Diff3LineList& d3ll);
Joachim Eibl's avatar
Joachim Eibl committed
334 335

void calcDiff3LineListUsingAC(
Michael Reeves's avatar
Michael Reeves committed
336 337
    const DiffList* pDiffListAC,
    Diff3LineList& d3ll);
Joachim Eibl's avatar
Joachim Eibl committed
338

Joachim Eibl's avatar
Joachim Eibl committed
339
void calcDiff3LineListUsingBC(
Michael Reeves's avatar
Michael Reeves committed
340 341
    const DiffList* pDiffListBC,
    Diff3LineList& d3ll);
Joachim Eibl's avatar
Joachim Eibl committed
342

Michael Reeves's avatar
Michael Reeves committed
343
void correctManualDiffAlignment(Diff3LineList& d3ll, ManualDiffHelpList* pManualDiffHelpList);
Joachim Eibl's avatar
Joachim Eibl committed
344

Michael Reeves's avatar
Michael Reeves committed
345
void calcDiff3LineListTrim(Diff3LineList& d3ll, const LineData* pldA, const LineData* pldB, const LineData* pldC, ManualDiffHelpList* pManualDiffHelpList);
346

Joachim Eibl's avatar
Joachim Eibl committed
347

Joachim Eibl's avatar
Joachim Eibl committed
348 349 350
// Helper class that swaps left and right for some commands.
class MyPainter : public QPainter
{
Michael Reeves's avatar
Michael Reeves committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    int m_factor;
    int m_xOffset;
    int m_fontWidth;

  public:
    MyPainter(QPaintDevice* pd, bool bRTL, int width, int fontWidth)
        : QPainter(pd)
    {
        if(bRTL)
        {
            m_fontWidth = fontWidth;
            m_factor = -1;
            m_xOffset = width - 1;
        }
        else
        {
            m_fontWidth = 0;
            m_factor = 1;
            m_xOffset = 0;
        }
    }

    void fillRect(int x, int y, int w, int h, const QBrush& b)
    {
        if(m_factor == 1)
            QPainter::fillRect(m_xOffset + x, y, w, h, b);
        else
            QPainter::fillRect(m_xOffset - x - w, y, w, h, b);
    }

    void drawText(int x, int y, const QString& s, bool bAdapt = false)
    {
        Qt::LayoutDirection ld = (m_factor == 1 || !bAdapt) ? Qt::LeftToRight : Qt::RightToLeft;
        //QPainter::setLayoutDirection( ld );
        if(ld == Qt::RightToLeft) // Reverse the text
        {
            QString s2;
            for(int i = s.length() - 1; i >= 0; --i)
            {
                s2 += s[i];
            }
            QPainter::drawText(m_xOffset - m_fontWidth * s.length() + m_factor * x, y, s2);
            return;
        }
        QPainter::drawText(m_xOffset - m_fontWidth * s.length() + m_factor * x, y, s);
    }

    void drawLine(int x1, int y1, int x2, int y2)
    {
        QPainter::drawLine(m_xOffset + m_factor * x1, y1, m_xOffset + m_factor * x2, y2);
    }
Joachim Eibl's avatar
Joachim Eibl committed
402
};
Joachim Eibl's avatar
0.9.80  
Joachim Eibl committed
403

Michael Reeves's avatar
Michael Reeves committed
404 405
bool runDiff(const LineData* p1, LineRef size1, const LineData* p2, LineRef size2, DiffList& diffList, int winIdx1, int winIdx2,
             ManualDiffHelpList* pManualDiffHelpList, Options* pOptions);
406

407
bool fineDiff(
Michael Reeves's avatar
Michael Reeves committed
408 409 410 411
    Diff3LineList& diff3LineList,
    int selector,
    const LineData* v1,
    const LineData* v2);
Joachim Eibl's avatar
Joachim Eibl committed
412

Michael Reeves's avatar
Michael Reeves committed
413
bool equal(const LineData& l1, const LineData& l2, bool bStrict);
Joachim Eibl's avatar
Joachim Eibl committed
414

Michael Reeves's avatar
Michael Reeves committed
415
inline bool isWhite(QChar c)
Joachim Eibl's avatar
Joachim Eibl committed
416
{
Michael Reeves's avatar
Michael Reeves committed
417
    return c == ' ' || c == '\t' || c == '\r';
Joachim Eibl's avatar
Joachim Eibl committed
418 419 420 421
}

/** Returns the number of equivalent spaces at position outPos.
*/
Michael Reeves's avatar
Michael Reeves committed
422
inline int tabber(int outPos, int tabSize)
Joachim Eibl's avatar
Joachim Eibl committed
423
{
Michael Reeves's avatar
Michael Reeves committed
424
    return tabSize - (outPos % tabSize);
Joachim Eibl's avatar
Joachim Eibl committed
425 426 427 428 429 430
}

/** Returns a line number where the linerange [line, line+nofLines] can
    be displayed best. If it fits into the currently visible range then
    the returned value is the current firstLine.
*/
Michael Reeves's avatar
Michael Reeves committed
431
int getBestFirstLine(int line, int nofLines, int firstLine, int visibleLines);
Joachim Eibl's avatar
Joachim Eibl committed
432 433 434 435 436 437

extern bool g_bIgnoreWhiteSpace;
extern bool g_bIgnoreTrivialMatches;
extern int g_bAutoSolve;

// Cursor conversions that consider g_tabSize.
Michael Reeves's avatar
Michael Reeves committed
438 439
int convertToPosInText(const QString& s, int posOnScreen, int tabSize);
int convertToPosOnScreen(const QString& s, int posInText, int tabSize);
Joachim Eibl's avatar
Joachim Eibl committed
440

Michael Reeves's avatar
Michael Reeves committed
441 442 443 444 445 446
enum e_CoordType
{
    eFileCoords,
    eD3LLineCoords,
    eWrapCoords
};
Joachim Eibl's avatar
Joachim Eibl committed
447

Michael Reeves's avatar
Michael Reeves committed
448
void calcTokenPos(const QString&, int posOnScreen, int& pos1, int& pos2, int tabSize);
Joachim Eibl's avatar
Joachim Eibl committed
449

Michael Reeves's avatar
Michael Reeves committed
450 451
QString calcHistorySortKey(const QString& keyOrder, QRegExp& matchedRegExpr, const QStringList& parenthesesGroupList);
bool findParenthesesGroups(const QString& s, QStringList& sl);
Joachim Eibl's avatar
Joachim Eibl committed
452
#endif