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

Commit 069dc80

Browse files
author
Austin Green
committed
Basic implementation for opening selection in browser
1 parent e4c8162 commit 069dc80

File tree

6 files changed

+64
-21
lines changed

6 files changed

+64
-21
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 = "YOUR CLIENT ID HERE";
6-
const string clientSecret = "YOUR CLIENT SECRET HERE";
5+
const string clientId = "";
6+
const string clientSecret = "";
77

88
partial void Configure()
99
{

src/GitHub.Exports/Extensions/SimpleRepositoryModelExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,12 @@ public static bool MightContainSolution(this ISimpleRepositoryModel repository)
3030
.Any(x => ((x.Attributes.HasFlag(FileAttributes.Directory) || x.Attributes.HasFlag(FileAttributes.Normal)) &&
3131
!x.Name.StartsWith(".", StringComparison.Ordinal) && !x.Name.StartsWith("readme", StringComparison.OrdinalIgnoreCase)));
3232
}
33+
34+
public static string CurrentSha(this ISimpleRepositoryModel repository)
35+
{
36+
var repo = GitService.GitServiceHelper.GetRepo(repository.LocalPath);
37+
var firstCommit = repo.Commits.ElementAt(0);
38+
return firstCommit.Sha;
39+
}
3340
}
3441
}

src/GitHub.Exports/Services/IActiveDocument.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
public interface IActiveDocument
44
{
55
string Name { get; }
6-
int Line { get; }
6+
int AnchorLine { get; }
7+
int AnchorColumn { get; }
8+
int EndLine { get; }
9+
int EndColumn { get; }
710
}
811
}

src/GitHub.VisualStudio/GitHub.VisualStudio.vsct

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@
122122

123123
<Button guid="guidContextMenuSet" id="getLinkCommand" priority="0x0100" type="Button">
124124
<Parent guid="guidContextMenuSet" id="idContextMenuGroup"/>
125+
<CommandFlag>DefaultInvisible</CommandFlag>
126+
<CommandFlag>DynamicVisibility</CommandFlag>
125127
<Strings>
126-
<ButtonText>Get Link</ButtonText>
128+
<ButtonText>Open in Browser</ButtonText>
127129
</Strings>
128130
</Button>
129131

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Runtime.InteropServices;
34
using GitHub.Extensions;
45
using GitHub.Services;
@@ -60,16 +61,39 @@ protected override void Initialize()
6061
var windowFrame = (IVsWindowFrame)window.Frame;
6162
ErrorHandler.ThrowOnFailure(windowFrame.Show());
6263
});
64+
ServiceProvider.AddDynamicMenuItem(GuidList.guidContextMenuSet, PkgCmdIDList.getLinkCommand,
65+
IsValidGithubRepo,
66+
OpenRepoInBrowser);
67+
68+
base.Initialize();
69+
}
70+
71+
private bool IsValidGithubRepo()
72+
{
73+
return !string.IsNullOrEmpty(ServiceProvider.GetExportedValue<ITeamExplorerServiceHolder>().ActiveRepo?.CloneUrl?.RepositoryName);
74+
}
75+
76+
private void OpenRepoInBrowser()
77+
{
78+
var activeDocument = ServiceProvider.GetExportedValue<IActiveDocument>();
79+
var activeRepo = ServiceProvider.GetExportedValue<ITeamExplorerServiceHolder>().ActiveRepo;
80+
81+
var currentCommitSha = activeRepo.CurrentSha();
82+
var lineTag = "L" + activeDocument.AnchorLine;
6383

64-
ServiceProvider.AddTopLevelMenuItem(GuidList.guidContextMenuSet, PkgCmdIDList.getLinkCommand, (s, e) =>
84+
if (activeDocument.AnchorLine != activeDocument.EndLine)
6585
{
66-
var ap = ServiceProvider.GetExportedValue<IActiveDocument>();
67-
var name = ap.Name;
68-
var line = ap.Line;
69-
System.Windows.Forms.MessageBox.Show(name + " : " + line);
70-
71-
});
72-
base.Initialize();
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);
94+
95+
var vsBrowserProvider = ServiceProvider.GetExportedValue<IVisualStudioBrowser>();
96+
vsBrowserProvider.OpenUrl(new Uri(outputUri));
7397
}
7498

7599
void StartFlow(UIControllerFlow controllerFlow)

src/GitHub.VisualStudio/Services/ActiveDocument.cs

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

89
namespace GitHub.VisualStudio
910
{
@@ -12,25 +13,31 @@ namespace GitHub.VisualStudio
1213
class ActiveDocument : IActiveDocument
1314
{
1415
public string Name { get; private set; }
15-
public int Line { get; private set; }
16-
public int Column { get; private set; }
16+
public string ShortName { get; private set; }
17+
public int AnchorLine { get; private set; }
18+
public int AnchorColumn { get; private set; }
19+
public int EndLine { get; private set; }
20+
public int EndColumn { get; private set; }
1721

1822
[ImportingConstructor]
1923
public ActiveDocument([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
2024
{
21-
Line = Column = -1;
25+
AnchorLine = AnchorColumn = EndLine = EndColumn = -1;
2226
Name = Services.Dte2?.ActiveDocument?.FullName;
27+
ShortName = Services.Dte2?.ActiveDocument?.Name;
28+
2329
var textManager = serviceProvider.GetService(typeof(SVsTextManager)) as IVsTextManager;
2430
Debug.Assert(textManager != null, "No SVsTextManager service available");
25-
if (textManager == null)
26-
return;
31+
2732
IVsTextView view;
28-
int line, col;
33+
int anchorLine, anchorCol, endLine, endCol;
2934
if (ErrorHandler.Succeeded(textManager.GetActiveView(0, null, out view)) &&
30-
ErrorHandler.Succeeded(view.GetCaretPos(out line, out col)))
35+
ErrorHandler.Succeeded(view.GetSelection(out anchorLine, out anchorCol, out endLine, out endCol)))
3136
{
32-
Line = line;
33-
Column = col;
37+
AnchorLine = anchorLine + 1;
38+
AnchorColumn = anchorCol + 1;
39+
EndLine = endLine + 1;
40+
EndColumn = endCol + 1;
3441
}
3542
}
3643
}

0 commit comments

Comments
 (0)