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

Commit deb07d5

Browse files
committed
Refactor navigation into NavigationService
1 parent 4310c4e commit deb07d5

File tree

6 files changed

+111
-55
lines changed

6 files changed

+111
-55
lines changed

src/GitHub.App/GitHub.App.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
<HintPath>..\..\packages\Microsoft.VisualStudio.ComponentModelHost.14.0.25424\lib\net45\Microsoft.VisualStudio.ComponentModelHost.dll</HintPath>
5959
<Private>True</Private>
6060
</Reference>
61+
<Reference Include="Microsoft.VisualStudio.OLE.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
62+
<HintPath>..\..\packages\Microsoft.VisualStudio.OLE.Interop.7.10.6070\lib\Microsoft.VisualStudio.OLE.Interop.dll</HintPath>
63+
<Private>True</Private>
64+
</Reference>
6165
<Reference Include="Microsoft.VisualStudio.Shell.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
6266
<HintPath>..\..\packages\Microsoft.VisualStudio.Shell.14.0.14.3.25407\lib\Microsoft.VisualStudio.Shell.14.0.dll</HintPath>
6367
<Private>True</Private>
@@ -66,10 +70,18 @@
6670
<HintPath>..\..\packages\Microsoft.VisualStudio.Shell.Immutable.10.0.10.0.30319\lib\net40\Microsoft.VisualStudio.Shell.Immutable.10.0.dll</HintPath>
6771
<Private>True</Private>
6872
</Reference>
73+
<Reference Include="Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
74+
<HintPath>..\..\packages\Microsoft.VisualStudio.Shell.Interop.7.10.6071\lib\Microsoft.VisualStudio.Shell.Interop.dll</HintPath>
75+
<Private>True</Private>
76+
</Reference>
6977
<Reference Include="Microsoft.VisualStudio.TextManager.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
7078
<HintPath>..\..\packages\Microsoft.VisualStudio.TextManager.Interop.7.10.6070\lib\Microsoft.VisualStudio.TextManager.Interop.dll</HintPath>
7179
<Private>True</Private>
7280
</Reference>
81+
<Reference Include="Microsoft.VisualStudio.TextManager.Interop.8.0, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
82+
<HintPath>..\..\packages\Microsoft.VisualStudio.TextManager.Interop.8.0.8.0.50727\lib\Microsoft.VisualStudio.TextManager.Interop.8.0.dll</HintPath>
83+
<Private>True</Private>
84+
</Reference>
7385
<Reference Include="Microsoft.VisualStudio.Threading, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
7486
<SpecificVersion>False</SpecificVersion>
7587
<HintPath>..\..\packages\Microsoft.VisualStudio.Threading.14.1.131\lib\net45\Microsoft.VisualStudio.Threading.dll</HintPath>
@@ -138,6 +150,7 @@
138150
<Compile Include="Models\PullRequestReviewCommentModel.cs" />
139151
<Compile Include="Models\PullRequestDetailArgument.cs" />
140152
<Compile Include="Services\GlobalConnection.cs" />
153+
<Compile Include="Services\NavigationService.cs" />
141154
<Compile Include="ViewModels\Dialog\GistCreationViewModel.cs" />
142155
<Compile Include="ViewModels\Dialog\GitHubDialogWindowViewModel.cs" />
143156
<Compile Include="ViewModels\Dialog\LoginCredentialsViewModel.cs" />
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.ComponentModel.Composition;
3+
using Microsoft.VisualStudio;
4+
using Microsoft.VisualStudio.Shell;
5+
using Microsoft.VisualStudio.Shell.Interop;
6+
using Microsoft.VisualStudio.TextManager.Interop;
7+
8+
namespace GitHub.Services
9+
{
10+
[Export(typeof(INavigationService))]
11+
public class NavigationService : INavigationService
12+
{
13+
readonly IServiceProvider serviceProvider;
14+
15+
[ImportingConstructor]
16+
public NavigationService([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
17+
{
18+
this.serviceProvider = serviceProvider;
19+
}
20+
21+
public IVsTextView NavigateToEquivalentPosition(IVsTextView sourceView, string targetFile)
22+
{
23+
int line;
24+
int column;
25+
sourceView.GetCaretPos(out line, out column);
26+
var text1 = GetText(sourceView);
27+
28+
var view = OpenDocument(targetFile);
29+
var text2 = VsShellUtilities.GetRunningDocumentContents(serviceProvider, targetFile);
30+
31+
var equivalentLine = FindEquivalentLine(text1, text2, line);
32+
33+
view.SetCaretPos(equivalentLine, column);
34+
view.CenterLines(equivalentLine, 1);
35+
36+
return view;
37+
}
38+
39+
public IVsTextView FindActiveView()
40+
{
41+
var textManager = (IVsTextManager2)serviceProvider.GetService(typeof(SVsTextManager));
42+
IVsTextView view;
43+
var hresult = textManager.GetActiveView2(1, null, (uint)_VIEWFRAMETYPE.vftCodeWindow, out view);
44+
return hresult == VSConstants.S_OK ? view : null;
45+
}
46+
47+
int FindEquivalentLine(string text1, string text2, int line)
48+
{
49+
return line;
50+
}
51+
52+
string GetText(IVsTextView textView)
53+
{
54+
IVsTextLines buffer;
55+
ErrorHandler.ThrowOnFailure(textView.GetBuffer(out buffer));
56+
57+
int line;
58+
int index;
59+
ErrorHandler.ThrowOnFailure(buffer.GetLastLineIndex(out line, out index));
60+
61+
string text;
62+
ErrorHandler.ThrowOnFailure(buffer.GetLineText(0, 0, line, index, out text));
63+
return text;
64+
}
65+
66+
IVsTextView OpenDocument(string fullPath)
67+
{
68+
var logicalView = VSConstants.LOGVIEWID.TextView_guid;
69+
IVsUIHierarchy hierarchy;
70+
uint itemID;
71+
IVsWindowFrame windowFrame;
72+
IVsTextView view;
73+
VsShellUtilities.OpenDocument(serviceProvider, fullPath, logicalView, out hierarchy, out itemID, out windowFrame, out view);
74+
return view;
75+
}
76+
}
77+
}

src/GitHub.App/packages.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
<package id="LibGit2Sharp" version="0.23.1" targetFramework="net461" />
44
<package id="LibGit2Sharp.NativeBinaries" version="1.0.164" targetFramework="net461" />
55
<package id="Microsoft.VisualStudio.ComponentModelHost" version="14.0.25424" targetFramework="net461" />
6+
<package id="Microsoft.VisualStudio.OLE.Interop" version="7.10.6070" targetFramework="net461" />
67
<package id="Microsoft.VisualStudio.Shell.14.0" version="14.3.25407" targetFramework="net461" />
78
<package id="Microsoft.VisualStudio.Shell.Immutable.10.0" version="10.0.30319" targetFramework="net461" />
9+
<package id="Microsoft.VisualStudio.Shell.Interop" version="7.10.6071" targetFramework="net461" />
810
<package id="Microsoft.VisualStudio.TextManager.Interop" version="7.10.6070" targetFramework="net461" />
11+
<package id="Microsoft.VisualStudio.TextManager.Interop.8.0" version="8.0.50727" targetFramework="net461" />
912
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
1013
<package id="Rx-Core" version="2.2.5-custom" targetFramework="net45" />
1114
<package id="Rx-Interfaces" version="2.2.5-custom" targetFramework="net45" />

src/GitHub.Exports/GitHub.Exports.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
<Compile Include="Services\IEnterpriseProbeTask.cs" />
155155
<Compile Include="Services\IGlobalConnection.cs" />
156156
<Compile Include="Services\ILocalRepositories.cs" />
157+
<Compile Include="Services\INavigationService.cs" />
157158
<Compile Include="Services\IVisualStudioBrowser.cs" />
158159
<Compile Include="Models\UsageData.cs" />
159160
<Compile Include="Services\IUsageService.cs" />
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Microsoft.VisualStudio.TextManager.Interop;
2+
3+
namespace GitHub.Services
4+
{
5+
public interface INavigationService
6+
{
7+
IVsTextView FindActiveView();
8+
IVsTextView NavigateToEquivalentPosition(IVsTextView sourceView, string targetFile);
9+
}
10+
}

src/GitHub.VisualStudio/Views/GitHubPane/PullRequestDetailView.xaml.cs

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public PullRequestDetailView()
6969
[Import]
7070
IUsageTracker UsageTracker { get; set; }
7171

72+
[Import]
73+
INavigationService NavigationService { get; set; }
74+
7275
protected override void OnVisualParentChanged(DependencyObject oldParent)
7376
{
7477
base.OnVisualParentChanged(oldParent);
@@ -123,26 +126,16 @@ async Task DoOpenLiveFile(IPullRequestFileNode file)
123126
{
124127
try
125128
{
126-
var activeView = Utilities.FindActiveView();
129+
var fullPath = ViewModel.GetLocalFilePath(file);
130+
131+
var activeView = NavigationService.FindActiveView();
127132
if (activeView == null)
128133
{
129134
ShowErrorInStatusBar("Couldn't find active view");
130135
return;
131136
}
132137

133-
int line;
134-
int column;
135-
activeView.GetCaretPos(out line, out column);
136-
var text1 = Utilities.GetText(activeView);
137-
138-
var fullPath = ViewModel.GetLocalFilePath(file);
139-
IVsTextView view = Utilities.OpenDocument(fullPath);
140-
var text2 = VsShellUtilities.GetRunningDocumentContents(Services.GitHubServiceProvider, fullPath);
141-
142-
var equivalentLine = Utilities.FindEquivalentLine(text1, text2, line);
143-
144-
view.SetCaretPos(line, column);
145-
view.CenterLines(line, 1);
138+
NavigationService.NavigateToEquivalentPosition(activeView, fullPath);
146139

147140
// TODO: Add metrics for NumberOfPRDetailsOpenLiveFile
148141
await UsageTracker.IncrementCounter(x => x.NumberOfPRDetailsOpenFileInSolution);
@@ -153,47 +146,6 @@ async Task DoOpenLiveFile(IPullRequestFileNode file)
153146
}
154147
}
155148

156-
public static class Utilities
157-
{
158-
public static object FindEquivalentLine(string text1, string text2, int line)
159-
{
160-
return line;
161-
}
162-
163-
internal static string GetText(IVsTextView textView)
164-
{
165-
IVsTextLines buffer;
166-
ErrorHandler.ThrowOnFailure(textView.GetBuffer(out buffer));
167-
168-
int line;
169-
int index;
170-
ErrorHandler.ThrowOnFailure(buffer.GetLastLineIndex(out line, out index));
171-
172-
string text;
173-
ErrorHandler.ThrowOnFailure(buffer.GetLineText(0, 0, line, index, out text));
174-
return text;
175-
}
176-
177-
internal static IVsTextView OpenDocument(string fullPath)
178-
{
179-
var logicalView = VSConstants.LOGVIEWID.TextView_guid;
180-
IVsUIHierarchy hierarchy;
181-
uint itemID;
182-
IVsWindowFrame windowFrame;
183-
IVsTextView view;
184-
VsShellUtilities.OpenDocument(Services.GitHubServiceProvider, fullPath, logicalView, out hierarchy, out itemID, out windowFrame, out view);
185-
return view;
186-
}
187-
188-
internal static IVsTextView FindActiveView()
189-
{
190-
var textManager = Services.GitHubServiceProvider.GetService<SVsTextManager, IVsTextManager2>();
191-
IVsTextView view;
192-
var hresult = textManager.GetActiveView2(1, null, (uint)_VIEWFRAMETYPE.vftCodeWindow, out view);
193-
return hresult == VSConstants.S_OK ? view : null;
194-
}
195-
}
196-
197149
async Task DoDiffFile(IPullRequestFileNode file, bool workingDirectory)
198150
{
199151
try

0 commit comments

Comments
 (0)