Commit 6509753e authored by Jaroslaw Kobus's avatar Jaroslaw Kobus

Make it possible to cancel async diff calculation

Change-Id: I38fe86159daab794d060860de6ee5ab30fb395eb
Reviewed-by: Tobias Hunger's avatarTobias Hunger <tobias.hunger@qt.io>
parent 4bea049c
...@@ -25,10 +25,13 @@ ...@@ -25,10 +25,13 @@
#include "diffutils.h" #include "diffutils.h"
#include "differ.h" #include "differ.h"
#include "texteditor/fontsettings.h"
#include <QFutureInterfaceBase>
#include <QRegularExpression> #include <QRegularExpression>
#include <QStringList> #include <QStringList>
#include <QTextStream> #include <QTextStream>
#include "texteditor/fontsettings.h"
namespace DiffEditor { namespace DiffEditor {
...@@ -808,7 +811,8 @@ static FileData readDiffHeaderAndChunks(QStringRef headerAndChunks, ...@@ -808,7 +811,8 @@ static FileData readDiffHeaderAndChunks(QStringRef headerAndChunks,
} }
static QList<FileData> readDiffPatch(QStringRef patch, static QList<FileData> readDiffPatch(QStringRef patch,
bool *ok) bool *ok,
QFutureInterfaceBase *jobController)
{ {
const QRegularExpression diffRegExp("(?:\\n|^)" // new line of the beginning of a patch const QRegularExpression diffRegExp("(?:\\n|^)" // new line of the beginning of a patch
"(" // either "(" // either
...@@ -835,6 +839,9 @@ static QList<FileData> readDiffPatch(QStringRef patch, ...@@ -835,6 +839,9 @@ static QList<FileData> readDiffPatch(QStringRef patch,
readOk = true; readOk = true;
int lastPos = -1; int lastPos = -1;
do { do {
if (jobController && jobController->isCanceled())
return QList<FileData>();
int pos = diffMatch.capturedStart(); int pos = diffMatch.capturedStart();
if (lastPos >= 0) { if (lastPos >= 0) {
QStringRef headerAndChunks = patch.mid(lastPos, QStringRef headerAndChunks = patch.mid(lastPos,
...@@ -1031,7 +1038,8 @@ static FileData readCopyRenameChunks(QStringRef copyRenameChunks, ...@@ -1031,7 +1038,8 @@ static FileData readCopyRenameChunks(QStringRef copyRenameChunks,
return fileData; return fileData;
} }
static QList<FileData> readGitPatch(QStringRef patch, bool *ok) static QList<FileData> readGitPatch(QStringRef patch, bool *ok,
QFutureInterfaceBase *jobController)
{ {
const QRegularExpression simpleGitRegExp( const QRegularExpression simpleGitRegExp(
...@@ -1127,6 +1135,9 @@ static QList<FileData> readGitPatch(QStringRef patch, bool *ok) ...@@ -1127,6 +1135,9 @@ static QList<FileData> readGitPatch(QStringRef patch, bool *ok)
}; };
do { do {
if (jobController && jobController->isCanceled())
return QList<FileData>();
collectFileData(); collectFileData();
if (!readOk) if (!readOk)
break; break;
...@@ -1168,12 +1179,17 @@ static QList<FileData> readGitPatch(QStringRef patch, bool *ok) ...@@ -1168,12 +1179,17 @@ static QList<FileData> readGitPatch(QStringRef patch, bool *ok)
return fileDataList; return fileDataList;
} }
QList<FileData> DiffUtils::readPatch(const QString &patch, bool *ok) QList<FileData> DiffUtils::readPatch(const QString &patch, bool *ok,
QFutureInterfaceBase *jobController)
{ {
bool readOk = false; bool readOk = false;
QList<FileData> fileDataList; QList<FileData> fileDataList;
if (jobController) {
jobController->setProgressRange(0, 1);
jobController->setProgressValue(0);
}
QStringRef croppedPatch(&patch); QStringRef croppedPatch(&patch);
// Crop e.g. "-- \n2.10.2.windows.1\n\n" at end of file // Crop e.g. "-- \n2.10.2.windows.1\n\n" at end of file
const QRegularExpression formatPatchEndingRegExp("(\\n-- \\n\\S*\\n\\n$)"); const QRegularExpression formatPatchEndingRegExp("(\\n-- \\n\\S*\\n\\n$)");
...@@ -1181,9 +1197,9 @@ QList<FileData> DiffUtils::readPatch(const QString &patch, bool *ok) ...@@ -1181,9 +1197,9 @@ QList<FileData> DiffUtils::readPatch(const QString &patch, bool *ok)
if (match.hasMatch()) if (match.hasMatch())
croppedPatch = croppedPatch.left(match.capturedStart() + 1); croppedPatch = croppedPatch.left(match.capturedStart() + 1);
fileDataList = readGitPatch(croppedPatch, &readOk); fileDataList = readGitPatch(croppedPatch, &readOk, jobController);
if (!readOk) if (!readOk)
fileDataList = readDiffPatch(croppedPatch, &readOk); fileDataList = readDiffPatch(croppedPatch, &readOk, jobController);
if (ok) if (ok)
*ok = readOk; *ok = readOk;
......
...@@ -30,6 +30,10 @@ ...@@ -30,6 +30,10 @@
#include <QString> #include <QString>
#include <QMap> #include <QMap>
QT_BEGIN_NAMESPACE
class QFutureInterfaceBase;
QT_END_NAMESPACE
namespace TextEditor { class FontSettings; } namespace TextEditor { class FontSettings; }
namespace DiffEditor { namespace DiffEditor {
...@@ -150,7 +154,8 @@ public: ...@@ -150,7 +154,8 @@ public:
static QString makePatch(const QList<FileData> &fileDataList, static QString makePatch(const QList<FileData> &fileDataList,
unsigned formatFlags = 0); unsigned formatFlags = 0);
static QList<FileData> readPatch(const QString &patch, static QList<FileData> readPatch(const QString &patch,
bool *ok = 0); bool *ok = 0,
QFutureInterfaceBase *jobController = nullptr);
}; };
} // namespace DiffEditor } // namespace DiffEditor
...@@ -44,7 +44,7 @@ static void readPatch(QFutureInterface<QList<FileData>> &futureInterface, ...@@ -44,7 +44,7 @@ static void readPatch(QFutureInterface<QList<FileData>> &futureInterface,
const QString &patch) const QString &patch)
{ {
bool ok; bool ok;
const QList<FileData> &fileDataList = DiffUtils::readPatch(patch, &ok); const QList<FileData> &fileDataList = DiffUtils::readPatch(patch, &ok, &futureInterface);
futureInterface.reportResult(fileDataList); futureInterface.reportResult(fileDataList);
} }
...@@ -88,10 +88,12 @@ VcsBaseDiffEditorControllerPrivate::~VcsBaseDiffEditorControllerPrivate() ...@@ -88,10 +88,12 @@ VcsBaseDiffEditorControllerPrivate::~VcsBaseDiffEditorControllerPrivate()
void VcsBaseDiffEditorControllerPrivate::processingFinished() void VcsBaseDiffEditorControllerPrivate::processingFinished()
{ {
const QList<FileData> fileDataList = m_processWatcher.future().result(); bool success = !m_processWatcher.future().isCanceled();
const QList<FileData> fileDataList = success
? m_processWatcher.future().result() : QList<FileData>();
q->setDiffFiles(fileDataList, q->workingDirectory(), q->startupFile()); q->setDiffFiles(fileDataList, q->workingDirectory(), q->startupFile());
q->reloadFinished(true); q->reloadFinished(success);
} }
void VcsBaseDiffEditorControllerPrivate::processDiff(const QString &patch) void VcsBaseDiffEditorControllerPrivate::processDiff(const QString &patch)
......
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