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

Commit 38ac072

Browse files
committed
Port changes from #1407.
Enable navigation from diff view to editor
1 parent 68e3e4c commit 38ac072

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

src/GitHub.App/GitHub.App.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
<Compile Include="Services\PullRequestEditorService.cs" />
214214
<Compile Include="Services\TeamExplorerContext.cs" />
215215
<Compile Include="Services\OAuthCallbackListener.cs" />
216+
<Compile Include="Services\TextViewCommandDispatcher.cs" />
216217
<Compile Include="ViewModels\Dialog\GistCreationViewModel.cs" />
217218
<Compile Include="ViewModels\Dialog\GitHubDialogWindowViewModel.cs" />
218219
<Compile Include="ViewModels\Dialog\LoginCredentialsViewModel.cs" />

src/GitHub.App/Services/PullRequestEditorService.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
using GitHub.Commands;
1010
using GitHub.Extensions;
1111
using GitHub.Models;
12+
using GitHub.ViewModels.GitHubPane;
1213
using GitHub.VisualStudio;
1314
using Microsoft.VisualStudio;
1415
using Microsoft.VisualStudio.Editor;
1516
using Microsoft.VisualStudio.Shell;
1617
using Microsoft.VisualStudio.Shell.Interop;
1718
using Microsoft.VisualStudio.Text;
19+
using Microsoft.VisualStudio.Text.Editor;
1820
using Microsoft.VisualStudio.Text.Projection;
1921
using Microsoft.VisualStudio.TextManager.Interop;
2022
using Task = System.Threading.Tasks.Task;
@@ -151,6 +153,9 @@ public async Task OpenDiff(
151153
if (!workingDirectory)
152154
{
153155
AddBufferTag(diffViewer.RightView.TextBuffer, session, rightPath, DiffSide.Right);
156+
EnableNavigateToEditor(diffViewer.LeftView, session, file);
157+
EnableNavigateToEditor(diffViewer.RightView, session, file);
158+
EnableNavigateToEditor(diffViewer.InlineView, session, file);
154159
}
155160

156161
if (workingDirectory)
@@ -350,6 +355,11 @@ IVsTextView OpenDocument(string fullPath)
350355
return view;
351356
}
352357

358+
void ShowErrorInStatusBar(string message)
359+
{
360+
statusBar.ShowMessage(message);
361+
}
362+
353363
void ShowErrorInStatusBar(string message, Exception e)
354364
{
355365
statusBar.ShowMessage(message + ": " + e.Message);
@@ -372,6 +382,54 @@ void AddBufferTag(ITextBuffer buffer, IPullRequestSession session, string path,
372382
}
373383
}
374384

385+
void EnableNavigateToEditor(IWpfTextView textView, IPullRequestSession session, IPullRequestSessionFile file)
386+
{
387+
var view = vsEditorAdaptersFactory.GetViewAdapter(textView);
388+
EnableNavigateToEditor(view, session, file);
389+
}
390+
391+
void EnableNavigateToEditor(IVsTextView textView, IPullRequestSession session, IPullRequestSessionFile file)
392+
{
393+
var commandGroup = VSConstants.CMDSETID.StandardCommandSet2K_guid;
394+
var commandId = (int)VSConstants.VSStd2KCmdID.RETURN;
395+
new TextViewCommandDispatcher(textView, commandGroup, commandId).Exec +=
396+
async (s, e) => await DoNavigateToEditor(session, file);
397+
398+
var contextMenuCommandGroup = new Guid(Guids.guidContextMenuSetString);
399+
var goToCommandId = PkgCmdIDList.openFileInSolutionCommand;
400+
new TextViewCommandDispatcher(textView, contextMenuCommandGroup, goToCommandId).Exec +=
401+
async (s, e) => await DoNavigateToEditor(session, file);
402+
}
403+
404+
async Task DoNavigateToEditor(IPullRequestSession session, IPullRequestSessionFile file)
405+
{
406+
try
407+
{
408+
if (!session.IsCheckedOut)
409+
{
410+
ShowInfoMessage("Checkout PR branch before opening file in solution.");
411+
return;
412+
}
413+
414+
var fullPath = GetAbsolutePath(session, file);
415+
416+
var activeView = FindActiveView();
417+
if (activeView == null)
418+
{
419+
ShowErrorInStatusBar("Couldn't find active view");
420+
return;
421+
}
422+
423+
NavigateToEquivalentPosition(activeView, fullPath);
424+
425+
await usageTracker.IncrementCounter(x => x.NumberOfPRDetailsNavigateToEditor);
426+
}
427+
catch (Exception e)
428+
{
429+
ShowErrorInStatusBar("Error navigating to editor", e);
430+
}
431+
}
432+
375433
async Task<string> ExtractFile(IPullRequestSession session, IPullRequestSessionFile file, bool head)
376434
{
377435
var encoding = pullRequestService.GetEncoding(session.LocalRepository, file.RelativePath);
@@ -424,6 +482,13 @@ async Task<string> GetBaseFileName(IPullRequestSession session, IPullRequestSess
424482
}
425483
}
426484

485+
void ShowInfoMessage(string message)
486+
{
487+
ErrorHandler.ThrowOnFailure(VsShellUtilities.ShowMessageBox(
488+
serviceProvider, message, null,
489+
OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST));
490+
}
491+
427492
static string GetAbsolutePath(IPullRequestSession session, IPullRequestSessionFile file)
428493
{
429494
return Path.Combine(session.LocalRepository.LocalPath, file.RelativePath);

src/GitHub.VisualStudio/Views/GitHubPane/TextViewCommandDispatcher.cs renamed to src/GitHub.App/Services/TextViewCommandDispatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Microsoft.VisualStudio.OLE.Interop;
44
using Microsoft.VisualStudio.TextManager.Interop;
55

6-
namespace GitHub.VisualStudio.Views.GitHubPane
6+
namespace GitHub.Services
77
{
88
/// <summary>
99
/// Intercepts all commands sent to a <see cref="IVsTextView"/> and fires <see href="Exec"/> when a specified command is encountered.

src/GitHub.VisualStudio/GitHub.VisualStudio.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@
400400
<Compile Include="Views\GitHubPane\NotAGitRepositoryView.xaml.cs">
401401
<DependentUpon>NotAGitRepositoryView.xaml</DependentUpon>
402402
</Compile>
403-
<Compile Include="Views\GitHubPane\TextViewCommandDispatcher.cs" />
404403
<Compile Include="Views\TeamExplorer\RepositoryPublishView.xaml.cs">
405404
<DependentUpon>RepositoryPublishView.xaml</DependentUpon>
406405
</Compile>

0 commit comments

Comments
 (0)