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

View File

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

View File

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

View File

@@ -383,126 +383,87 @@ QStringList GitDiffEditorController::addHeadWhenCommandInProgress() const
class RepositoryDiffController : public GitDiffEditorController
{
Q_OBJECT
public:
explicit RepositoryDiffController(IDocument *document) :
GitDiffEditorController(document)
{ }
void reload() override;
};
void RepositoryDiffController::reload()
{
{
setReloader([this] {
QStringList args = {"diff"};
args.append(addHeadWhenCommandInProgress());
runCommand(QList<QStringList>() << addConfigurationArguments(args));
}
runCommand({addConfigurationArguments(args)});
});
}
};
class FileDiffController : public GitDiffEditorController
{
Q_OBJECT
public:
FileDiffController(IDocument *document, const QString &fileName) :
GitDiffEditorController(document),
m_fileName(fileName)
{ }
void reload() override;
private:
const QString m_fileName;
};
void FileDiffController::reload()
{
GitDiffEditorController(document)
{
setReloader([this, fileName] {
QStringList args = {"diff"};
args.append(addHeadWhenCommandInProgress());
args << "--" << m_fileName;
runCommand(QList<QStringList>() << addConfigurationArguments(args));
}
args << "--" << fileName;
runCommand({addConfigurationArguments(args)});
});
}
};
class FileListDiffController : public GitDiffEditorController
{
Q_OBJECT
public:
FileListDiffController(IDocument *document,
const QStringList &stagedFiles, const QStringList &unstagedFiles) :
GitDiffEditorController(document),
m_stagedFiles(stagedFiles),
m_unstagedFiles(unstagedFiles)
{ }
void reload() override;
private:
const QStringList m_stagedFiles;
const QStringList m_unstagedFiles;
};
void FileListDiffController::reload()
{
GitDiffEditorController(document)
{
setReloader([this, stagedFiles, unstagedFiles] {
QList<QStringList> argLists;
if (!m_stagedFiles.isEmpty()) {
if (!stagedFiles.isEmpty()) {
QStringList stagedArgs = {"diff", "--cached", "--"};
stagedArgs << m_stagedFiles;
stagedArgs << stagedFiles;
argLists << addConfigurationArguments(stagedArgs);
}
if (!m_unstagedFiles.isEmpty()) {
if (!unstagedFiles.isEmpty()) {
QStringList unstagedArgs = {"diff"};
unstagedArgs << addHeadWhenCommandInProgress() << "--" << m_unstagedFiles;
unstagedArgs << addHeadWhenCommandInProgress() << "--" << unstagedFiles;
argLists << addConfigurationArguments(unstagedArgs);
}
if (!argLists.isEmpty())
runCommand(argLists);
}
});
}
};
class ProjectDiffController : public GitDiffEditorController
{
Q_OBJECT
public:
ProjectDiffController(IDocument *document, const QStringList &projectPaths) :
GitDiffEditorController(document),
m_projectPaths(projectPaths)
{ }
void reload() override;
private:
const QStringList m_projectPaths;
};
void ProjectDiffController::reload()
{
GitDiffEditorController(document)
{
setReloader([this, projectPaths] {
QStringList args = {"diff"};
args << addHeadWhenCommandInProgress() << "--" << m_projectPaths;
runCommand(QList<QStringList>() << addConfigurationArguments(args));
}
args << addHeadWhenCommandInProgress() << "--" << projectPaths;
runCommand({addConfigurationArguments(args)});
});
}
};
class BranchDiffController : public GitDiffEditorController
{
Q_OBJECT
public:
BranchDiffController(IDocument *document, const QString &branch) :
GitDiffEditorController(document),
m_branch(branch)
{ }
void reload() override;
private:
const QString m_branch;
};
void BranchDiffController::reload()
{
GitDiffEditorController(document)
{
setReloader([this, branch] {
QStringList args = {"diff"};
args << addHeadWhenCommandInProgress() << m_branch;
runCommand(QList<QStringList>() << addConfigurationArguments(args));
}
args << addHeadWhenCommandInProgress() << branch;
runCommand({addConfigurationArguments(args)});
});
}
};
class ShowController : public GitDiffEditorController
{
@@ -514,9 +475,14 @@ public:
m_state(Idle)
{
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;
private:
@@ -525,15 +491,6 @@ private:
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)
{
QTC_ASSERT(m_state != Idle, return);

View File

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

View File

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