diff.h 13.3 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

Joachim Eibl's avatar
Joachim Eibl committed
24 25
// 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
26
class Diff
Joachim Eibl's avatar
Joachim Eibl committed
27
{
Michael Reeves's avatar
Michael Reeves committed
28 29 30 31 32 33 34 35 36 37 38 39
  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
40 41 42 43
};

typedef std::list<Diff> DiffList;

Michael Reeves's avatar
Michael Reeves committed
44
class LineData
Joachim Eibl's avatar
Joachim Eibl committed
45
{
46 47 48 49 50 51 52 53 54 55 56 57 58 59
   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
60 61
    int width(int tabSize) const; // Calcs width considering tabs.
    //int occurrences;
62
    bool whiteLine() const { return pFirstNonWhiteChar - pLine == mSize; }
Joachim Eibl's avatar
Joachim Eibl committed
63 64 65 66 67
};

class Diff3LineList;
class Diff3LineVector;

Michael Reeves's avatar
Michael Reeves committed
68
class DiffBufferInfo
Joachim Eibl's avatar
Joachim Eibl committed
69
{
Michael Reeves's avatar
Michael Reeves committed
70 71 72 73 74 75 76 77 78 79 80
  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
81 82
};

Michael Reeves's avatar
Michael Reeves committed
83
class Diff3Line
Joachim Eibl's avatar
Joachim Eibl committed
84
{
85
  private:
Michael Reeves's avatar
Michael Reeves committed
86 87 88
    LineRef lineA = -1;
    LineRef lineB = -1;
    LineRef lineC = -1;
89
  public:
Michael Reeves's avatar
Michael Reeves committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

    bool bAEqC : 1; // These are true if equal or only white-space changes exist.
    bool bBEqC : 1;
    bool bAEqB : 1;

    bool bWhiteLineA : 1;
    bool bWhiteLineB : 1;
    bool bWhiteLineC : 1;

    DiffList* pFineAB; // These are 0 only if completely equal or if either source doesn't exist.
    DiffList* pFineBC;
    DiffList* pFineCA;

    int linesNeededForDisplay;    // Due to wordwrap
    int sumLinesNeededForDisplay; // For fast conversion to m_diff3WrapLineVector

    DiffBufferInfo* m_pDiffBufferInfo; // For convenience

    Diff3Line()
    {
        lineA = -1;
        lineB = -1;
        lineC = -1;
        bAEqC = false;
        bAEqB = false;
        bBEqC = false;
        pFineAB = nullptr;
        pFineBC = nullptr;
        pFineCA = nullptr;
        linesNeededForDisplay = 1;
        sumLinesNeededForDisplay = 0;
        bWhiteLineA = false;
        bWhiteLineB = false;
        bWhiteLineC = false;
        m_pDiffBufferInfo = nullptr;
    }

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

137 138 139 140 141 142 143 144
    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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    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)
162
            return QString(pld->getLine(), pld->size());
Michael Reeves's avatar
Michael Reeves committed
163 164 165 166 167 168 169 170 171 172
        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;
    }
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

    bool fineDiff(const int selector, const LineData* v1, const LineData* v2);
  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
198 199
};

200
class Diff3LineList : public QList<Diff3Line>
Joachim Eibl's avatar
Joachim Eibl committed
201
{
202 203 204 205
  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
206
};
207

208
class Diff3LineVector : public QVector<Diff3Line*>
Joachim Eibl's avatar
Joachim Eibl committed
209 210
{
};
Joachim Eibl's avatar
0.9.86  
Joachim Eibl committed
211 212 213

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

221
typedef QVector<Diff3WrapLine> Diff3WrapLineVector;
Joachim Eibl's avatar
0.9.86  
Joachim Eibl committed
222

Joachim Eibl's avatar
Joachim Eibl committed
223 224
class TotalDiffStatus
{
Michael Reeves's avatar
Michael Reeves committed
225 226 227 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
  public:
    TotalDiffStatus() { reset(); }
    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
264 265 266
    int nofUnsolvedConflicts = 0;
    int nofSolvedConflicts = 0;
    int nofWhitespaceConflicts = 0;
Joachim Eibl's avatar
Joachim Eibl committed
267 268
};

269
class ManualDiffHelpList; // A list of corresponding ranges
Joachim Eibl's avatar
Joachim Eibl committed
270 271 272
// Three corresponding ranges. (Minimum size of a valid range is one line.)
class ManualDiffHelpEntry
{
273
  private:
Michael Reeves's avatar
Michael Reeves committed
274 275 276 277 278 279
    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
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  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;
    }
298 299

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

    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;
        }
    }

316 317
    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;}
318
    bool isValidMove(int line1, int line2, int winIdx1, int winIdx2) const;
Joachim Eibl's avatar
Joachim Eibl committed
319 320 321
};

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

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

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

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

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

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

Joachim Eibl's avatar
Joachim Eibl committed
345

Joachim Eibl's avatar
Joachim Eibl committed
346 347 348
// Helper class that swaps left and right for some commands.
class MyPainter : public QPainter
{
Michael Reeves's avatar
Michael Reeves committed
349 350 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
    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
400
};
Joachim Eibl's avatar
0.9.80  
Joachim Eibl committed
401

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

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

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

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

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

/** 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
429
int getBestFirstLine(int line, int nofLines, int firstLine, int visibleLines);
Joachim Eibl's avatar
Joachim Eibl committed
430 431 432 433 434 435

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
436 437
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
438

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

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

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