Skip to content

Commit

Permalink
Merge pull request #58397 from CyrusNajmabadi/shareComponentModel
Browse files Browse the repository at this point in the history
Move component model code to common location for all AbstractPackages
  • Loading branch information
CyrusNajmabadi authored Dec 18, 2021
2 parents 801ab48 + 31d442c commit 785ce8c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,36 @@
// 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;
using Microsoft.VisualStudio.Shell;
using Task = System.Threading.Tasks.Task;

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<ServiceProgressData> progress)
{
await base.InitializeAsync(cancellationToken, progress).ConfigureAwait(true);
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

_componentModel_doNotAccessDirectly = (IComponentModel)await GetServiceAsync(typeof(SComponentModel)).ConfigureAwait(true);
Assumes.Present(_componentModel_doNotAccessDirectly);
}

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ internal abstract partial class AbstractPackage<TPackage, TLanguageService> : Ab

private PackageInstallerService _packageInstallerService;
private VisualStudioSymbolSearchService _symbolSearchService;
private IComponentModel _componentModel_doNotAccessDirectly;

protected AbstractPackage()
{
Expand All @@ -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);

Expand Down Expand Up @@ -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<IVsEditorFactory> CreateEditorFactories();
Expand Down
17 changes: 3 additions & 14 deletions src/VisualStudio/Core/Def/RoslynPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<VisualStudioWorkspace>();
_workspace = this.ComponentModel.GetService<VisualStudioWorkspace>();

// 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:
Expand All @@ -179,7 +176,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke

TrackBulkFileOperations();

var settingsEditorFactory = _componentModel.GetService<SettingsEditorFactory>();
var settingsEditorFactory = this.ComponentModel.GetService<SettingsEditorFactory>();
RegisterEditorFactory(settingsEditorFactory);
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 785ce8c

Please sign in to comment.