This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathGoToSolutionOrPullRequestFileCommand.cs
More file actions
310 lines (270 loc) · 12.1 KB
/
Copy pathGoToSolutionOrPullRequestFileCommand.cs
File metadata and controls
310 lines (270 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using System.IO;
using System.ComponentModel.Composition;
using GitHub.Services;
using GitHub.Extensions;
using GitHub.VisualStudio;
using GitHub.Services.Vssdk.Commands;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Differencing;
using Microsoft.VisualStudio.TextManager.Interop;
using Task = System.Threading.Tasks.Task;
namespace GitHub.Commands
{
/// <summary>
/// Navigate from a PR file to the equivalent file and location in the editor (or the reverse).
/// </summary>
/// <remarks>
/// This command will do one of the following depending on context.
/// Navigate from PR file diff to the working file in the solution.
/// Navigate from the working file in the solution to the PR file diff.
/// Navigate from an editable diff (e.g. 'View Changes in Solution') to the editor view.
/// </remarks>
[Export(typeof(IGoToSolutionOrPullRequestFileCommand))]
public class GoToSolutionOrPullRequestFileCommand : VsCommand, IGoToSolutionOrPullRequestFileCommand
{
/// <summary>
/// Gets the GUID of the group the command belongs to.
/// </summary>
public static readonly Guid CommandSet = Guids.guidContextMenuSet;
/// <summary>
/// Gets the numeric identifier of the command.
/// </summary>
public const int CommandId = PkgCmdIDList.goToSolutionOrPullRequestFileCommand;
readonly IGitHubServiceProvider serviceProvider;
readonly Lazy<IVsEditorAdaptersFactoryService> editorAdapter;
readonly Lazy<IPullRequestSessionManager> sessionManager;
readonly Lazy<IPullRequestEditorService> pullRequestEditorService;
readonly Lazy<ITeamExplorerContext> teamExplorerContext;
readonly Lazy<IGitHubContextService> gitHubContextService;
readonly Lazy<IStatusBarNotificationService> statusBar;
readonly Lazy<IUsageTracker> usageTracker;
[ImportingConstructor]
public GoToSolutionOrPullRequestFileCommand(
IGitHubServiceProvider serviceProvider,
Lazy<IVsEditorAdaptersFactoryService> editorAdapter,
Lazy<IPullRequestSessionManager> sessionManager,
Lazy<IPullRequestEditorService> pullRequestEditorService,
Lazy<ITeamExplorerContext> teamExplorerContext,
Lazy<IGitHubContextService> gitHubContextService,
Lazy<IStatusBarNotificationService> statusBar,
Lazy<IUsageTracker> usageTracker) : base(CommandSet, CommandId)
{
this.serviceProvider = serviceProvider;
this.editorAdapter = editorAdapter;
this.sessionManager = sessionManager;
this.pullRequestEditorService = pullRequestEditorService;
this.gitHubContextService = gitHubContextService;
this.teamExplorerContext = teamExplorerContext;
this.statusBar = statusBar;
this.usageTracker = usageTracker;
BeforeQueryStatus += OnBeforeQueryStatus;
}
public override async Task Execute()
{
usageTracker.Value.IncrementCounter(x => x.ExecuteGoToSolutionOrPullRequestFileCommand).Forget();
try
{
var sourceView = pullRequestEditorService.Value.FindActiveView();
if (sourceView == null)
{
ShowErrorInStatusBar("Couldn't find active source view");
return;
}
var textView = editorAdapter.Value.GetWpfTextView(sourceView);
bool isEditableDiff = pullRequestEditorService.Value.IsEditableDiff(textView);
if (isEditableDiff)
{
// Navigating from editable diff to code view
await usageTracker.Value.IncrementCounter(x => x.NumberOfNavigateToCodeView);
// Open active document in Code View
pullRequestEditorService.Value.OpenActiveDocumentInCodeView(sourceView);
return;
}
var info = sessionManager.Value.GetTextBufferInfo(textView.TextBuffer);
if (info != null)
{
if (!info.Session.IsCheckedOut)
{
ShowInfoMessage(Resources.NavigateToEditorNotCheckedOutInfoMessage);
return;
}
// Navigate from PR file to solution
await usageTracker.Value.IncrementCounter(x => x.NumberOfPRDetailsNavigateToEditor);
var fileTextView = await pullRequestEditorService.Value.OpenFile(info.Session, info.RelativePath, true);
if (fileTextView == null)
{
ShowErrorInStatusBar("Couldn't find active target view");
return;
}
var fileView = editorAdapter.Value.GetViewAdapter(fileTextView);
pullRequestEditorService.Value.NavigateToEquivalentPosition(sourceView, fileView);
return;
}
if (TryNavigateFromHistoryFile(sourceView))
{
return;
}
var relativePath = sessionManager.Value.GetRelativePath(textView.TextBuffer);
if (relativePath == null)
{
ShowErrorInStatusBar("File isn't part of repository");
return;
}
var session = sessionManager.Value.CurrentSession;
if (session == null)
{
ShowErrorInStatusBar("Couldn't find Pull request session");
return;
}
// Navigate from solution file to PR diff file
await usageTracker.Value.IncrementCounter(x => x.NumberOfNavigateToPullRequestFileDiff);
var diffViewer = await pullRequestEditorService.Value.OpenDiff(session, relativePath, "HEAD", scrollToFirstDiff: false);
if (diffViewer == null)
{
ShowErrorInStatusBar("Couldn't find active diff viewer");
return;
}
var diffTextView = FindActiveTextView(diffViewer);
var diffView = editorAdapter.Value.GetViewAdapter(diffTextView);
pullRequestEditorService.Value.NavigateToEquivalentPosition(sourceView, diffView);
}
catch (Exception e)
{
ShowErrorInStatusBar("Error Navigating", e);
}
}
void OnBeforeQueryStatus(object sender, EventArgs e)
{
try
{
var sourceView = pullRequestEditorService.Value.FindActiveView();
if (sourceView == null)
{
// No active text view
Visible = false;
return;
}
var textView = editorAdapter.Value.GetWpfTextView(sourceView);
if (pullRequestEditorService.Value.IsEditableDiff(textView))
{
// Active text view is editable diff
Text = "Open File in Code View";
Visible = true;
return;
}
var info = sessionManager.Value.GetTextBufferInfo(textView.TextBuffer);
if (info != null)
{
// Active text view is a PR file
Text = "Open File in Solution";
Visible = true;
return;
}
var session = sessionManager.Value.CurrentSession;
if (session != null)
{
var relativePath = sessionManager.Value.GetRelativePath(textView.TextBuffer);
if (relativePath != null)
{
// Active text view is part of a repository
Text = "View Changes in #" + session.PullRequest.Number;
Visible = true;
return;
}
}
if (TryNavigateFromHistoryFileQueryStatus(sourceView))
{
return;
}
}
catch (Exception ex)
{
ShowErrorInStatusBar("Error QueryStatus", ex);
}
Visible = false;
}
// Set command Text/Visible properties and return true when active
bool TryNavigateFromHistoryFileQueryStatus(IVsTextView sourceView)
{
if (teamExplorerContext.Value.ActiveRepository?.LocalPath is string && // Check there is an active repo
FindObjectishForTFSTempFile(sourceView) is string) // Looks like a history file
{
// Navigate from history file is active
Text = "Open File in Solution";
Visible = true;
return true;
}
return false;
}
// Attempt navigation to historical file
bool TryNavigateFromHistoryFile(IVsTextView sourceView)
{
if (teamExplorerContext.Value.ActiveRepository?.LocalPath is string repositoryDir &&
FindObjectishForTFSTempFile(sourceView) is string objectish)
{
var (_, blobPath) = gitHubContextService.Value.ResolveBlobFromHistory(repositoryDir, objectish);
if (blobPath is string)
{
var workingFile = Path.Combine(repositoryDir, blobPath);
VsShellUtilities.OpenDocument(serviceProvider, workingFile, VSConstants.LOGVIEWID.TextView_guid,
out IVsUIHierarchy hierarchy, out uint itemID, out IVsWindowFrame windowFrame, out IVsTextView targetView);
pullRequestEditorService.Value.NavigateToEquivalentPosition(sourceView, targetView);
return true;
}
}
return false;
}
// Find the blob SHA in a file name if any
string FindObjectishForTFSTempFile(IVsTextView sourceView)
{
return
FindPath(sourceView) is string path &&
gitHubContextService.Value.FindObjectishForTFSTempFile(path) is string objectish ?
objectish : null;
}
// See http://microsoft.public.vstudio.extensibility.narkive.com/agfoD1GO/full-pathname-of-file-shown-in-current-view-of-core-editor#post2
static string FindPath(IVsTextView textView)
{
ErrorHandler.ThrowOnFailure(textView.GetBuffer(out IVsTextLines buffer));
var userData = buffer as IVsUserData;
if (userData == null)
{
return null;
}
ErrorHandler.ThrowOnFailure(userData.GetData(typeof(IVsUserData).GUID, out object data));
return data as string;
}
ITextView FindActiveTextView(IDifferenceViewer diffViewer)
{
switch (diffViewer.ActiveViewType)
{
case DifferenceViewType.InlineView:
return diffViewer.InlineView;
case DifferenceViewType.LeftView:
return diffViewer.LeftView;
case DifferenceViewType.RightView:
return diffViewer.RightView;
}
return null;
}
void ShowInfoMessage(string message)
{
ErrorHandler.ThrowOnFailure(VsShellUtilities.ShowMessageBox(
serviceProvider, message, null,
OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST));
}
void ShowErrorInStatusBar(string message)
{
statusBar.Value.ShowMessage(message);
}
void ShowErrorInStatusBar(string message, Exception e)
{
statusBar.Value.ShowMessage(message + ": " + e.Message);
}
}
}