diffutils.h 5.4 KB
Newer Older
1 2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16 17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
27 28 29 30 31 32 33
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#ifndef DIFFUTILS_H
#define DIFFUTILS_H

jkobus's avatar
jkobus committed
34 35
#include "diffeditor_global.h"

36 37
#include <QString>
#include <QMap>
38 39

namespace TextEditor { class FontSettings; }
40 41 42 43 44

namespace DiffEditor {

class Diff;

jkobus's avatar
jkobus committed
45 46
class DIFFEDITOR_EXPORT DiffFileInfo {
public:
47 48
    DiffFileInfo() {}
    DiffFileInfo(const QString &file) : fileName(file) {}
jkobus's avatar
jkobus committed
49
    DiffFileInfo(const QString &file, const QString &type)
50
        : fileName(file), typeInfo(type) {}
jkobus's avatar
jkobus committed
51 52 53
    QString fileName;
    QString typeInfo;
};
54

jkobus's avatar
jkobus committed
55
class DIFFEDITOR_EXPORT TextLineData {
56 57 58 59 60 61 62 63 64 65 66
public:
    enum TextLineType {
        TextLine,
        Separator,
        Invalid
    };
    TextLineData() : textLineType(Invalid) {}
    TextLineData(const QString &txt) : textLineType(TextLine), text(txt) {}
    TextLineData(TextLineType t) : textLineType(t) {}
    TextLineType textLineType;
    QString text;
67 68 69 70 71 72 73
    /*
     * <start position, end position>
     * <-1, n> means this is a continuation from the previous line
     * <n, -1> means this will be continued in the next line
     * <-1, -1> the whole line is a continuation (from the previous line to the next line)
     */
    QMap<int, int> changedPositions; // counting from the beginning of the line
74 75
};

jkobus's avatar
jkobus committed
76
class DIFFEDITOR_EXPORT RowData {
77 78 79 80 81 82 83 84 85 86 87
public:
    RowData() : equal(false) {}
    RowData(const TextLineData &l)
        : leftLine(l), rightLine(l), equal(true) {}
    RowData(const TextLineData &l, const TextLineData &r)
        : leftLine(l), rightLine(r), equal(false) {}
    TextLineData leftLine;
    TextLineData rightLine;
    bool equal;
};

jkobus's avatar
jkobus committed
88
class DIFFEDITOR_EXPORT ChunkData {
89
public:
jkobus's avatar
jkobus committed
90 91
    ChunkData() : contextChunk(false),
        leftStartingLineNumber(0), rightStartingLineNumber(0) {}
92 93
    QList<RowData> rows;
    bool contextChunk;
jkobus's avatar
jkobus committed
94 95
    int leftStartingLineNumber;
    int rightStartingLineNumber;
96
    QString contextInfo;
97 98
};

jkobus's avatar
jkobus committed
99
class DIFFEDITOR_EXPORT FileData {
100
public:
101 102 103 104 105 106 107 108
    enum FileOperation {
        ChangeFile,
        NewFile,
        DeleteFile,
        CopyFile,
        RenameFile
    };

jkobus's avatar
jkobus committed
109
    FileData()
110 111
        : fileOperation(ChangeFile),
          binaryFiles(false),
jkobus's avatar
jkobus committed
112 113 114
          lastChunkAtTheEndOfFile(false),
          contextChunksIncluded(false) {}
    FileData(const ChunkData &chunkData)
115 116
        : fileOperation(ChangeFile),
          binaryFiles(false),
jkobus's avatar
jkobus committed
117 118
          lastChunkAtTheEndOfFile(false),
          contextChunksIncluded(false) { chunks.append(chunkData); }
119
    QList<ChunkData> chunks;
jkobus's avatar
jkobus committed
120 121
    DiffFileInfo leftFileInfo;
    DiffFileInfo rightFileInfo;
122
    FileOperation fileOperation;
jkobus's avatar
jkobus committed
123 124 125 126 127 128 129
    bool binaryFiles;
    bool lastChunkAtTheEndOfFile;
    bool contextChunksIncluded;
};

class DIFFEDITOR_EXPORT DiffUtils {
public:
130 131 132 133
    enum PatchFormattingFlags {
        AddLevel = 0x1, // Add 'a/' , '/b' for git am
        GitFormat = AddLevel | 0x2, // Add line 'diff ..' as git does
    };
jkobus's avatar
jkobus committed
134 135 136 137

    static ChunkData calculateOriginalData(const QList<Diff> &leftDiffList,
                                           const QList<Diff> &rightDiffList);
    static FileData calculateContextData(const ChunkData &originalData,
138
                                         int contextLineCount,
jkobus's avatar
jkobus committed
139 140 141 142 143
                                         int joinChunkThreshold = 1);
    static QString makePatchLine(const QChar &startLineCharacter,
                                 const QString &textLine,
                                 bool lastChunk,
                                 bool lastLine);
144 145
    static QString makePatch(const ChunkData &chunkData,
                             bool lastChunk = false);
jkobus's avatar
jkobus committed
146 147 148 149
    static QString makePatch(const ChunkData &chunkData,
                             const QString &leftFileName,
                             const QString &rightFileName,
                             bool lastChunk = false);
150 151
    static QString makePatch(const QList<FileData> &fileDataList,
                             unsigned formatFlags = 0);
jkobus's avatar
jkobus committed
152 153
    static QList<FileData> readPatch(const QString &patch,
                                     bool *ok = 0);
154 155 156 157 158
};

} // namespace DiffEditor

#endif // DIFFUTILS_H