-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitDiffDialog.cpp
More file actions
132 lines (113 loc) · 4.15 KB
/
Copy pathGitDiffDialog.cpp
File metadata and controls
132 lines (113 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
#include "git/GitDiffDialog.h"
#include <QFileInfo>
#include <QFont>
#include <QHBoxLayout>
#include <QLabel>
#include <QPlainTextEdit>
#include <QSplitter>
#include <QVBoxLayout>
namespace cold {
GitDiffDialog::GitDiffDialog(QWidget* parent) : QDialog(parent) {
setWindowTitle(QStringLiteral("Diff"));
resize(900, 600);
auto* layout = new QVBoxLayout(this);
auto* headerLayout = new QHBoxLayout();
headerLayout->addWidget(new QLabel(QStringLiteral("Index / HEAD"), this));
headerLayout->addStretch();
headerLayout->addWidget(new QLabel(QStringLiteral("Working tree"), this));
layout->addLayout(headerLayout);
m_truncationLabel = new QLabel(this);
m_truncationLabel->setVisible(false);
m_truncationLabel->setWordWrap(true);
layout->addWidget(m_truncationLabel);
m_splitter = new QSplitter(Qt::Horizontal, this);
m_leftPane = new QPlainTextEdit(m_splitter);
m_rightPane = new QPlainTextEdit(m_splitter);
m_leftPane->setReadOnly(true);
m_rightPane->setReadOnly(true);
m_leftPane->setLineWrapMode(QPlainTextEdit::NoWrap);
m_rightPane->setLineWrapMode(QPlainTextEdit::NoWrap);
QFont mono = m_leftPane->font();
mono.setStyleHint(QFont::Monospace);
mono.setFamily(QStringLiteral("monospace"));
m_leftPane->setFont(mono);
m_rightPane->setFont(mono);
m_splitter->addWidget(m_leftPane);
m_splitter->addWidget(m_rightPane);
m_splitter->setStretchFactor(0, 1);
m_splitter->setStretchFactor(1, 1);
layout->addWidget(m_splitter, 1);
}
void GitDiffDialog::showDiff(const QString& filePath, const QString& unifiedDiff) {
setWindowTitle(QStringLiteral("Diff — %1").arg(QFileInfo(filePath).fileName()));
QString left;
QString right;
bool truncated = false;
int omitted = 0;
parseUnifiedDiff(unifiedDiff, &left, &right, &truncated, &omitted);
m_leftPane->setPlainText(left.isEmpty() && right.isEmpty() ? QStringLiteral("(No changes)")
: left);
m_rightPane->setPlainText(right.isEmpty() && left.isEmpty() ? QStringLiteral("(No changes)")
: right);
if (truncated) {
m_truncationLabel->setText(
QStringLiteral("Diff truncated (%1 lines omitted)").arg(omitted));
m_truncationLabel->setVisible(true);
} else {
m_truncationLabel->setVisible(false);
}
show();
raise();
activateWindow();
}
void GitDiffDialog::parseUnifiedDiff(const QString& diff, QString* leftOut, QString* rightOut,
bool* truncatedOut, int* omittedOut) {
QStringList leftLines;
QStringList rightLines;
int totalLines = 0;
bool truncated = false;
int omitted = 0;
const QStringList lines = diff.split(QLatin1Char('\n'));
for (const QString& line : lines) {
if (line.startsWith(QStringLiteral("---")) || line.startsWith(QStringLiteral("+++")) ||
line.startsWith(QStringLiteral("@@")) ||
line.startsWith(QStringLiteral("\\ No newline"))) {
continue;
}
if (line.startsWith(QLatin1Char('-'))) {
if (totalLines < kMaxLines) {
leftLines.append(line.mid(1));
++totalLines;
} else {
truncated = true;
++omitted;
}
} else if (line.startsWith(QLatin1Char('+'))) {
if (totalLines < kMaxLines) {
rightLines.append(line.mid(1));
++totalLines;
} else {
truncated = true;
++omitted;
}
} else if (!line.isEmpty()) {
if (totalLines < kMaxLines) {
leftLines.append(line);
rightLines.append(line);
++totalLines;
} else {
truncated = true;
++omitted;
}
}
}
*leftOut = leftLines.join(QLatin1Char('\n'));
*rightOut = rightLines.join(QLatin1Char('\n'));
if (truncatedOut) {
*truncatedOut = truncated;
}
if (omittedOut) {
*omittedOut = omitted;
}
}
} // namespace cold