Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit ab7ec8b

Browse files
Merge pull request #1836 from github/fixes/1348-dont-open-deleted-file
Don't allow "Open File in Solution" for deleted files.
2 parents e486cf9 + c738fec commit ab7ec8b

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/GitHub.App/ViewModels/GitHubPane/PullRequestFilesViewModel.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
using System.Reactive.Disposables;
88
using System.Reactive.Linq;
99
using System.Reactive.Subjects;
10+
using System.Reactive.Threading.Tasks;
1011
using System.Threading.Tasks;
12+
using System.Windows.Input;
1113
using GitHub.Extensions;
1214
using GitHub.Models;
1315
using GitHub.Services;
@@ -50,7 +52,7 @@ public PullRequestFilesViewModel(
5052
DiffFileWithWorkingDirectory = ReactiveCommand.CreateAsyncTask(
5153
isBranchCheckedOut,
5254
x => (Task)editorService.OpenDiff(pullRequestSession, ((IPullRequestFileNode)x).RelativePath));
53-
OpenFileInWorkingDirectory = ReactiveCommand.CreateAsyncTask(
55+
OpenFileInWorkingDirectory = new NonDeletedFileCommand(
5456
isBranchCheckedOut,
5557
x => (Task)editorService.OpenFile(pullRequestSession, ((IPullRequestFileNode)x).RelativePath, true));
5658

@@ -198,5 +200,37 @@ async Task<IInlineCommentThreadModel> GetFirstCommentThread(IPullRequestFileNode
198200

199201
return threads.FirstOrDefault();
200202
}
203+
204+
/// <summary>
205+
/// Implements the <see cref="OpenFileInWorkingDirectory"/> command.
206+
/// </summary>
207+
/// <remarks>
208+
/// We need to "Open File in Solution" when the parameter passed to the command parameter
209+
/// represents a deleted file. ReactiveCommand doesn't allow us to change the CanExecute
210+
/// state depending on the parameter, so we override
211+
/// <see cref="ICommand.CanExecute(object)"/> to do this ourselves.
212+
/// </remarks>
213+
class NonDeletedFileCommand : ReactiveCommand<Unit>, ICommand
214+
{
215+
public NonDeletedFileCommand(
216+
IObservable<bool> canExecute,
217+
Func<object, Task> executeAsync)
218+
: base(canExecute, x => executeAsync(x).ToObservable())
219+
{
220+
}
221+
222+
bool ICommand.CanExecute(object parameter)
223+
{
224+
if (parameter is IPullRequestFileNode node)
225+
{
226+
if (node.Status == PullRequestFileStatus.Removed)
227+
{
228+
return false;
229+
}
230+
}
231+
232+
return CanExecute(parameter);
233+
}
234+
}
201235
}
202236
}

src/GitHub.VisualStudio/Views/GitHubPane/PullRequestFilesView.xaml.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ void ApplyContextMenuBinding<TItem>(object sender, ContextMenuEventArgs e) where
6161

6262
if (fileNode != null)
6363
{
64-
container.ContextMenu.DataContext = this.DataContext;
65-
6664
foreach (var menuItem in container.ContextMenu.Items.OfType<MenuItem>())
6765
{
6866
menuItem.CommandParameter = fileNode;
6967
}
7068

69+
// HACK: MenuItem doesn't re-query ICommand.CanExecute when CommandParameter changes. Force
70+
// this to happen by resetting its DataContext to null and then to the correct value.
71+
container.ContextMenu.DataContext = null;
72+
container.ContextMenu.DataContext = this.DataContext;
7173
e.Handled = false;
7274
}
7375
}

0 commit comments

Comments
 (0)