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,15 +147,8 @@ 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();
}
void DiffFilesController::reload()
{
cancelReload(); cancelReload();
m_futureWatcher.setFuture(Utils::map(reloadInputList(), m_futureWatcher.setFuture(Utils::map(reloadInputList(),
DiffFile(ignoreWhitespace(), DiffFile(ignoreWhitespace(),
@@ -163,6 +156,7 @@ void DiffFilesController::reload()
Core::ProgressManager::addTask(m_futureWatcher.future(), Core::ProgressManager::addTask(m_futureWatcher.future(),
tr("Calculating diff"), "DiffEditor"); tr("Calculating diff"), "DiffEditor");
});
} }
void DiffFilesController::reloaded() void DiffFilesController::reloaded()

View File

@@ -383,126 +383,87 @@ 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;
};
void RepositoryDiffController::reload()
{
QStringList args = {"diff"}; QStringList args = {"diff"};
args.append(addHeadWhenCommandInProgress()); args.append(addHeadWhenCommandInProgress());
runCommand(QList<QStringList>() << addConfigurationArguments(args)); runCommand({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] {
void reload() override;
private:
const QString m_fileName;
};
void FileDiffController::reload()
{
QStringList args = {"diff"}; QStringList args = {"diff"};
args.append(addHeadWhenCommandInProgress()); args.append(addHeadWhenCommandInProgress());
args << "--" << m_fileName; args << "--" << fileName;
runCommand({addConfigurationArguments(args)});
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] {
{ }
void reload() override;
private:
const QStringList m_stagedFiles;
const QStringList m_unstagedFiles;
};
void FileListDiffController::reload()
{
QList<QStringList> argLists; QList<QStringList> argLists;
if (!m_stagedFiles.isEmpty()) { if (!stagedFiles.isEmpty()) {
QStringList stagedArgs = {"diff", "--cached", "--"}; QStringList stagedArgs = {"diff", "--cached", "--"};
stagedArgs << m_stagedFiles; stagedArgs << stagedFiles;
argLists << addConfigurationArguments(stagedArgs); argLists << addConfigurationArguments(stagedArgs);
} }
if (!m_unstagedFiles.isEmpty()) { if (!unstagedFiles.isEmpty()) {
QStringList unstagedArgs = {"diff"}; QStringList unstagedArgs = {"diff"};
unstagedArgs << addHeadWhenCommandInProgress() << "--" << m_unstagedFiles; unstagedArgs << addHeadWhenCommandInProgress() << "--" << unstagedFiles;
argLists << addConfigurationArguments(unstagedArgs); argLists << addConfigurationArguments(unstagedArgs);
} }
if (!argLists.isEmpty()) if (!argLists.isEmpty())
runCommand(argLists); 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] {
void reload() override;
private:
const QStringList m_projectPaths;
};
void ProjectDiffController::reload()
{
QStringList args = {"diff"}; QStringList args = {"diff"};
args << addHeadWhenCommandInProgress() << "--" << m_projectPaths; args << addHeadWhenCommandInProgress() << "--" << projectPaths;
runCommand(QList<QStringList>() << addConfigurationArguments(args)); runCommand({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] {
void reload() override;
private:
const QString m_branch;
};
void BranchDiffController::reload()
{
QStringList args = {"diff"}; QStringList args = {"diff"};
args << addHeadWhenCommandInProgress() << m_branch; args << addHeadWhenCommandInProgress() << branch;
runCommand(QList<QStringList>() << addConfigurationArguments(args)); runCommand({addConfigurationArguments(args)});
} });
}
};
class ShowController : public GitDiffEditorController class ShowController : public GitDiffEditorController
{ {
@@ -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] {
QStringList args = { "diff", fileName };
runCommand({ addConfigurationArguments(args) }); 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
{ {
setReloader([this, fileNames] {
QStringList args { "diff" }; QStringList args { "diff" };
args << m_fileNames; args << fileNames;
runCommand({addConfigurationArguments(args)}); 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
{ {
setReloader([this] {
QStringList args = { "diff" }; QStringList args = { "diff" };
runCommand({addConfigurationArguments(args)}); 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);