diff --git a/SharedProject/Core/SourceFileOpener.cs b/SharedProject/Core/SourceFileOpener.cs index 90fa787f..b1187bef 100644 --- a/SharedProject/Core/SourceFileOpener.cs +++ b/SharedProject/Core/SourceFileOpener.cs @@ -2,6 +2,7 @@ using System.ComponentModel.Composition; using System.Linq; using EnvDTE; +using EnvDTE80; using FineCodeCoverage.Engine.Cobertura; using Microsoft; using Microsoft.VisualStudio.Shell; @@ -15,6 +16,7 @@ internal class SourceFileOpener : ISourceFileOpener private readonly IMessageBox messageBox; private readonly ILogger logger; private readonly IServiceProvider serviceProvider; + private readonly DTE2 dte; [ImportingConstructor] public SourceFileOpener( @@ -27,6 +29,9 @@ public SourceFileOpener( this.messageBox = messageBox; this.logger = logger; this.serviceProvider = serviceProvider; + ThreadHelper.ThrowIfNotOnUIThread(); + dte = (DTE2)serviceProvider.GetService(typeof(DTE)); + Assumes.Present(dte); } public async System.Threading.Tasks.Task OpenFileAsync(string assemblyName, string qualifiedClassName, int file, int line) { @@ -43,17 +48,15 @@ public async System.Threading.Tasks.Task OpenFileAsync(string assemblyName, stri } await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); - var Dte = (DTE)serviceProvider.GetService(typeof(DTE)); - Assumes.Present(Dte); - Dte.MainWindow.Activate(); + dte.MainWindow.Activate(); foreach (var sourceFile in sourceFiles) { - Dte.ItemOperations.OpenFile(sourceFile, Constants.vsViewKindCode); + dte.ItemOperations.OpenFile(sourceFile, Constants.vsViewKindCode); if (line != 0) { - ((TextSelection)Dte.ActiveDocument.Selection).GotoLine(line, false); + ((TextSelection)dte.ActiveDocument.Selection).GotoLine(line, false); } } diff --git a/SharedProject/Core/Utilities/SolutionEvents.cs b/SharedProject/Core/Utilities/SolutionEvents.cs index 00f489f0..83bb2272 100644 --- a/SharedProject/Core/Utilities/SolutionEvents.cs +++ b/SharedProject/Core/Utilities/SolutionEvents.cs @@ -1,16 +1,15 @@ -using EnvDTE; -using Microsoft; +using Microsoft; +using Microsoft.VisualStudio; using Microsoft.VisualStudio.Shell; +using Microsoft.VisualStudio.Shell.Interop; using System; using System.ComponentModel.Composition; namespace FineCodeCoverage.Core.Utilities { [Export(typeof(ISolutionEvents))] - public class SolutionEvents : ISolutionEvents + public class SolutionEvents : ISolutionEvents, IVsSolutionEvents { - private Events Events; - private EnvDTE.SolutionEvents dteSolutionEvents; public event EventHandler AfterClosing; [ImportingConstructor] @@ -20,11 +19,60 @@ IServiceProvider serviceProvider ) { ThreadHelper.ThrowIfNotOnUIThread(); - var Dte = (DTE)serviceProvider.GetService(typeof(DTE)); - Assumes.Present(Dte); - Events = Dte.Events; - dteSolutionEvents = Events.SolutionEvents; - dteSolutionEvents.AfterClosing += () => AfterClosing?.Invoke(this, EventArgs.Empty); + var vsSolution = (IVsSolution)serviceProvider.GetService(typeof(SVsSolution)); + Assumes.Present(vsSolution); + vsSolution.AdviseSolutionEvents(this, out uint _); + } + + public int OnAfterOpenProject(IVsHierarchy pHierarchy, int fAdded) + { + return VSConstants.S_OK; + } + + public int OnQueryCloseProject(IVsHierarchy pHierarchy, int fRemoving, ref int pfCancel) + { + return VSConstants.S_OK; + } + + public int OnBeforeCloseProject(IVsHierarchy pHierarchy, int fRemoved) + { + return VSConstants.S_OK; + } + + public int OnAfterLoadProject(IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy) + { + return VSConstants.S_OK; + } + + public int OnQueryUnloadProject(IVsHierarchy pRealHierarchy, ref int pfCancel) + { + return VSConstants.S_OK; + } + + public int OnBeforeUnloadProject(IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy) + { + return VSConstants.S_OK; + } + + public int OnAfterOpenSolution(object pUnkReserved, int fNewSolution) + { + return VSConstants.S_OK; + } + + public int OnQueryCloseSolution(object pUnkReserved, ref int pfCancel) + { + return VSConstants.S_OK; + } + + public int OnBeforeCloseSolution(object pUnkReserved) + { + return VSConstants.S_OK; + } + + public int OnAfterCloseSolution(object pUnkReserved) + { + AfterClosing?.Invoke(this, new EventArgs()); + return VSConstants.S_OK; } } } diff --git a/SharedProject/Output/OutputToolWindow.cs b/SharedProject/Output/OutputToolWindow.cs index 0ee0437a..2a1efb63 100644 --- a/SharedProject/Output/OutputToolWindow.cs +++ b/SharedProject/Output/OutputToolWindow.cs @@ -28,6 +28,16 @@ internal class OutputToolWindow : ToolWindowPane /// public OutputToolWindow(OutputToolWindowContext context) : base(null) { + Initialize(context); + } + + public OutputToolWindow() + { + Initialize(OutputToolWindowPackage.GetOutputToolWindowContext()); + } + + private void Initialize(OutputToolWindowContext context) + { //to see if OutputToolWindow can be internal ( and thus IScriptManager ) Caption = Vsix.Name; context.ScriptManager.FocusCallback = () => @@ -46,7 +56,7 @@ public OutputToolWindow(OutputToolWindowContext context) : base(null) try { AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; - Content = new OutputToolWindowControl(context.ScriptManager,context.FccEngine); + Content = new OutputToolWindowControl(context.ScriptManager, context.FccEngine); } finally { diff --git a/SharedProject/Output/OutputToolWindowPackage.cs b/SharedProject/Output/OutputToolWindowPackage.cs index 31cab5c0..6ee5ddea 100644 --- a/SharedProject/Output/OutputToolWindowPackage.cs +++ b/SharedProject/Output/OutputToolWindowPackage.cs @@ -40,7 +40,7 @@ namespace FineCodeCoverage.Output [ProvideToolWindow(typeof(OutputToolWindow), Style = VsDockStyle.Tabbed, DockedHeight = 300, Window = EnvDTE.Constants.vsWindowKindOutput)] public sealed class OutputToolWindowPackage : AsyncPackage { - private Microsoft.VisualStudio.ComponentModelHost.IComponentModel componentModel; + private static Microsoft.VisualStudio.ComponentModelHost.IComponentModel componentModel; /// /// OutputToolWindowPackage GUID string. /// @@ -57,6 +57,19 @@ public OutputToolWindowPackage() // initialization is the Initialize method. } + /* + Hack necessary for debugging in 2022 ! + https://developercommunity.visualstudio.com/t/vsix-tool-window-vs2022-different-instantiation-wh/1663280 + */ + internal static OutputToolWindowContext GetOutputToolWindowContext() + { + return new OutputToolWindowContext + { + FccEngine = componentModel.GetService(), + ScriptManager = componentModel.GetService() + }; + } + /// /// Initialization of the package; this method is called right after the package is sited, so this is the place /// 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 protected override System.Threading.Tasks.Task InitializeToolWindowAsync(Type toolWindowType, int id, CancellationToken cancellationToken) { - var context = new OutputToolWindowContext - { - FccEngine = componentModel.GetService(), - ScriptManager = componentModel.GetService() - }; - return System.Threading.Tasks.Task.FromResult(context); + return System.Threading.Tasks.Task.FromResult(GetOutputToolWindowContext()); } public override IVsAsyncToolWindowFactory GetAsyncToolWindowFactory(Guid toolWindowType) {