Commit 07ee5372 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Remove word diff parsing

We only handle normal diff now
parent ddbabc88
......@@ -629,9 +629,6 @@ void GitWidget::showDiff(const QString &file, bool staged, bool showInKate)
if (staged) {
args.append(QStringLiteral("--staged"));
}
if (showInKate) {
// args.append(QStringLiteral("--word-diff=porcelain"));
}
if (!file.isEmpty()) {
args.append(QStringLiteral("--"));
......@@ -645,7 +642,7 @@ void GitWidget::showDiff(const QString &file, bool staged, bool showInKate)
} else {
if (showInKate) {
auto mw = mainWindow()->window();
QMetaObject::invokeMethod(mw, "showWordDiff", Q_ARG(QByteArray, git->readAllStandardOutput()), Q_ARG(QString, file), Q_ARG(QString, {}));
QMetaObject::invokeMethod(mw, "showDiff", Q_ARG(QByteArray, git->readAllStandardOutput()), Q_ARG(QString, file), Q_ARG(QString, {}));
return;
}
auto addContextMenuActions = [this, file, staged](KTextEditor::View *v) {
......@@ -1172,7 +1169,7 @@ void GitWidget::treeViewContextMenuEvent(QContextMenuEvent *e)
m_mainWin->openUrl(QUrl::fromLocalFile(*it));
}
} else if (!untracked && act == diff) {
showDiff(QString(), false);
showDiff(QString(), false, true);
}
} else if (treeItem == GitStatusModel::NodeFile) {
QMenu menu;
......
......@@ -80,9 +80,7 @@ void DiffWidget::diffDocs(KTextEditor::Document *l, KTextEditor::Document *r)
const QString right = r->url().toLocalFile();
QPointer<QProcess> git = new QProcess(this);
setupGitProcess(*git,
qApp->applicationDirPath(),
{QStringLiteral("diff"), /*QStringLiteral("--word-diff=porcelain"),*/ QStringLiteral("--no-index"), left, right});
setupGitProcess(*git, qApp->applicationDirPath(), {QStringLiteral("diff"), QStringLiteral("--no-index"), left, right});
connect(git, &QProcess::readyReadStandardOutput, this, [this, git]() {
onTextReceived(git->readAllStandardOutput());
......@@ -202,7 +200,7 @@ static void markInlineDiffs(QVector<HunkChangedLine> &hunkChangedLinesA,
hunkChangedLinesB.clear();
}
void DiffWidget::openDiff(const QByteArray &raw)
void DiffWidget::parseAndShowDiff(const QByteArray &raw)
{
// printf("show diff:\n%s\n================================", raw.constData());
const QStringList text = QString::fromUtf8(raw).replace(QStringLiteral("\r\n"), QStringLiteral("\n")).split(QLatin1Char('\n'), Qt::SkipEmptyParts);
......@@ -361,162 +359,19 @@ void DiffWidget::openDiff(const QByteArray &raw)
}
}
void DiffWidget::openWordDiff(const QByteArray &raw)
void DiffWidget::openDiff(const QByteArray &raw)
{
// printf("show diff:\n%s\n================================", raw.constData());
openDiff(raw);
return;
const QStringList text = QString::fromUtf8(raw).replace(QStringLiteral("\r\n"), QStringLiteral("\n")).split(QLatin1Char('\n'), Qt::SkipEmptyParts);
static const QRegularExpression HUNK_HEADER_RE(QStringLiteral("^@@ -([0-9,]+) \\+([0-9,]+) @@(.*)"));
QStringList left;
QStringList right;
QVector<LineHighlight> leftHlts;
QVector<LineHighlight> rightHlts;
// QVector<QPair<int, HunkData>> hunkDatas; // lineNo => HunkData
QVector<int> lineToLineNumLeft;
QVector<int> lineToLineNumRight;
int maxFound = 0;
int lineNo = 0;
for (int i = 0; i < text.size(); ++i) {
const QString &line = text.at(i);
const auto match = HUNK_HEADER_RE.match(line);
if (!match.hasMatch())
continue;
const auto oldRange = parseRange(match.captured(1));
const auto newRange = parseRange(match.captured(2));
lineToLineNumLeft.append(-1);
lineToLineNumRight.append(-1);
left.append(line);
right.append(line);
lineNo++;
left.append(QString());
right.append(QString());
const int srcStart = oldRange.first;
int srcLine = oldRange.first;
const int oldCount = oldRange.second;
const int tgtStart = newRange.first;
int tgtLine = newRange.first;
const int newCount = newRange.second;
maxFound = qMax(qMax(srcStart + oldCount, tgtStart + newCount), maxFound);
const int oldSize = left.size();
for (int j = i + 1; j < text.size(); j++) {
QString l = text.at(j);
if (l.startsWith(QLatin1Char(' '))) {
l = l.mid(1);
left.back().append(l);
right.back().append(l);
} else if (l.startsWith(QLatin1Char('+'))) {
// qDebug() << "- line";
l = l.mid(1);
LineHighlight h;
h.line = lineNo;
h.added = true;
h.changes.push_back({right.back().size(), l.size()});
if (!rightHlts.isEmpty() && rightHlts.back().line == lineNo) {
rightHlts.back().changes.append(h.changes);
} else {
rightHlts.push_back(h);
}
right.back().append(l);
} else if (l.startsWith(QLatin1Char('-'))) {
l = l.mid(1);
// qDebug() << "+ line: " << l;
LineHighlight h;
h.line = lineNo;
h.added = false;
h.changes.push_back({left.back().size(), l.size()});
if (!leftHlts.isEmpty() && leftHlts.back().line == lineNo) {
leftHlts.back().changes.append(h.changes);
} else {
leftHlts.push_back(h);
}
left.back().append(l);
} else if (l.startsWith(QLatin1Char('~'))) {
left.append(QString());
right.append(QString());
lineToLineNumLeft.append(tgtLine++);
lineToLineNumRight.append(srcLine++);
lineNo++;
} else if (l.startsWith(QStringLiteral("@@ ")) && HUNK_HEADER_RE.match(l).hasMatch()) {
i = j - 1;
// add line number for current line
lineToLineNumLeft.append(tgtStart);
lineToLineNumRight.append(srcStart);
// add new line
left.append(QString());
right.append(QString());
lineNo++;
// line number for this line
lineToLineNumLeft.append(-1);
lineToLineNumRight.append(-1);
lineNo++;
break;
}
if (j + 1 >= text.size()) {
lineToLineNumLeft.append(tgtLine++);
lineToLineNumRight.append(srcLine++);
i = j; // ensure outer loop also exits after this
}
}
// Adjust line numbers
Q_ASSERT(left.size() == right.size() && left.size() == lineToLineNumLeft.size() && right.size() == lineToLineNumRight.size());
const int newSize = left.size();
int s = srcStart;
int t = tgtStart;
for (int i = oldSize - 1; i < newSize; ++i) {
if (left.at(i).isNull() || t > tgtStart + newCount) {
lineToLineNumLeft[i] = -1;
} else {
lineToLineNumLeft[i] = t++;
}
if (right.at(i).isNull() || s > srcStart + oldCount) {
lineToLineNumRight[i] = -1;
} else {
lineToLineNumRight[i] = s++;
}
}
qDebug() << srcStart << tgtStart << s << t << oldCount << newCount;
}
Q_ASSERT(left.size() == right.size() && left.size() == lineToLineNumLeft.size() && right.size() == lineToLineNumRight.size());
QString leftText = left.join(QLatin1Char('\n'));
QString rightText = right.join(QLatin1Char('\n'));
// printf("(%d), lt %d ln %d -- rt %d rn %d\n", lineNo, left.size(), lineToLineNumLeft.size(), right.size(), lineToLineNumRight.size());
m_left->appendData(leftHlts);
m_right->appendData(rightHlts);
m_left->appendPlainText(leftText);
m_right->appendPlainText(rightText);
m_left->setLineNumberData(lineToLineNumLeft, maxFound);
m_right->setLineNumberData(lineToLineNumRight, maxFound);
parseAndShowDiff(raw);
m_left->verticalScrollBar()->setValue(0);
m_right->verticalScrollBar()->setValue(0);
}
void DiffWidget::onTextReceived(const QByteArray &raw)
{
// printf("Got Text: \n%s\n==============\n", raw.constData());
openDiff(raw);
// openWordDiff(raw);
parseAndShowDiff(raw);
}
void DiffWidget::onError(const QByteArray &error, int /*code*/)
void DiffWidget::onError(const QByteArray & /*error*/, int /*code*/)
{
// printf("Got error: \n%s\n==============\n", error.constData());
}
......@@ -22,7 +22,6 @@ public:
void diffDocs(KTextEditor::Document *l, KTextEditor::Document *r);
void openDiff(const QByteArray &diff);
void openWordDiff(const QByteArray &diff);
Q_INVOKABLE bool shouldClose()
{
......@@ -32,6 +31,7 @@ public:
private:
void onTextReceived(const QByteArray &text);
void onError(const QByteArray &error, int code);
void parseAndShowDiff(const QByteArray &raw);
class DiffEditor *m_left;
class DiffEditor *m_right;
......
......@@ -1307,7 +1307,7 @@ void KateMainWindow::addWidgetAsTab(QWidget *widget)
vs->addWidgetAsTab(widget);
}
void KateMainWindow::showWordDiff(const QByteArray &wordDiff, const QString &fileName1, const QString &fileName2)
void KateMainWindow::showDiff(const QByteArray &wordDiff, const QString &fileName1, const QString &fileName2)
{
auto getFileName = [](const QString &s) {
int lastSlash = s.lastIndexOf(QLatin1Char('/'));
......@@ -1320,7 +1320,7 @@ void KateMainWindow::showWordDiff(const QByteArray &wordDiff, const QString &fil
else
w->setWindowTitle(i18n("Diff %1..%2", getFileName(fileName1), getFileName(fileName2)));
addWidgetAsTab(w);
w->openWordDiff(wordDiff);
w->openDiff(wordDiff);
}
void KateMainWindow::mousePressEvent(QMouseEvent *e)
......
......@@ -512,7 +512,7 @@ public Q_SLOTS:
void addWidgetAsTab(QWidget *widget);
void showWordDiff(const QByteArray &wordDiff, const QString &fileName1, const QString &fileName2);
void showDiff(const QByteArray &wordDiff, const QString &fileName1, const QString &fileName2);
private Q_SLOTS:
void slotUpdateBottomViewBar();
......
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