Commit 30a9ef88 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Use a single fixed view for viewing diffs


Signed-off-by: Waqar Ahmed's avatarWaqar Ahmed <waqar.17a@gmail.com>
parent 68bcb8d3
......@@ -259,6 +259,6 @@ void FileHistoryWidget::itemClicked(const QModelIndex &idx)
contents.append(git.readAllStandardOutput());
// we send this signal to the parent, which will pass it on to
// the GitWidget from where a temporary file is opened
Q_EMIT commitClicked(fi.fileName(), /*QStringLiteral("XXXXXX %1.diff"),*/ contents);
Q_EMIT commitClicked(contents);
}
}
......@@ -28,7 +28,7 @@ private:
Q_SIGNALS:
void backClicked();
void commitClicked(const QString &file, const QByteArray &contents);
void commitClicked(const QByteArray &contents);
void errorMessage(const QString &msg, bool warn);
};
......
......@@ -39,6 +39,8 @@
#include <KMessageBox>
#include <QPointer>
#include <KSyntaxHighlighting/Definition>
#include <KSyntaxHighlighting/Repository>
#include <KTextEditor/Application>
#include <KTextEditor/ConfigInterface>
#include <KTextEditor/Editor>
......@@ -279,11 +281,6 @@ KTextEditor::MainWindow *GitWidget::mainWindow()
return m_mainWin;
}
std::vector<GitWidget::TempFileViewPair> *GitWidget::tempFilesVector()
{
return &m_tempFiles;
}
QProcess *GitWidget::gitp()
{
auto git = new QProcess(this);
......@@ -426,7 +423,12 @@ void GitWidget::openAtHEAD(const QString &file)
if (es != QProcess::NormalExit || exitCode != 0) {
sendMessage(i18n("Failed to open file at HEAD: %1", QString::fromUtf8(git->readAllStandardError())), true);
} else {
openTempFile(QFileInfo(file).fileName(), QStringLiteral("XXXXXX - (HEAD) - %1"), git->readAllStandardOutput());
auto view = m_mainWin->openUrl(QUrl());
if (view && view->document()) {
view->document()->setText(QString::fromUtf8(git->readAllStandardOutput()));
auto mode = KTextEditor::Editor::instance()->repository().definitionForFileName(file).name();
view->document()->setHighlightingMode(mode);
}
}
git->deleteLater();
});
......@@ -454,44 +456,38 @@ void GitWidget::showDiff(const QString &file, bool staged)
} else {
const QString filename = file.isEmpty() ? QString() : QFileInfo(file).fileName();
openTempFile(filename, QStringLiteral("XXXXXX %1.diff"), git->readAllStandardOutput());
auto addContextMenuActions = [this, file, staged](KTextEditor::View *v) {
auto m = v->contextMenu();
if (!staged) {
QMenu *menu = new QMenu(v);
auto sh = menu->addAction(i18n("Stage Hunk"));
auto sl = menu->addAction(i18n("Stage Lines"));
menu->addActions(m->actions());
v->setContextMenu(menu);
connect(sh, &QAction::triggered, v, [=] {
applyDiff(file, false, true, v);
});
connect(sl, &QAction::triggered, v, [=] {
applyDiff(file, false, false, v);
});
} else {
QMenu *menu = new QMenu(v);
auto ush = menu->addAction(i18n("Unstage Hunk"));
auto usl = menu->addAction(i18n("Unstage Lines"));
menu->addActions(m->actions());
v->setContextMenu(menu);
connect(ush, &QAction::triggered, v, [=] {
applyDiff(file, true, true, v);
});
connect(usl, &QAction::triggered, v, [=] {
applyDiff(file, true, false, v);
});
}
};
if (m_tempFiles.empty()) {
return;
}
auto v = m_tempFiles.back().second;
if (!v || !v->document()) {
return;
}
auto m = v->contextMenu();
if (!staged) {
QMenu *menu = new QMenu(v);
auto sh = menu->addAction(i18n("Stage Hunk"));
auto sl = menu->addAction(i18n("Stage Lines"));
menu->addActions(m->actions());
v->setContextMenu(menu);
connect(sh, &QAction::triggered, v, [=] {
applyDiff(file, false, true, v);
});
connect(sl, &QAction::triggered, v, [=] {
applyDiff(file, false, false, v);
});
} else {
QMenu *menu = new QMenu(v);
auto ush = menu->addAction(i18n("Unstage Hunk"));
auto usl = menu->addAction(i18n("Unstage Lines"));
menu->addActions(m->actions());
v->setContextMenu(menu);
connect(ush, &QAction::triggered, v, [=] {
applyDiff(file, true, true, v);
});
connect(usl, &QAction::triggered, v, [=] {
applyDiff(file, true, false, v);
});
}
m_pluginView->showDiffInFixedView(git->readAllStandardOutput(), addContextMenuActions);
}
git->deleteLater();
});
......@@ -593,11 +589,10 @@ void GitWidget::applyDiff(const QString &fileName, bool staged, bool hunk, KText
} else {
// close and reopen doc to show updated diff
if (v && v->document()) {
KTextEditor::Editor::instance()->application()->closeDocument(v->document());
showDiff(fileName, staged);
}
// must come at the end
QTimer::singleShot(100, this, [this] {
QTimer::singleShot(10, this, [this] {
getStatus();
});
}
......@@ -756,8 +751,8 @@ void GitWidget::createStashDialog(StashMode m, const QString &gitPath)
{
auto stashDialog = new StashDialog(this, mainWindow()->window(), gitPath);
connect(stashDialog, &StashDialog::message, this, &GitWidget::sendMessage);
connect(stashDialog, &StashDialog::openTempFile, this, [this](const QString &t, const QByteArray &r) {
openTempFile(QString(), t, r);
connect(stashDialog, &StashDialog::showStashDiff, this, [this](const QByteArray &r) {
m_pluginView->showDiffInFixedView(r);
});
connect(stashDialog, &StashDialog::done, this, [this, stashDialog] {
getStatus();
......@@ -823,51 +818,6 @@ QMenu *GitWidget::stashMenu()
return menu;
}
void GitWidget::clearTempFile(KTextEditor::Document *document)
{
m_tempFiles.erase(std::remove_if(m_tempFiles.begin(),
m_tempFiles.end(),
[document](const GitWidget::TempFileViewPair &tf) {
if (tf.second && tf.second->document() == document) {
return true;
}
return false;
}),
m_tempFiles.end());
}
void GitWidget::openTempFile(const QString &file, const QString &templatString, const QByteArray &contents)
{
if (contents.isEmpty()) {
return;
}
std::unique_ptr<QTemporaryFile> f(new QTemporaryFile);
if (!templatString.isEmpty() && !file.isEmpty()) {
f->setFileTemplate(templatString.arg(file));
} else if (!templatString.isEmpty() && file.isEmpty()) {
f->setFileTemplate(templatString);
}
if (!f->open()) {
sendMessage(i18n("Failed to open temporary file for diff: %1", f->errorString()), true);
return;
}
f->write(contents);
f->flush();
const QUrl tempFileUrl(QUrl::fromLocalFile(f->fileName()));
QPointer<KTextEditor::View> v = m_mainWin->openUrl(tempFileUrl);
if (!v || !v->document()) {
return;
}
TempFileViewPair tfvp = std::make_pair(std::move(f), v);
m_tempFiles.push_back(std::move(tfvp));
connect(v->document(), &KTextEditor::Document::aboutToClose, this, &GitWidget::clearTempFile);
}
static KMessageBox::ButtonCode confirm(GitWidget *_this, const QString &text)
{
return KMessageBox::questionYesNo(_this, text, {}, KStandardGuiItem::yes(), KStandardGuiItem::no(), {}, KMessageBox::Dangerous);
......
......@@ -47,10 +47,6 @@ public:
void getStatus(bool untracked = true, bool submodules = false);
KTextEditor::MainWindow *mainWindow();
using TempFileViewPair = std::pair<std::unique_ptr<QTemporaryFile>, QPointer<KTextEditor::View>>;
std::vector<TempFileViewPair> *tempFilesVector();
void openTempFile(const QString &file, const QString &templatString, const QByteArray &contents);
// will just proxy the message to the plugin view
void sendMessage(const QString &message, bool warn);
......@@ -69,7 +65,6 @@ private:
QString m_commitMessage;
KTextEditor::MainWindow *m_mainWin;
QMenu *m_gitMenu;
std::vector<TempFileViewPair> m_tempFiles;
KateProjectPluginView *m_pluginView;
using CancelHandle = QPointer<QProcess>;
......@@ -103,9 +98,6 @@ private:
void enableCancel(QProcess *git);
void hideCancel();
public Q_SLOTS:
void clearTempFile(KTextEditor::Document *document);
private Q_SLOTS:
void parseStatusReady();
void opencommitChangesDialog();
......
......@@ -41,7 +41,6 @@ KateProjectView::KateProjectView(KateProjectPluginView *pluginView, KateProject
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_branchBtn);
// layout->addWidget(m_treeView);
layout->addWidget(m_stackWidget);
layout->addWidget(m_filter);
setLayout(layout);
......@@ -146,7 +145,9 @@ void KateProjectView::showFileGitHistory(const QString &file)
// create on demand and on switch back delete
auto fhs = new FileHistoryWidget(file);
connect(fhs, &FileHistoryWidget::backClicked, this, &KateProjectView::setTreeViewAsCurrent);
connect(fhs, &FileHistoryWidget::commitClicked, this, &KateProjectView::showDiffInFixedView);
connect(fhs, &FileHistoryWidget::commitClicked, this, [this](const QByteArray &diff) {
m_pluginView->showDiffInFixedView(diff);
});
connect(fhs, &FileHistoryWidget::errorMessage, m_pluginView, [this](const QString &s, bool warn) {
QVariantMap genericMessage;
genericMessage.insert(QStringLiteral("type"), warn ? QStringLiteral("Error") : QStringLiteral("Info"));
......@@ -158,15 +159,3 @@ void KateProjectView::showFileGitHistory(const QString &file)
m_stackWidget->addWidget(fhs);
m_stackWidget->setCurrentWidget(fhs);
}
void KateProjectView::showDiffInFixedView(const QString & /*file*/, const QByteArray &contents)
{
if (!m_fixedView) {
m_fixedView = m_pluginView->mainWindow()->openUrl(QUrl());
}
if (m_fixedView->document()) {
m_fixedView->document()->setText(QString::fromUtf8(contents));
m_fixedView->document()->setHighlightingMode(QStringLiteral("Diff"));
}
}
......@@ -73,8 +73,6 @@ private Q_SLOTS:
void showFileGitHistory(const QString &file);
void showDiffInFixedView(const QString &file, const QByteArray &contents);
private:
/**
* our plugin view
......@@ -110,11 +108,6 @@ private:
* watches for changes to .git/HEAD
*/
QFileSystemWatcher m_branchChangedWatcher;
/**
* Fixed view for view diffs
*/
QPointer<KTextEditor::View> m_fixedView;
};
#endif
......@@ -349,7 +349,7 @@ void StashDialog::showStash(const QByteArray &index)
if (es != QProcess::NormalExit || exitCode != 0) {
Q_EMIT message(i18n("Show stash failed. Error: ") + QString::fromUtf8(git->readAll()), true);
} else {
Q_EMIT openTempFile(QStringLiteral("XXXXXX.diff"), git->readAllStandardOutput());
Q_EMIT showStashDiff(git->readAllStandardOutput());
}
Q_EMIT done();
git->deleteLater();
......
......@@ -50,7 +50,7 @@ public:
Q_SIGNAL void message(const QString &msg, bool warn);
Q_SIGNAL void done();
Q_SIGNAL void openTempFile(const QString &templatee, const QByteArray &gitOutput);
Q_SIGNAL void showStashDiff(const QByteArray &diff);
protected Q_SLOTS:
void slotReturnPressed() override;
......
Markdown is supported
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