Skip to content

Commit f159f3e

Browse files
committed
fix for vs2022 bug and update use of vs sdk
1 parent 9967ac4 commit f159f3e

File tree

4 files changed

+92
-23
lines changed

4 files changed

+92
-23
lines changed

SharedProject/Core/SourceFileOpener.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.ComponentModel.Composition;
33
using System.Linq;
44
using EnvDTE;
5+
using EnvDTE80;
56
using FineCodeCoverage.Engine.Cobertura;
67
using Microsoft;
78
using Microsoft.VisualStudio.Shell;
@@ -15,6 +16,7 @@ internal class SourceFileOpener : ISourceFileOpener
1516
private readonly IMessageBox messageBox;
1617
private readonly ILogger logger;
1718
private readonly IServiceProvider serviceProvider;
19+
private readonly DTE2 dte;
1820

1921
[ImportingConstructor]
2022
public SourceFileOpener(
@@ -27,6 +29,9 @@ public SourceFileOpener(
2729
this.messageBox = messageBox;
2830
this.logger = logger;
2931
this.serviceProvider = serviceProvider;
32+
ThreadHelper.ThrowIfNotOnUIThread();
33+
dte = (DTE2)serviceProvider.GetService(typeof(DTE));
34+
Assumes.Present(dte);
3035
}
3136
public async System.Threading.Tasks.Task OpenFileAsync(string assemblyName, string qualifiedClassName, int file, int line)
3237
{
@@ -43,17 +48,15 @@ public async System.Threading.Tasks.Task OpenFileAsync(string assemblyName, stri
4348
}
4449

4550
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
46-
var Dte = (DTE)serviceProvider.GetService(typeof(DTE));
47-
Assumes.Present(Dte);
48-
Dte.MainWindow.Activate();
51+
dte.MainWindow.Activate();
4952

5053
foreach (var sourceFile in sourceFiles)
5154
{
52-
Dte.ItemOperations.OpenFile(sourceFile, Constants.vsViewKindCode);
55+
dte.ItemOperations.OpenFile(sourceFile, Constants.vsViewKindCode);
5356

5457
if (line != 0)
5558
{
56-
((TextSelection)Dte.ActiveDocument.Selection).GotoLine(line, false);
59+
((TextSelection)dte.ActiveDocument.Selection).GotoLine(line, false);
5760
}
5861
}
5962

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
using EnvDTE;
2-
using Microsoft;
1+
using Microsoft;
2+
using Microsoft.VisualStudio;
33
using Microsoft.VisualStudio.Shell;
4+
using Microsoft.VisualStudio.Shell.Interop;
45
using System;
56
using System.ComponentModel.Composition;
67

78
namespace FineCodeCoverage.Core.Utilities
89
{
910
[Export(typeof(ISolutionEvents))]
10-
public class SolutionEvents : ISolutionEvents
11+
public class SolutionEvents : ISolutionEvents, IVsSolutionEvents
1112
{
12-
private Events Events;
13-
private EnvDTE.SolutionEvents dteSolutionEvents;
1413
public event EventHandler AfterClosing;
1514

1615
[ImportingConstructor]
@@ -20,11 +19,60 @@ IServiceProvider serviceProvider
2019
)
2120
{
2221
ThreadHelper.ThrowIfNotOnUIThread();
23-
var Dte = (DTE)serviceProvider.GetService(typeof(DTE));
24-
Assumes.Present(Dte);
25-
Events = Dte.Events;
26-
dteSolutionEvents = Events.SolutionEvents;
27-
dteSolutionEvents.AfterClosing += () => AfterClosing?.Invoke(this, EventArgs.Empty);
22+
var vsSolution = (IVsSolution)serviceProvider.GetService(typeof(SVsSolution));
23+
Assumes.Present(vsSolution);
24+
vsSolution.AdviseSolutionEvents(this, out uint _);
25+
}
26+
27+
public int OnAfterOpenProject(IVsHierarchy pHierarchy, int fAdded)
28+
{
29+
return VSConstants.S_OK;
30+
}
31+
32+
public int OnQueryCloseProject(IVsHierarchy pHierarchy, int fRemoving, ref int pfCancel)
33+
{
34+
return VSConstants.S_OK;
35+
}
36+
37+
public int OnBeforeCloseProject(IVsHierarchy pHierarchy, int fRemoved)
38+
{
39+
return VSConstants.S_OK;
40+
}
41+
42+
public int OnAfterLoadProject(IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy)
43+
{
44+
return VSConstants.S_OK;
45+
}
46+
47+
public int OnQueryUnloadProject(IVsHierarchy pRealHierarchy, ref int pfCancel)
48+
{
49+
return VSConstants.S_OK;
50+
}
51+
52+
public int OnBeforeUnloadProject(IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy)
53+
{
54+
return VSConstants.S_OK;
55+
}
56+
57+
public int OnAfterOpenSolution(object pUnkReserved, int fNewSolution)
58+
{
59+
return VSConstants.S_OK;
60+
}
61+
62+
public int OnQueryCloseSolution(object pUnkReserved, ref int pfCancel)
63+
{
64+
return VSConstants.S_OK;
65+
}
66+
67+
public int OnBeforeCloseSolution(object pUnkReserved)
68+
{
69+
return VSConstants.S_OK;
70+
}
71+
72+
public int OnAfterCloseSolution(object pUnkReserved)
73+
{
74+
AfterClosing?.Invoke(this, new EventArgs());
75+
return VSConstants.S_OK;
2876
}
2977
}
3078
}

SharedProject/Output/OutputToolWindow.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ internal class OutputToolWindow : ToolWindowPane
2828
/// </summary>
2929
public OutputToolWindow(OutputToolWindowContext context) : base(null)
3030
{
31+
Initialize(context);
32+
}
33+
34+
public OutputToolWindow()
35+
{
36+
Initialize(OutputToolWindowPackage.GetOutputToolWindowContext());
37+
}
38+
39+
private void Initialize(OutputToolWindowContext context)
40+
{
3141
//to see if OutputToolWindow can be internal ( and thus IScriptManager )
3242
Caption = Vsix.Name;
3343
context.ScriptManager.FocusCallback = () =>
@@ -46,7 +56,7 @@ public OutputToolWindow(OutputToolWindowContext context) : base(null)
4656
try
4757
{
4858
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
49-
Content = new OutputToolWindowControl(context.ScriptManager,context.FccEngine);
59+
Content = new OutputToolWindowControl(context.ScriptManager, context.FccEngine);
5060
}
5161
finally
5262
{

SharedProject/Output/OutputToolWindowPackage.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace FineCodeCoverage.Output
4040
[ProvideToolWindow(typeof(OutputToolWindow), Style = VsDockStyle.Tabbed, DockedHeight = 300, Window = EnvDTE.Constants.vsWindowKindOutput)]
4141
public sealed class OutputToolWindowPackage : AsyncPackage
4242
{
43-
private Microsoft.VisualStudio.ComponentModelHost.IComponentModel componentModel;
43+
private static Microsoft.VisualStudio.ComponentModelHost.IComponentModel componentModel;
4444
/// <summary>
4545
/// OutputToolWindowPackage GUID string.
4646
/// </summary>
@@ -57,6 +57,19 @@ public OutputToolWindowPackage()
5757
// initialization is the Initialize method.
5858
}
5959

60+
/*
61+
Hack necessary for debugging in 2022 !
62+
https://developercommunity.visualstudio.com/t/vsix-tool-window-vs2022-different-instantiation-wh/1663280
63+
*/
64+
internal static OutputToolWindowContext GetOutputToolWindowContext()
65+
{
66+
return new OutputToolWindowContext
67+
{
68+
FccEngine = componentModel.GetService<IFCCEngine>(),
69+
ScriptManager = componentModel.GetService<ScriptManager>()
70+
};
71+
}
72+
6073
/// <summary>
6174
/// Initialization of the package; this method is called right after the package is sited, so this is the place
6275
/// where you can put all the initialization code that rely on services provided by VisualStudio.
@@ -80,12 +93,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
8093

8194
protected override System.Threading.Tasks.Task<object> InitializeToolWindowAsync(Type toolWindowType, int id, CancellationToken cancellationToken)
8295
{
83-
var context = new OutputToolWindowContext
84-
{
85-
FccEngine = componentModel.GetService<IFCCEngine>(),
86-
ScriptManager = componentModel.GetService<ScriptManager>()
87-
};
88-
return System.Threading.Tasks.Task.FromResult<object>(context);
96+
return System.Threading.Tasks.Task.FromResult<object>(GetOutputToolWindowContext());
8997
}
9098
public override IVsAsyncToolWindowFactory GetAsyncToolWindowFactory(Guid toolWindowType)
9199
{

0 commit comments

Comments
 (0)