Commit dc2e3435 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Diff: Show file names in multifile diffs

parent e59246d5
Pipeline #227094 passed with stage
in 6 minutes and 10 seconds
......@@ -648,6 +648,9 @@ void GitWidget::showDiff(const QString &file, bool staged)
d.flags.setFlag(DiffParams::Flag::ShowStage, !staged);
d.flags.setFlag(DiffParams::Flag::ShowUnstage, staged);
d.flags.setFlag(DiffParams::Flag::ShowDiscard, !staged);
// When file is empty, we are showing diff of multiple file usually
const bool showfile = file.isEmpty() && (staged ? m_model->stagedFiles().size() > 1 : m_model->changedFiles().size() > 1);
d.flags.setFlag(DiffParams::Flag::ShowFileName, showfile);
QMetaObject::invokeMethod(mw, "showDiff", Q_ARG(QByteArray, git->readAllStandardOutput()), Q_ARG(DiffParams, d));
}
git->deleteLater();
......
......@@ -294,6 +294,20 @@ void DiffEditor::paintEvent(QPaintEvent *e)
p.restore();
}
if (m_diffWidget->isFileNameLine(block.blockNumber())) {
p.save();
QPen pen;
pen.setColor(hunkSeparatorColor);
pen.setWidthF(1.1);
p.setPen(pen);
p.setBrush(Qt::NoBrush);
auto rCopy = r;
rCopy.setRight(block.layout()->lineAt(0).naturalTextRect().right() + 4);
rCopy.setLeft(rCopy.left() - 2);
p.drawRect(rCopy);
p.restore();
}
offset.ry() += r.height();
if (offset.y() > viewportRect.height()) {
break;
......
......@@ -9,7 +9,16 @@
#include <QStringList>
struct DiffParams {
enum Flag { ShowStage = 1, ShowUnstage = 2, ShowDiscard = 4 };
enum Flag {
/** show stage action in context menu **/
ShowStage = 1,
/** show unstage action in context menu **/
ShowUnstage = 2,
/** show discard action in context menu **/
ShowDiscard = 4,
/** show filename with diff. Appears right before hunk heading **/
ShowFileName,
};
Q_DECLARE_FLAGS(Flags, Flag)
Q_FLAGS(Flags)
......
......@@ -231,6 +231,7 @@ void DiffWidget::clearData()
m_lineToRawDiffLine.clear();
m_lineToDiffHunkLine.clear();
m_params.clear();
m_linesWithFileName.clear();
}
void DiffWidget::diffDocs(KTextEditor::Document *l, KTextEditor::Document *r)
......@@ -437,6 +438,7 @@ void DiffWidget::parseAndShowDiff(const QByteArray &raw)
QVector<ViewLineToDiffLine> lineToRawDiffLine;
// for Folding/stage/unstage hunk
QVector<ViewLineToDiffLine> linesWithHunkHeading;
QVector<int> linesWithFileName;
int maxLineNoFound = 0;
int lineA = 0;
......@@ -444,15 +446,26 @@ void DiffWidget::parseAndShowDiff(const QByteArray &raw)
for (int i = 0; i < text.size(); ++i) {
const QString &line = text.at(i);
auto match = DIFF_FILENAME_RE.match(line);
if (match.hasMatch()) {
if (line.startsWith(QLatin1Char('-'))) {
srcFile = match.captured(1);
} else if (line.startsWith(QLatin1Char('+'))) {
if (match.hasMatch() && i + 1 < text.size()) {
srcFile = match.captured(1);
mimeTypes.insert(QMimeDatabase().mimeTypeForFile(match.captured(1), QMimeDatabase::MatchExtension).name());
auto match = DIFF_FILENAME_RE.match(text.at(i + 1));
if (match.hasMatch()) {
tgtFile = match.captured(1);
}
if (!match.captured(1).isEmpty()) {
mimeTypes.insert(QMimeDatabase().mimeTypeForFile(match.captured(1), QMimeDatabase::MatchExtension).name());
}
i++;
if (m_params.flags.testFlag(DiffParams::ShowFileName)) {
left.append(Utils::fileNameFromPath(srcFile));
right.append(Utils::fileNameFromPath(tgtFile));
linesWithFileName.append(lineA);
lineNumsA.append(-1);
lineNumsB.append(-1);
lineA++;
lineB++;
}
continue;
}
......@@ -578,6 +591,7 @@ void DiffWidget::parseAndShowDiff(const QByteArray &raw)
m_right->setLineNumberData(lineNumsB, maxLineNoFound);
m_lineToRawDiffLine += lineToRawDiffLine;
m_lineToDiffHunkLine += linesWithHunkHeading;
m_linesWithFileName += linesWithFileName;
const auto defs = defsForMimeTypes(mimeTypes);
leftHl->setDefinition(defs.constFirst());
......@@ -609,6 +623,8 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
QVector<ViewLineToDiffLine> lineToRawDiffLine;
// for Folding/stage/unstage hunk
QVector<ViewLineToDiffLine> linesWithHunkHeading;
// Lines containing filename
QVector<int> linesWithFileName;
QSet<QString> mimeTypes;
QString srcFile;
......@@ -620,13 +636,23 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
for (int i = 0; i < text.size(); ++i) {
const QString &line = text.at(i);
auto match = DIFF_FILENAME_RE.match(line);
srcFile = match.captured(1);
} else if (line.startsWith(QLatin1Char('+'))) {
if (match.hasMatch() && i + 1 < text.size()) {
srcFile = match.captured(1);
mimeTypes.insert(QMimeDatabase().mimeTypeForFile(match.captured(1), QMimeDatabase::MatchExtension).name());
auto match = DIFF_FILENAME_RE.match(text.at(i + 1));
if (match.hasMatch()) {
tgtFile = match.captured(1);
}
if (!match.captured(1).isEmpty()) {
mimeTypes.insert(QMimeDatabase().mimeTypeForFile(match.captured(1), QMimeDatabase::MatchExtension).name());
}
i++;
if (m_params.flags.testFlag(DiffParams::ShowFileName)) {
lines.append(QStringLiteral("%1 → %2").arg(Utils::fileNameFromPath(srcFile), Utils::fileNameFromPath(tgtFile)));
lineNums.append(-1);
linesWithFileName.append(lineNo);
lineNo++;
}
continue;
}
......@@ -636,8 +662,8 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
const auto oldRange = parseRange(match.captured(1));
const auto newRange = parseRange(match.captured(2));
const QString headingLeft = QStringLiteral("@@ ") + match.captured(1) + match.captured(3) /* + QStringLiteral(" ") + srcFile*/;
const QString headingRight = QStringLiteral("@@ ") + match.captured(2) + match.captured(3) /* + QStringLiteral(" ") + tgtFile*/;
// const QString headingLeft = QStringLiteral("@@ ") + match.captured(1) + match.captured(3) /* + QStringLiteral(" ") + srcFile*/;
// const QString headingRight = QStringLiteral("@@ ") + match.captured(2) + match.captured(3) /* + QStringLiteral(" ") + tgtFile*/;
lines.push_back(line);
lineNums.append(-1);
......@@ -722,8 +748,9 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
m_left->appendData(hlts);
m_left->appendPlainText(lines.join(QLatin1Char('\n')));
m_left->setLineNumberData(lineNums, maxLineNoFound);
m_lineToDiffHunkLine = linesWithHunkHeading;
m_lineToRawDiffLine = lineToRawDiffLine;
m_lineToDiffHunkLine += linesWithHunkHeading;
m_lineToRawDiffLine += lineToRawDiffLine;
m_linesWithFileName += linesWithFileName;
const auto defs = defsForMimeTypes(mimeTypes);
leftHl->setDefinition(defs.constFirst());
......
......@@ -30,6 +30,10 @@ public:
}
bool isHunk(int line) const;
bool isFileNameLine(int line) const
{
return m_linesWithFileName.contains(line);
}
private:
void clearData();
......@@ -66,4 +70,5 @@ private:
QByteArray m_rawDiff; // Raw diff saved as is
QVector<ViewLineToDiffLine> m_lineToRawDiffLine;
QVector<ViewLineToDiffLine> m_lineToDiffHunkLine;
QVector<int> m_linesWithFileName;
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment