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

Commit 2f94a75

Browse files
committed
Trigger navigation using command dispacher
Currently uses VSStd2KCmdID.RETURN command.
1 parent c07bd16 commit 2f94a75

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

src/GitHub.VisualStudio/GitHub.VisualStudio.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@
391391
<Compile Include="Views\GitHubPane\NotAGitRepositoryView.xaml.cs">
392392
<DependentUpon>NotAGitRepositoryView.xaml</DependentUpon>
393393
</Compile>
394+
<Compile Include="Views\GitHubPane\TextViewCommandDispatcher.cs" />
394395
<Compile Include="Views\TeamExplorer\RepositoryPublishView.xaml.cs">
395396
<DependentUpon>RepositoryPublishView.xaml</DependentUpon>
396397
</Compile>

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public PullRequestDetailView()
7272
[Import]
7373
INavigationService NavigationService { get; set; }
7474

75+
[Import]
76+
IVsEditorAdaptersFactoryService EditorAdaptersFactoryService { get; set; }
77+
7578
protected override void OnVisualParentChanged(DependencyObject oldParent)
7679
{
7780
base.OnVisualParentChanged(oldParent);
@@ -228,19 +231,11 @@ void AddBufferTag(ITextBuffer buffer, IPullRequestSession session, string path,
228231

229232
void EnableNavigateToEditor(IWpfTextView textView, IPullRequestFileNode file)
230233
{
231-
textView.VisualElement.PreviewKeyDown += async (s, e) =>
232-
{
233-
if (e.Key == Key.Space)
234-
{
235-
await DoOpenLiveFile(file);
236-
}
237-
else
238-
{
239-
Services.Dte.StatusBar.Text = "Press space bar to open file in solution";
240-
}
241-
242-
e.Handled = true;
243-
};
234+
var view = EditorAdaptersFactoryService.GetViewAdapter(textView);
235+
var commandGroup = VSConstants.CMDSETID.StandardCommandSet2K_guid;
236+
var commandId = (int)VSConstants.VSStd2KCmdID.RETURN;
237+
var dispatcher = new TextViewCommandDispatcher(view, commandGroup, commandId);
238+
dispatcher.Exec += async (s, e) => await DoOpenLiveFile(file);
244239
}
245240

246241
void ShowErrorInStatusBar(string message, Exception e = null)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using Microsoft.VisualStudio;
3+
using Microsoft.VisualStudio.OLE.Interop;
4+
using Microsoft.VisualStudio.TextManager.Interop;
5+
6+
namespace GitHub.VisualStudio.Views.GitHubPane
7+
{
8+
class TextViewCommandDispatcher : IOleCommandTarget, IDisposable
9+
{
10+
readonly IVsTextView textView;
11+
readonly Guid commandGroup;
12+
readonly int commandId;
13+
readonly IOleCommandTarget next;
14+
15+
public TextViewCommandDispatcher(IVsTextView textView, Guid commandGroup, int commandId)
16+
{
17+
this.textView = textView;
18+
this.commandGroup = commandGroup;
19+
this.commandId = commandId;
20+
21+
ErrorHandler.ThrowOnFailure(textView.AddCommandFilter(this, out next));
22+
}
23+
24+
public void Dispose()
25+
{
26+
ErrorHandler.ThrowOnFailure(textView.RemoveCommandFilter(this));
27+
}
28+
29+
int IOleCommandTarget.Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
30+
{
31+
if (pguidCmdGroup == commandGroup && nCmdID == commandId)
32+
{
33+
Exec?.Invoke(this, EventArgs.Empty);
34+
return 0;
35+
}
36+
37+
return next?.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut) ?? 0;
38+
}
39+
40+
int IOleCommandTarget.QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
41+
{
42+
return next?.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText) ?? 0;
43+
}
44+
45+
public event EventHandler Exec;
46+
}
47+
}

0 commit comments

Comments
 (0)