Skip to content

Commit

Permalink
Allow user scripts to operate on selected files
Browse files Browse the repository at this point in the history
  • Loading branch information
SlugFiller committed Oct 12, 2023
1 parent 8ec3ae8 commit e4ce8ac
Show file tree
Hide file tree
Showing 22 changed files with 549 additions and 84 deletions.
6 changes: 3 additions & 3 deletions GitUI/CommandsDialogs/FormBrowse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ internal FormBrowse(GitUICommands commands, BrowseArguments args, ISettingsSourc
_aheadBehindDataProvider = new AheadBehindDataProvider(() => Module.GitExecutable);
toolStripButtonPush.Initialize(_aheadBehindDataProvider);
repoObjectsTree.Initialize(_aheadBehindDataProvider, filterRevisionGridBySpaceSeparatedRefs: ToolStripFilters.SetBranchFilter, refsSource: RevisionGrid, revisionGridInfo: RevisionGrid, scriptRunner: RevisionGrid);
revisionDiff.Bind(revisionGridInfo: RevisionGrid, revisionGridUpdate: RevisionGrid, revisionFileTree: fileTree, () => RevisionGrid.CurrentFilter.PathFilter, RefreshGitStatusMonitor);
revisionDiff.Bind(revisionGridInfo: RevisionGrid, revisionGridUpdate: RevisionGrid, revisionFileTree: fileTree, () => RevisionGrid.CurrentFilter.PathFilter, RefreshGitStatusMonitor, scriptRunner: RevisionGrid);

// Show blame by default if not started from command line
fileTree.Bind(revisionGridInfo: RevisionGrid, revisionGridUpdate: RevisionGrid, RefreshGitStatusMonitor, _isFileBlameHistory);
fileTree.Bind(revisionGridInfo: RevisionGrid, revisionGridUpdate: RevisionGrid, RefreshGitStatusMonitor, _isFileBlameHistory, scriptRunner: RevisionGrid);
RevisionGrid.ResumeRefreshRevisions();

// Application is init, the repo related operations are triggered in OnLoad()
Expand Down Expand Up @@ -1014,7 +1014,7 @@ void LoadUserMenu()

button.Click += delegate
{
if (ScriptsRunner.RunScript(script.HotkeyCommandIdentifier, this, RevisionGrid).NeedsGridRefresh)
if (ScriptsRunner.RunScript(script.HotkeyCommandIdentifier, this, RevisionGrid, null).NeedsGridRefresh)
{
RefreshRevisions();
}
Expand Down
40 changes: 38 additions & 2 deletions GitUI/CommandsDialogs/FormCommit.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 53 additions & 1 deletion GitUI/CommandsDialogs/FormCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace GitUI.CommandsDialogs
{
public sealed partial class FormCommit : GitModuleForm
public sealed partial class FormCommit : GitModuleForm, IRunScript.IFileListSource
{
#region Translation

Expand Down Expand Up @@ -183,6 +183,8 @@ public sealed partial class FormCommit : GitModuleForm
private IReadOnlyList<GitItemStatus>? _currentSelection;
private int _alreadyLoadedTemplatesCount = -1;
private EventHandler? _branchNameLabelOnClick;
private IRunScript.IFileListSource _unstagedFileListSource;
private IRunScript.IFileListSource _stagedFileListSource;

private CommitKind CommitKind
{
Expand Down Expand Up @@ -211,6 +213,8 @@ public FormCommit(GitUICommands commands, CommitKind commitKind = CommitKind.Nor
ThreadHelper.ThrowIfNotOnUIThread();

_editedCommit = editedCommit;
_unstagedFileListSource = new RunScriptFileListSource(this, false);
_stagedFileListSource = new RunScriptFileListSource(this, true);

InitializeComponent();

Expand Down Expand Up @@ -1651,6 +1655,8 @@ private void UnstagedFileContext_Opening(object sender, System.ComponentModel.Ca
openWithToolStripMenuItem.Enabled = !isAnyDeleted;
deleteFileToolStripMenuItem.Enabled = !isAnyDeleted;
openContainingFolderToolStripMenuItem.Enabled = !isAnyDeleted;

UnstagedFileContext.AddUserScripts(runScriptToolStripMenuItem, ExecuteScriptUnstaged, (script) => script.OnEvent == ScriptEvent.ShowInFileList, UICommands);
}

private void StagedFileContext_Opening(object sender, System.ComponentModel.CancelEventArgs e)
Expand All @@ -1670,6 +1676,8 @@ private void StagedFileContext_Opening(object sender, System.ComponentModel.Canc
stagedOpenToolStripMenuItem7.Enabled = !isAnyDeleted;
stagedOpenWithToolStripMenuItem8.Enabled = !isAnyDeleted;
stagedOpenFolderToolStripMenuItem10.Enabled = !isAnyDeleted;

StagedFileContext.AddUserScripts(stagedRunScriptToolStripMenuItem, ExecuteScriptStaged, (script) => script.OnEvent == ScriptEvent.ShowInFileList, UICommands);
}

private void UnstagedSubmoduleContext_Opening(object sender, System.ComponentModel.CancelEventArgs e)
Expand Down Expand Up @@ -3308,6 +3316,50 @@ private void UpdateButtonStates()
: TranslatedStrings.ButtonPush;
}

private class RunScriptFileListSource : IRunScript.IFileListSource
{
private FormCommit _formCommit;
private bool _staged;

public RunScriptFileListSource(FormCommit formCommit, bool staged)
{
_formCommit = formCommit;
_staged = staged;
}

IEnumerable<string>? IRunScript.IFileListSource.GetFiles() => (_staged ? _formCommit.Staged : _formCommit.Unstaged).SelectedItems.Select(item => _formCommit._fullPathResolver.Resolve(item.Item.Name));

int? IRunScript.IFileListSource.GetLineNumber()
{
return _formCommit.SelectedDiff.CurrentFileLine;
}
}

private void ExecuteScriptUnstaged(int scriptId)
{
var scriptsRunner = UICommands.GetRequiredService<IScriptsRunner>();
if (scriptsRunner.RunScript(scriptId, FindForm() as GitModuleForm, null, _unstagedFileListSource).NeedsGridRefresh)
{
RescanChanges();
}
}

private void ExecuteScriptStaged(int scriptId)
{
var scriptsRunner = UICommands.GetRequiredService<IScriptsRunner>();
if (scriptsRunner.RunScript(scriptId, FindForm() as GitModuleForm, null, _stagedFileListSource).NeedsGridRefresh)
{
RescanChanges();
}
}

IEnumerable<string>? IRunScript.IFileListSource.GetFiles() => _currentFilesList?.SelectedItems?.Select(item => _fullPathResolver.Resolve(item.Item.Name));

int? IRunScript.IFileListSource.GetLineNumber()
{
return SelectedDiff.CurrentFileLine;
}

internal TestAccessor GetTestAccessor()
=> new(this);

Expand Down
20 changes: 19 additions & 1 deletion GitUI/CommandsDialogs/RevisionDiffControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions GitUI/CommandsDialogs/RevisionDiffControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace GitUI.CommandsDialogs
{
public partial class RevisionDiffControl : GitModuleControl
public partial class RevisionDiffControl : GitModuleControl, IRunScript.IFileListSource
{
private readonly TranslationString _saveFileFilterCurrentFormat = new("Current format");
private readonly TranslationString _saveFileFilterAllFiles = new("All files");
Expand Down Expand Up @@ -50,6 +50,7 @@ public partial class RevisionDiffControl : GitModuleControl
private string? _fallbackFollowedFile;
private FileStatusItem? _lastExplicitlySelectedItem;
private bool _isImplicitListSelection = false;
private IRunScript _scriptRunner;

public RevisionDiffControl()
{
Expand Down Expand Up @@ -306,13 +307,14 @@ private async Task SetDiffsAsync(IReadOnlyList<GitRevision> revisions)
}
}

public void Bind(IRevisionGridInfo revisionGridInfo, IRevisionGridUpdate revisionGridUpdate, RevisionFileTreeControl revisionFileTree, Func<string>? pathFilter, Action? refreshGitStatus)
public void Bind(IRevisionGridInfo revisionGridInfo, IRevisionGridUpdate revisionGridUpdate, RevisionFileTreeControl revisionFileTree, Func<string>? pathFilter, Action? refreshGitStatus, IRunScript scriptRunner)
{
_revisionGridInfo = revisionGridInfo;
_revisionGridUpdate = revisionGridUpdate;
_revisionFileTree = revisionFileTree;
_pathFilter = pathFilter;
_refreshGitStatus = refreshGitStatus;
_scriptRunner = scriptRunner;
DiffFiles.Bind(objectId => DescribeRevision(objectId), _revisionGridInfo.GetActualRevision);
}

Expand Down Expand Up @@ -754,6 +756,8 @@ private void UpdateStatusOfMenuItems()
{
blameToolStripMenuItem.Checked = false;
}

DiffContextMenu.AddUserScripts(runScriptToolStripMenuItem, Execute, (script) => script.OnEvent == ScriptEvent.ShowInFileList, UICommands);
}

private void DiffContextMenu_Opening(object sender, CancelEventArgs e)
Expand Down Expand Up @@ -1459,6 +1463,18 @@ private void ResetSelectedItemsWithConfirmation(bool resetToParent)

private static bool RenamedIndexItem(FileStatusItem item) => item.Item.IsRenamed && item.Item.Staged == StagedStatus.Index;

IEnumerable<string>? IRunScript.IFileListSource.GetFiles() => DiffFiles.SelectedItems.Select(item => _fullPathResolver.Resolve(item.Item.Name));

int? IRunScript.IFileListSource.GetLineNumber()
{
return BlameControl.Visible ? BlameControl.CurrentFileLine : DiffText.CurrentFileLine;
}

private void Execute(int scriptId)
{
_scriptRunner.Execute(scriptId, this);
}

internal void RegisterGitHostingPluginInBlameControl()
{
BlameControl.ConfigureRepositoryHostPlugin(PluginRegistry.TryGetGitHosterForModule(Module));
Expand Down
Loading

0 comments on commit e4ce8ac

Please sign in to comment.