Diff/Vcs: Use a function object for reloading

Helps with slimming down the user code side.

Change-Id: I4b0aac76c0d1516eb05bff9c18594e64f8b41a7a
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
hjk
2020-02-06 11:52:59 +01:00
parent 0737291d54
commit b22768e980
6 changed files with 94 additions and 156 deletions

View File

@@ -123,6 +123,11 @@ void DiffEditorController::forceContextLineCount(int lines)
m_document->forceContextLineCount(lines); m_document->forceContextLineCount(lines);
} }
void DiffEditorController::setReloader(const std::function<void ()> &reloader)
{
m_reloader = reloader;
}
Core::IDocument *DiffEditorController::document() const Core::IDocument *DiffEditorController::document() const
{ {
return m_document; return m_document;
@@ -135,7 +140,8 @@ void DiffEditorController::requestReload()
{ {
m_isReloading = true; m_isReloading = true;
m_document->beginReload(); m_document->beginReload();
reload(); QTC_ASSERT(m_reloader, reloadFinished(false); return);
m_reloader();
} }
void DiffEditorController::reloadFinished(bool success) void DiffEditorController::reloadFinished(bool success)

View File

@@ -72,15 +72,15 @@ public:
bool chunkExists(int fileIndex, int chunkIndex) const; bool chunkExists(int fileIndex, int chunkIndex) const;
Core::IDocument *document() const; Core::IDocument *document() const;
// reloadFinished() should be called inside the reloader (for synchronous reload)
// or later (for asynchronous reload)
void setReloader(const std::function<void ()> &reloader);
signals: signals:
void chunkActionsRequested(QMenu *menu, int fileIndex, int chunkIndex, void chunkActionsRequested(QMenu *menu, int fileIndex, int chunkIndex,
const ChunkSelection &selection); const ChunkSelection &selection);
protected: protected:
// reloadFinished() should be called
// inside reload() (for synchronous reload)
// or later (for asynchronous reload)
virtual void reload() = 0;
void reloadFinished(bool success); void reloadFinished(bool success);
void setDiffFiles(const QList<FileData> &diffFileList, void setDiffFiles(const QList<FileData> &diffFileList,
@@ -93,6 +93,7 @@ protected:
private: private:
Internal::DiffEditorDocument *const m_document; Internal::DiffEditorDocument *const m_document;
bool m_isReloading = false; bool m_isReloading = false;
std::function<void()> m_reloader;
friend class Internal::DiffEditorDocument; friend class Internal::DiffEditorDocument;
}; };

View File

@@ -130,11 +130,11 @@ class DiffFilesController : public DiffEditorController
Q_OBJECT Q_OBJECT
public: public:
DiffFilesController(IDocument *document); DiffFilesController(IDocument *document);
~DiffFilesController() override; ~DiffFilesController() override { cancelReload(); }
protected: protected:
void reload() final;
virtual QList<ReloadInput> reloadInputList() const = 0; virtual QList<ReloadInput> reloadInputList() const = 0;
private: private:
void reloaded(); void reloaded();
void cancelReload(); void cancelReload();
@@ -147,22 +147,16 @@ DiffFilesController::DiffFilesController(IDocument *document)
{ {
connect(&m_futureWatcher, &QFutureWatcher<FileData>::finished, connect(&m_futureWatcher, &QFutureWatcher<FileData>::finished,
this, &DiffFilesController::reloaded); this, &DiffFilesController::reloaded);
}
DiffFilesController::~DiffFilesController() setReloader([this] {
{ cancelReload();
cancelReload(); m_futureWatcher.setFuture(Utils::map(reloadInputList(),
} DiffFile(ignoreWhitespace(),
contextLineCount())));
void DiffFilesController::reload() Core::ProgressManager::addTask(m_futureWatcher.future(),
{ tr("Calculating diff"), "DiffEditor");
cancelReload(); });
m_futureWatcher.setFuture(Utils::map(reloadInputList(),
DiffFile(ignoreWhitespace(),
contextLineCount())));
Core::ProgressManager::addTask(m_futureWatcher.future(),
tr("Calculating diff"), "DiffEditor");
} }
void DiffFilesController::reloaded() void DiffFilesController::reloaded()

View File

@@ -383,127 +383,88 @@ QStringList GitDiffEditorController::addHeadWhenCommandInProgress() const
class RepositoryDiffController : public GitDiffEditorController class RepositoryDiffController : public GitDiffEditorController
{ {
Q_OBJECT
public: public:
explicit RepositoryDiffController(IDocument *document) : explicit RepositoryDiffController(IDocument *document) :
GitDiffEditorController(document) GitDiffEditorController(document)
{ } {
setReloader([this] {
void reload() override; QStringList args = {"diff"};
args.append(addHeadWhenCommandInProgress());
runCommand({addConfigurationArguments(args)});
});
}
}; };
void RepositoryDiffController::reload()
{
QStringList args = {"diff"};
args.append(addHeadWhenCommandInProgress());
runCommand(QList<QStringList>() << addConfigurationArguments(args));
}
class FileDiffController : public GitDiffEditorController class FileDiffController : public GitDiffEditorController
{ {
Q_OBJECT
public: public:
FileDiffController(IDocument *document, const QString &fileName) : FileDiffController(IDocument *document, const QString &fileName) :
GitDiffEditorController(document), GitDiffEditorController(document)
m_fileName(fileName) {
{ } setReloader([this, fileName] {
QStringList args = {"diff"};
void reload() override; args.append(addHeadWhenCommandInProgress());
args << "--" << fileName;
private: runCommand({addConfigurationArguments(args)});
const QString m_fileName; });
}
}; };
void FileDiffController::reload()
{
QStringList args = {"diff"};
args.append(addHeadWhenCommandInProgress());
args << "--" << m_fileName;
runCommand(QList<QStringList>() << addConfigurationArguments(args));
}
class FileListDiffController : public GitDiffEditorController class FileListDiffController : public GitDiffEditorController
{ {
Q_OBJECT
public: public:
FileListDiffController(IDocument *document, FileListDiffController(IDocument *document,
const QStringList &stagedFiles, const QStringList &unstagedFiles) : const QStringList &stagedFiles, const QStringList &unstagedFiles) :
GitDiffEditorController(document), GitDiffEditorController(document)
m_stagedFiles(stagedFiles), {
m_unstagedFiles(unstagedFiles) setReloader([this, stagedFiles, unstagedFiles] {
{ } QList<QStringList> argLists;
if (!stagedFiles.isEmpty()) {
QStringList stagedArgs = {"diff", "--cached", "--"};
stagedArgs << stagedFiles;
argLists << addConfigurationArguments(stagedArgs);
}
void reload() override; if (!unstagedFiles.isEmpty()) {
QStringList unstagedArgs = {"diff"};
unstagedArgs << addHeadWhenCommandInProgress() << "--" << unstagedFiles;
argLists << addConfigurationArguments(unstagedArgs);
}
private: if (!argLists.isEmpty())
const QStringList m_stagedFiles; runCommand(argLists);
const QStringList m_unstagedFiles; });
}
}; };
void FileListDiffController::reload()
{
QList<QStringList> argLists;
if (!m_stagedFiles.isEmpty()) {
QStringList stagedArgs = {"diff", "--cached", "--"};
stagedArgs << m_stagedFiles;
argLists << addConfigurationArguments(stagedArgs);
}
if (!m_unstagedFiles.isEmpty()) {
QStringList unstagedArgs = {"diff"};
unstagedArgs << addHeadWhenCommandInProgress() << "--" << m_unstagedFiles;
argLists << addConfigurationArguments(unstagedArgs);
}
if (!argLists.isEmpty())
runCommand(argLists);
}
class ProjectDiffController : public GitDiffEditorController class ProjectDiffController : public GitDiffEditorController
{ {
Q_OBJECT
public: public:
ProjectDiffController(IDocument *document, const QStringList &projectPaths) : ProjectDiffController(IDocument *document, const QStringList &projectPaths) :
GitDiffEditorController(document), GitDiffEditorController(document)
m_projectPaths(projectPaths) {
{ } setReloader([this, projectPaths] {
QStringList args = {"diff"};
void reload() override; args << addHeadWhenCommandInProgress() << "--" << projectPaths;
runCommand({addConfigurationArguments(args)});
private: });
const QStringList m_projectPaths; }
}; };
void ProjectDiffController::reload()
{
QStringList args = {"diff"};
args << addHeadWhenCommandInProgress() << "--" << m_projectPaths;
runCommand(QList<QStringList>() << addConfigurationArguments(args));
}
class BranchDiffController : public GitDiffEditorController class BranchDiffController : public GitDiffEditorController
{ {
Q_OBJECT
public: public:
BranchDiffController(IDocument *document, const QString &branch) : BranchDiffController(IDocument *document, const QString &branch) :
GitDiffEditorController(document), GitDiffEditorController(document)
m_branch(branch) {
{ } setReloader([this, branch] {
QStringList args = {"diff"};
void reload() override; args << addHeadWhenCommandInProgress() << branch;
runCommand({addConfigurationArguments(args)});
private: });
const QString m_branch; }
}; };
void BranchDiffController::reload()
{
QStringList args = {"diff"};
args << addHeadWhenCommandInProgress() << m_branch;
runCommand(QList<QStringList>() << addConfigurationArguments(args));
}
class ShowController : public GitDiffEditorController class ShowController : public GitDiffEditorController
{ {
Q_OBJECT Q_OBJECT
@@ -514,9 +475,14 @@ public:
m_state(Idle) m_state(Idle)
{ {
setDisplayName("Git Show"); setDisplayName("Git Show");
setReloader([this] {
m_state = GettingDescription;
const QStringList args = {"show", "-s", noColorOption, showFormatC, m_id};
runCommand({args}, GitPluginPrivate::client()->encoding(workingDirectory(), "i18n.commitEncoding"));
setStartupFile(VcsBase::source(this->document()));
});
} }
void reload() override;
void processCommandOutput(const QString &output) override; void processCommandOutput(const QString &output) override;
private: private:
@@ -525,15 +491,6 @@ private:
State m_state; State m_state;
}; };
void ShowController::reload()
{
// stage 1
m_state = GettingDescription;
const QStringList args = {"show", "-s", noColorOption, showFormatC, m_id};
runCommand(QList<QStringList>() << args, GitPluginPrivate::client()->encoding(workingDirectory(), "i18n.commitEncoding"));
setStartupFile(VcsBase::source(document()));
}
void ShowController::processCommandOutput(const QString &output) void ShowController::processCommandOutput(const QString &output)
{ {
QTC_ASSERT(m_state != Idle, return); QTC_ASSERT(m_state != Idle, return);

View File

@@ -89,37 +89,27 @@ class FileDiffController : public MercurialDiffEditorController
{ {
public: public:
FileDiffController(IDocument *document, const QString &fileName) : FileDiffController(IDocument *document, const QString &fileName) :
MercurialDiffEditorController(document), MercurialDiffEditorController(document)
m_fileName(fileName)
{ }
void reload() override
{ {
QStringList args = { "diff", m_fileName }; setReloader([this, fileName] {
runCommand({ addConfigurationArguments(args) }); QStringList args = { "diff", fileName };
runCommand({ addConfigurationArguments(args) });
});
} }
private:
const QString m_fileName;
}; };
class FileListDiffController : public MercurialDiffEditorController class FileListDiffController : public MercurialDiffEditorController
{ {
public: public:
FileListDiffController(IDocument *document, const QStringList &fileNames) : FileListDiffController(IDocument *document, const QStringList &fileNames) :
MercurialDiffEditorController(document), MercurialDiffEditorController(document)
m_fileNames(fileNames)
{ }
void reload() override
{ {
QStringList args { "diff" }; setReloader([this, fileNames] {
args << m_fileNames; QStringList args { "diff" };
runCommand({addConfigurationArguments(args)}); args << fileNames;
runCommand({addConfigurationArguments(args)});
});
} }
private:
const QStringList m_fileNames;
}; };
@@ -128,12 +118,11 @@ class RepositoryDiffController : public MercurialDiffEditorController
public: public:
RepositoryDiffController(IDocument *document) : RepositoryDiffController(IDocument *document) :
MercurialDiffEditorController(document) MercurialDiffEditorController(document)
{ }
void reload() override
{ {
QStringList args = { "diff" }; setReloader([this] {
runCommand({addConfigurationArguments(args)}); QStringList args = { "diff" };
runCommand({addConfigurationArguments(args)});
});
} }
}; };

View File

@@ -175,13 +175,13 @@ public:
: VcsBaseDiffEditorController(document), m_authenticationOptions(authOptions) : VcsBaseDiffEditorController(document), m_authenticationOptions(authOptions)
{ {
forceContextLineCount(3); // SVN cannot change that when using internal diff forceContextLineCount(3); // SVN cannot change that when using internal diff
setReloader([this] { m_changeNumber ? requestDescription() : requestDiff(); });
} }
void setFilesList(const QStringList &filesList); void setFilesList(const QStringList &filesList);
void setChangeNumber(int changeNumber); void setChangeNumber(int changeNumber);
protected: protected:
void reload() override;
void processCommandOutput(const QString &output) override; void processCommandOutput(const QString &output) override;
private: private:
@@ -242,15 +242,6 @@ void SubversionDiffEditorController::requestDiff()
runCommand(QList<QStringList>() << args, 0); runCommand(QList<QStringList>() << args, 0);
} }
void SubversionDiffEditorController::reload()
{
if (m_changeNumber) {
requestDescription();
} else {
requestDiff();
}
}
void SubversionDiffEditorController::processCommandOutput(const QString &output) void SubversionDiffEditorController::processCommandOutput(const QString &output)
{ {
QTC_ASSERT(m_state != Idle, return); QTC_ASSERT(m_state != Idle, return);