diff.h 11.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 17
#include <QLinkedList>
#include <QVector>
18 19
#include <QTemporaryFile>

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

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

typedef std::list<Diff> DiffList;

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

class Diff3LineList;
class Diff3LineVector;

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

Michael Reeves's avatar
Michael Reeves committed
85
class Diff3Line
Joachim Eibl's avatar
Joachim Eibl committed
86
{
Michael Reeves's avatar
Michael Reeves committed
87 88 89 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  public:
    LineRef lineA = -1;
    LineRef lineB = -1;
    LineRef lineC = -1;

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

    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)
155
            return QString(pld->getLine(), pld->size());
Michael Reeves's avatar
Michael Reeves committed
156 157 158 159 160 161 162 163 164 165
        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;
    }
Joachim Eibl's avatar
Joachim Eibl committed
166 167
};

168
class Diff3LineList : public QLinkedList<Diff3Line>
Joachim Eibl's avatar
Joachim Eibl committed
169 170
{
};
171
class Diff3LineVector : public QVector<Diff3Line*>
Joachim Eibl's avatar
Joachim Eibl committed
172 173
{
};
Joachim Eibl's avatar
0.9.86  
Joachim Eibl committed
174 175 176

class Diff3WrapLine
{
Michael Reeves's avatar
Michael Reeves committed
177 178 179 180 181
  public:
    Diff3Line* pD3L;
    int diff3LineIndex;
    int wrapLineOffset;
    int wrapLineLength;
Joachim Eibl's avatar
0.9.86  
Joachim Eibl committed
182 183
};

184
typedef QVector<Diff3WrapLine> Diff3WrapLineVector;
Joachim Eibl's avatar
0.9.86  
Joachim Eibl committed
185

Joachim Eibl's avatar
Joachim Eibl committed
186 187
class TotalDiffStatus
{
Michael Reeves's avatar
Michael Reeves committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  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:
    int nofUnsolvedConflicts;
    int nofSolvedConflicts;
    int nofWhitespaceConflicts;
Joachim Eibl's avatar
Joachim Eibl committed
230 231
};

Joachim Eibl's avatar
Joachim Eibl committed
232 233 234
// Three corresponding ranges. (Minimum size of a valid range is one line.)
class ManualDiffHelpEntry
{
Michael Reeves's avatar
Michael Reeves committed
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 266 267
  public:
    ManualDiffHelpEntry()
    {
        lineA1 = -1;
        lineA2 = -1;
        lineB1 = -1;
        lineB2 = -1;
        lineC1 = -1;
        lineC2 = -1;
    }
    LineRef lineA1;
    LineRef lineA2;
    LineRef lineB1;
    LineRef lineB2;
    LineRef lineC1;
    LineRef lineC2;
    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;
    }
Joachim Eibl's avatar
Joachim Eibl committed
268 269 270 271 272
};

// A list of corresponding ranges
typedef std::list<ManualDiffHelpEntry> ManualDiffHelpList;

Joachim Eibl's avatar
0.9.80  
Joachim Eibl committed
273
void calcDiff3LineListUsingAB(
Michael Reeves's avatar
Michael Reeves committed
274 275
    const DiffList* pDiffListAB,
    Diff3LineList& d3ll);
Joachim Eibl's avatar
Joachim Eibl committed
276 277

void calcDiff3LineListUsingAC(
Michael Reeves's avatar
Michael Reeves committed
278 279
    const DiffList* pDiffListAC,
    Diff3LineList& d3ll);
Joachim Eibl's avatar
Joachim Eibl committed
280

Joachim Eibl's avatar
Joachim Eibl committed
281
void calcDiff3LineListUsingBC(
Michael Reeves's avatar
Michael Reeves committed
282 283
    const DiffList* pDiffListBC,
    Diff3LineList& d3ll);
Joachim Eibl's avatar
Joachim Eibl committed
284

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

Michael Reeves's avatar
Michael Reeves committed
287 288
void calcDiff3LineListTrim(Diff3LineList& d3ll, const LineData* pldA, const LineData* pldB, const LineData* pldC, ManualDiffHelpList* pManualDiffHelpList);
void calcWhiteDiff3Lines(Diff3LineList& d3ll, const LineData* pldA, const LineData* pldB, const LineData* pldC);
Joachim Eibl's avatar
Joachim Eibl committed
289

Michael Reeves's avatar
Michael Reeves committed
290
void calcDiff3LineVector(Diff3LineList& d3ll, Diff3LineVector& d3lv);
Joachim Eibl's avatar
Joachim Eibl committed
291

Joachim Eibl's avatar
Joachim Eibl committed
292 293 294
// Helper class that swaps left and right for some commands.
class MyPainter : public QPainter
{
Michael Reeves's avatar
Michael Reeves committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    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
346
};
Joachim Eibl's avatar
0.9.80  
Joachim Eibl committed
347

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

351
bool fineDiff(
Michael Reeves's avatar
Michael Reeves committed
352 353 354 355
    Diff3LineList& diff3LineList,
    int selector,
    const LineData* v1,
    const LineData* v2);
Joachim Eibl's avatar
Joachim Eibl committed
356

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

Michael Reeves's avatar
Michael Reeves committed
359
inline bool isWhite(QChar c)
Joachim Eibl's avatar
Joachim Eibl committed
360
{
Michael Reeves's avatar
Michael Reeves committed
361
    return c == ' ' || c == '\t' || c == '\r';
Joachim Eibl's avatar
Joachim Eibl committed
362 363 364 365
}

/** Returns the number of equivalent spaces at position outPos.
*/
Michael Reeves's avatar
Michael Reeves committed
366
inline int tabber(int outPos, int tabSize)
Joachim Eibl's avatar
Joachim Eibl committed
367
{
Michael Reeves's avatar
Michael Reeves committed
368
    return tabSize - (outPos % tabSize);
Joachim Eibl's avatar
Joachim Eibl committed
369 370 371 372 373 374
}

/** 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
375
int getBestFirstLine(int line, int nofLines, int firstLine, int visibleLines);
Joachim Eibl's avatar
Joachim Eibl committed
376 377 378 379 380 381

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
382 383
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
384

Michael Reeves's avatar
Michael Reeves committed
385 386 387 388 389 390
enum e_CoordType
{
    eFileCoords,
    eD3LLineCoords,
    eWrapCoords
};
Joachim Eibl's avatar
Joachim Eibl committed
391

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

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