From 882a2f236954bf3e7be2152b499f6fdf8471c441 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Fri, 17 Dec 2021 12:50:25 -0800 Subject: [PATCH 1/4] Move component model code to common location for all AbstractPackages --- .../LanguageService/AbstractPackage.cs | 20 +++++++++++++++++++ .../LanguageService/AbstractPackage`2.cs | 12 ----------- src/VisualStudio/Core/Def/RoslynPackage.cs | 17 +++------------- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs index 2e697d6d82423..1e257d9034ff3 100644 --- a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs +++ b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs @@ -4,7 +4,9 @@ #nullable disable +using System; using System.Threading; +using Microsoft.VisualStudio.ComponentModelHost; using Microsoft.VisualStudio.Shell; using Task = System.Threading.Tasks.Task; @@ -12,6 +14,24 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation.LanguageService { internal abstract class AbstractPackage : AsyncPackage { + private IComponentModel _componentModel_doNotAccessDirectly; + + internal IComponentModel ComponentModel + { + get + { + Assumes.Present(_componentModel_doNotAccessDirectly); + return _componentModel_doNotAccessDirectly; + } + } + + protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress progress) + { + await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); + + _componentModel_doNotAccessDirectly = (IComponentModel)await GetServiceAsync(typeof(SComponentModel)).ConfigureAwait(true); + } + protected async Task LoadComponentsInUIContextOnceSolutionFullyLoadedAsync(CancellationToken cancellationToken) { // UIContexts can be "zombied" if UIContexts aren't supported because we're in a command line build or in other scenarios. diff --git a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage`2.cs b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage`2.cs index d238894e8d293..f7546e274dc53 100644 --- a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage`2.cs +++ b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage`2.cs @@ -32,7 +32,6 @@ internal abstract partial class AbstractPackage : Ab private PackageInstallerService _packageInstallerService; private VisualStudioSymbolSearchService _symbolSearchService; - private IComponentModel _componentModel_doNotAccessDirectly; protected AbstractPackage() { @@ -44,11 +43,9 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); - _componentModel_doNotAccessDirectly = (IComponentModel)await GetServiceAsync(typeof(SComponentModel)).ConfigureAwait(true); var shell = (IVsShell7)await GetServiceAsync(typeof(SVsShell)).ConfigureAwait(true); var solution = (IVsSolution)await GetServiceAsync(typeof(SVsSolution)).ConfigureAwait(true); cancellationToken.ThrowIfCancellationRequested(); - Assumes.Present(_componentModel_doNotAccessDirectly); Assumes.Present(shell); Assumes.Present(solution); @@ -105,15 +102,6 @@ protected override async Task LoadComponentsAsync(CancellationToken cancellation _symbolSearchService?.Connect(this.RoslynLanguageName); } - internal IComponentModel ComponentModel - { - get - { - Assumes.Present(_componentModel_doNotAccessDirectly); - return _componentModel_doNotAccessDirectly; - } - } - protected abstract void RegisterMiscellaneousFilesWorkspaceInformation(MiscellaneousFilesWorkspace miscellaneousFilesWorkspace); protected abstract IEnumerable CreateEditorFactories(); diff --git a/src/VisualStudio/Core/Def/RoslynPackage.cs b/src/VisualStudio/Core/Def/RoslynPackage.cs index 5890c9ad8e5d6..5cfd84c835325 100644 --- a/src/VisualStudio/Core/Def/RoslynPackage.cs +++ b/src/VisualStudio/Core/Def/RoslynPackage.cs @@ -64,7 +64,6 @@ internal sealed class RoslynPackage : AbstractPackage private static RoslynPackage? _lazyInstance; private VisualStudioWorkspace? _workspace; - private IComponentModel? _componentModel; private RuleSetEventHandler? _ruleSetEventHandler; private ColorSchemeApplier? _colorSchemeApplier; private IDisposable? _solutionEventMonitor; @@ -151,14 +150,12 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); - _componentModel = (IComponentModel)await GetServiceAsync(typeof(SComponentModel)).ConfigureAwait(true); cancellationToken.ThrowIfCancellationRequested(); - Assumes.Present(_componentModel); // Ensure the options persisters are loaded since we have to fetch options from the shell - LoadOptionPersistersAsync(_componentModel, cancellationToken).Forget(); + LoadOptionPersistersAsync(this.ComponentModel, cancellationToken).Forget(); - _workspace = _componentModel.GetService(); + _workspace = this.ComponentModel.GetService(); // Fetch the session synchronously on the UI thread; if this doesn't happen before we try using this on // the background thread then we will experience hangs like we see in this bug: @@ -179,7 +176,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke TrackBulkFileOperations(); - var settingsEditorFactory = _componentModel.GetService(); + var settingsEditorFactory = this.ComponentModel.GetService(); RegisterEditorFactory(settingsEditorFactory); } @@ -304,14 +301,6 @@ private async Task LoadCallstackExplorerMenusAsync(CancellationToken cancellatio StackTraceExplorerCommandHandler.Initialize(menuCommandService, this); } - internal IComponentModel ComponentModel - { - get - { - return _componentModel ?? throw new InvalidOperationException($"Cannot use {nameof(RoslynPackage)}.{nameof(ComponentModel)} prior to initialization."); - } - } - protected override void Dispose(bool disposing) { DisposeVisualStudioServices(); From e17723589da390b39f682d72bbbb073cc62b77f5 Mon Sep 17 00:00:00 2001 From: CyrusNajmabadi Date: Fri, 17 Dec 2021 19:10:05 -0500 Subject: [PATCH 2/4] Update src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs Co-authored-by: Sam Harwell --- .../Core/Def/Implementation/LanguageService/AbstractPackage.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs index 1e257d9034ff3..5e49c70e5a5e9 100644 --- a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs +++ b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs @@ -30,6 +30,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); _componentModel_doNotAccessDirectly = (IComponentModel)await GetServiceAsync(typeof(SComponentModel)).ConfigureAwait(true); + Assumes.Present(_componentModel_doNotAccessDirectly); } protected async Task LoadComponentsInUIContextOnceSolutionFullyLoadedAsync(CancellationToken cancellationToken) From e7d78b23f24ca1c4f3f2f19311ba5eff9711c201 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Fri, 17 Dec 2021 21:23:36 -0800 Subject: [PATCH 3/4] call to base --- .../Core/Def/Implementation/LanguageService/AbstractPackage.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs index 1e257d9034ff3..6eef91308e7ee 100644 --- a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs +++ b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs @@ -27,6 +27,7 @@ internal IComponentModel ComponentModel protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress progress) { + await base.InitializeAsync(cancellationToken, progress).ConfigureAwait(true); await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); _componentModel_doNotAccessDirectly = (IComponentModel)await GetServiceAsync(typeof(SComponentModel)).ConfigureAwait(true); From 31d442ce427c197b3117f8f5e41cf6f0ae525423 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Fri, 17 Dec 2021 21:24:12 -0800 Subject: [PATCH 4/4] NRT' --- .../Def/Implementation/LanguageService/AbstractPackage.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs index 35bae587be256..654e8721e475e 100644 --- a/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs +++ b/src/VisualStudio/Core/Def/Implementation/LanguageService/AbstractPackage.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System; using System.Threading; using Microsoft.VisualStudio.ComponentModelHost; @@ -14,7 +12,7 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation.LanguageService { internal abstract class AbstractPackage : AsyncPackage { - private IComponentModel _componentModel_doNotAccessDirectly; + private IComponentModel? _componentModel_doNotAccessDirectly; internal IComponentModel ComponentModel {