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

Commit d0dd283

Browse files
committed
Allow navigation from editable diff to code view
This allows navigation from View Changes in Solution to Code View. It also allows navigation from the default Team Explorer changes diff view (Compare with Unmodified...) to code view.
1 parent 871bba1 commit d0dd283

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/GitHub.App/Services/PullRequestEditorService.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.VisualStudio.Text.Editor;
1919
using Microsoft.VisualStudio.Text.Projection;
2020
using Microsoft.VisualStudio.TextManager.Interop;
21+
using EnvDTE;
2122
using Task = System.Threading.Tasks.Task;
2223

2324
namespace GitHub.Services
@@ -275,6 +276,25 @@ static bool RaiseWhenAvailable(string guid, int id, object param)
275276
return false;
276277
}
277278

279+
public void OpenActiveDocumentInCodeView(IVsTextView sourceView)
280+
{
281+
var dte = serviceProvider.GetService<DTE>();
282+
// Not sure how to get a file name directly from IVsTextView. Using DTE.ActiveDocument.FullName.
283+
var fullPath = dte.ActiveDocument.FullName;
284+
// VsShellUtilities.OpenDocument with VSConstants.LOGVIEWID.Code_guid always open a new Code view.
285+
// Using DTE.ItemOperations.OpenFile with Constants.vsViewKindCode instead,
286+
dte.ItemOperations.OpenFile(fullPath, EnvDTE.Constants.vsViewKindCode);
287+
var codeView = FindActiveView();
288+
NavigateToEquivalentPosition(sourceView, codeView);
289+
}
290+
291+
public bool IsEditableDiff(ITextView textView)
292+
{
293+
var readOnly = textView.Options.GetOptionValue(DefaultTextViewOptions.ViewProhibitUserInputId);
294+
var isDiff = IsDiff(textView);
295+
return !readOnly && isDiff;
296+
}
297+
278298
public void NavigateToEquivalentPosition(IVsTextView sourceView, IVsTextView targetView)
279299
{
280300
int line;
@@ -574,6 +594,8 @@ async Task<string> GetBaseFileName(IPullRequestSession session, IPullRequestSess
574594
}
575595
}
576596

597+
static bool IsDiff(ITextView textView) => textView.Roles.Contains("DIFF");
598+
577599
static IDifferenceViewer GetDiffViewer(IVsWindowFrame frame)
578600
{
579601
object docView;

src/GitHub.Exports.Reactive/Services/IPullRequestEditorService.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,21 @@ public interface IPullRequestEditorService
5858
/// <param name="sourceView">The text view to navigate from.</param>
5959
/// <param name="targetView">The text view to navigate to.</param>
6060
void NavigateToEquivalentPosition(IVsTextView sourceView, IVsTextView targetView);
61+
62+
/// <summary>
63+
/// Check to see if text view is an ediatable document.
64+
/// </summary>
65+
/// <param name="textView">The text view to check.</param>
66+
/// <returns>True if text view is editable and part of a diff view.</returns>
67+
bool IsEditableDiff(ITextView textView);
68+
69+
/// <summary>
70+
/// Open the active document in a new code view.
71+
/// </summary>
72+
/// <remarks>
73+
/// If the active document is part of a diff view, open in a new code view.
74+
/// </remarks>
75+
/// <param name="sourceView">The source view to use the line and position from.</param>
76+
void OpenActiveDocumentInCodeView(IVsTextView sourceView);
6177
}
6278
}

src/GitHub.VisualStudio/Commands/GoToSolutionOrPullRequestFileCommand.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ public override async Task Execute()
6565

6666
var textView = editorAdapter.Value.GetWpfTextView(sourceView);
6767

68+
bool isEditableDiff = pullRequestEditorService.Value.IsEditableDiff(textView);
69+
if (isEditableDiff)
70+
{
71+
// Open active document in Code View
72+
pullRequestEditorService.Value.OpenActiveDocumentInCodeView(sourceView);
73+
return;
74+
}
75+
6876
var info = sessionManager.Value.GetTextBufferInfo(textView.TextBuffer);
6977
if (info != null)
7078
{
@@ -144,6 +152,15 @@ void OnBeforeQueryStatus(object sender, EventArgs e)
144152
}
145153

146154
var textView = editorAdapter.Value.GetWpfTextView(sourceView);
155+
156+
if (pullRequestEditorService.Value.IsEditableDiff(textView))
157+
{
158+
// Active text view is editable diff
159+
Text = "Open File in Code View";
160+
Visible = true;
161+
return;
162+
}
163+
147164
var info = sessionManager.Value.GetTextBufferInfo(textView.TextBuffer);
148165
if (info != null)
149166
{

0 commit comments

Comments
 (0)