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

Commit d4d7762

Browse files
author
Austin Green
committed
Refactored browser url creation and added copy to clipboard menu item
1 parent 069dc80 commit d4d7762

File tree

6 files changed

+80
-20
lines changed

6 files changed

+80
-20
lines changed

src/GitHub.App/Api/ApiClientConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ namespace GitHub.Api
22
{
33
public partial class ApiClient : IApiClient
44
{
5-
const string clientId = "";
6-
const string clientSecret = "";
5+
const string clientId = "YOUR CLIENT ID HERE";
6+
const string clientSecret = "YOUR CLIENT SECRET HERE";
77

88
partial void Configure()
99
{

src/GitHub.Exports/Extensions/SimpleRepositoryModelExtensions.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Globalization;
23
using System.Linq;
34
using System.IO;
45
using GitHub.Models;
56
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
67
using GitHub.Services;
8+
using GitHub.VisualStudio;
79

810
namespace GitHub.Extensions
911
{
@@ -33,9 +35,43 @@ public static bool MightContainSolution(this ISimpleRepositoryModel repository)
3335

3436
public static string CurrentSha(this ISimpleRepositoryModel repository)
3537
{
38+
if (repository == null)
39+
return null;
40+
3641
var repo = GitService.GitServiceHelper.GetRepo(repository.LocalPath);
37-
var firstCommit = repo.Commits.ElementAt(0);
38-
return firstCommit.Sha;
42+
43+
if (repo == null)
44+
return null;
45+
46+
return !repo.Commits.Any() ? null : repo.Commits.ElementAt(0).Sha;
47+
}
48+
49+
public static string BrowserUrl(this ISimpleRepositoryModel repository, IActiveDocument activeDocument)
50+
{
51+
if (repository == null || activeDocument == null)
52+
return null;
53+
54+
var currentCommitSha = repository.CurrentSha();
55+
var currentCloneUrl = repository.CloneUrl;
56+
var localPath = repository.LocalPath;
57+
var lineTag = "L" + activeDocument.AnchorLine;
58+
59+
if (string.IsNullOrEmpty(currentCommitSha) || string.IsNullOrEmpty(currentCloneUrl) ||
60+
string.IsNullOrEmpty(localPath))
61+
return null;
62+
63+
if (activeDocument.AnchorLine != activeDocument.EndLine)
64+
{
65+
lineTag += "-L" + activeDocument.EndLine;
66+
}
67+
68+
var outputUri = string.Format(CultureInfo.CurrentCulture, "{0}/blob/{1}{2}#{3}",
69+
currentCloneUrl,
70+
currentCommitSha,
71+
activeDocument.Name.Replace(localPath, "").Replace("\\", "/"),
72+
lineTag);
73+
74+
return outputUri;
3975
}
4076
}
4177
}

src/GitHub.VisualStudio/GitHub.VisualStudio.vsct

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@
129129
</Strings>
130130
</Button>
131131

132+
<Button guid="guidContextMenuSet" id="copyLinkCommand" priority="0x0101" type="Button">
133+
<Parent guid="guidContextMenuSet" id="idContextMenuGroup"/>
134+
<CommandFlag>DefaultInvisible</CommandFlag>
135+
<CommandFlag>DynamicVisibility</CommandFlag>
136+
<Strings>
137+
<ButtonText>Copy Link to Clipboard</ButtonText>
138+
</Strings>
139+
</Button>
132140
</Buttons>
133141

134142
</Commands>
@@ -189,6 +197,7 @@
189197
<GuidSymbol name="guidContextMenuSet" value="{31057D08-8C3C-4C5B-9F91-8682EA08EC27}">
190198
<IDSymbol name="idContextMenuGroup" value="0x1010" />
191199
<IDSymbol name="getLinkCommand" value="0x100" />
200+
<IDSymbol name="copyLinkCommand" value="0x101"/>
192201
</GuidSymbol>
193202

194203
<GuidSymbol name="GUID_XAML_EDITOR" value="{4C87B692-1202-46AA-B64C-EF01FAEC53DA}">

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Globalization;
33
using System.Runtime.InteropServices;
44
using GitHub.Extensions;
5+
using GitHub.Models;
56
using GitHub.Services;
67
using GitHub.UI;
78
using GitHub.VisualStudio.Base;
@@ -64,33 +65,46 @@ protected override void Initialize()
6465
ServiceProvider.AddDynamicMenuItem(GuidList.guidContextMenuSet, PkgCmdIDList.getLinkCommand,
6566
IsValidGithubRepo,
6667
OpenRepoInBrowser);
67-
68+
ServiceProvider.AddDynamicMenuItem(GuidList.guidContextMenuSet, PkgCmdIDList.copyLinkCommand,
69+
IsValidGithubRepo,
70+
CopyRepoLinkToClipboard);
71+
6872
base.Initialize();
6973
}
7074

75+
private void CopyRepoLinkToClipboard()
76+
{
77+
if (!IsValidGithubRepo()) return;
78+
79+
var activeDocument = ServiceProvider.GetExportedValue<IActiveDocument>();
80+
var activeRepo = ServiceProvider.GetExportedValue<ITeamExplorerServiceHolder>().ActiveRepo;
81+
var outputUri = activeRepo?.BrowserUrl(activeDocument);
82+
83+
if (string.IsNullOrEmpty(outputUri)) return;
84+
85+
System.Windows.Clipboard.SetText(outputUri);
86+
}
87+
7188
private bool IsValidGithubRepo()
7289
{
73-
return !string.IsNullOrEmpty(ServiceProvider.GetExportedValue<ITeamExplorerServiceHolder>().ActiveRepo?.CloneUrl?.RepositoryName);
90+
var cloneUrl = ServiceProvider.GetExportedValue<ITeamExplorerServiceHolder>().ActiveRepo?.CloneUrl;
91+
92+
if (string.IsNullOrEmpty(cloneUrl))
93+
return false;
94+
95+
return cloneUrl.Host == "github.com";
7496
}
7597

7698
private void OpenRepoInBrowser()
7799
{
100+
if (!IsValidGithubRepo()) return;
101+
78102
var activeDocument = ServiceProvider.GetExportedValue<IActiveDocument>();
79103
var activeRepo = ServiceProvider.GetExportedValue<ITeamExplorerServiceHolder>().ActiveRepo;
80104

81-
var currentCommitSha = activeRepo.CurrentSha();
82-
var lineTag = "L" + activeDocument.AnchorLine;
105+
var outputUri = activeRepo?.BrowserUrl(activeDocument);
83106

84-
if (activeDocument.AnchorLine != activeDocument.EndLine)
85-
{
86-
lineTag += "-L" + activeDocument.EndLine;
87-
}
88-
89-
var outputUri = string.Format(CultureInfo.CurrentCulture, "{0}/blob/{1}{2}#L{3}",
90-
activeRepo.CloneUrl,
91-
currentCommitSha,
92-
activeDocument.Name.Replace(activeRepo.LocalPath, "").Replace("\\", "/"),
93-
lineTag);
107+
if (string.IsNullOrEmpty(outputUri)) return;
94108

95109
var vsBrowserProvider = ServiceProvider.GetExportedValue<IVisualStudioBrowser>();
96110
vsBrowserProvider.OpenUrl(new Uri(outputUri));

src/GitHub.VisualStudio/PkgCmdID.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ static class PkgCmdIDList
1212
public const int refreshCommand = 0x302;
1313
public const int pullRequestCommand = 0x310;
1414
public const int getLinkCommand = 0x100;
15+
public const int copyLinkCommand = 0x101;
1516
};
1617
}

src/GitHub.VisualStudio/Services/ActiveDocument.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.ComponentModel.Composition;
66
using System.Diagnostics;
7-
using System.Windows.Media.TextFormatting;
87

98
namespace GitHub.VisualStudio
109
{
@@ -28,7 +27,8 @@ public ActiveDocument([Import(typeof(SVsServiceProvider))] IServiceProvider serv
2827

2928
var textManager = serviceProvider.GetService(typeof(SVsTextManager)) as IVsTextManager;
3029
Debug.Assert(textManager != null, "No SVsTextManager service available");
31-
30+
if (textManager == null)
31+
return;
3232
IVsTextView view;
3333
int anchorLine, anchorCol, endLine, endCol;
3434
if (ErrorHandler.Succeeded(textManager.GetActiveView(0, null, out view)) &&

0 commit comments

Comments
 (0)