Skip to content

Commit

Permalink
Merge pull request dotnet#62909 from CyrusNajmabadi/removeSolutionSer…
Browse files Browse the repository at this point in the history
…vice

Remove SolutionServices in favor of HostWorkspaceServices
  • Loading branch information
CyrusNajmabadi authored Jul 25, 2022
2 parents 3b9d8be + 7f9b7f5 commit b69612d
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
Expand Down Expand Up @@ -276,8 +277,8 @@ public async Task<string> GetFullyQualifiedNameAsync(Solution solution, Document
CancellationToken cancellationToken)
{
var document = solution.GetDocument(syntaxNode.GetLocation().SourceTree);

using (solution.Services.CacheService?.EnableCaching(document.Project.Id))
var cacheService = solution.Services.GetService<IProjectCacheHostService>();
using (cacheService?.EnableCaching(document.Project.Id))
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var declaredSymbol = semanticModel.GetDeclaredSymbol(syntaxNode, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ await AddClassificationsInCurrentProcessAsync(

private static bool IsFullyLoaded(Document document, CancellationToken cancellationToken)
{
var workspaceStatusService = document.Project.Solution.Workspace.Services.GetRequiredService<IWorkspaceStatusService>();
var workspaceStatusService = document.Project.Solution.Services.GetRequiredService<IWorkspaceStatusService>();

// Importantly, we do not await/wait on the fullyLoadedStateTask. We do not want to ever be waiting on work
// that may end up touching the UI thread (As we can deadlock if GetTagsSynchronous waits on us). Instead,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -118,7 +119,8 @@ public async Task<MutableConflictResolution> ResolveConflictsAsync()
foreach (var documentsByProject in documentsGroupedByTopologicallySortedProjectId)
{
var documentIdsThatGetsAnnotatedAndRenamed = new HashSet<DocumentId>(documentsByProject);
using (baseSolution.Services.CacheService?.EnableCaching(documentsByProject.Key))
var cacheService = baseSolution.Services.GetService<IProjectCacheHostService>();
using (cacheService?.EnableCaching(documentsByProject.Key))
{
// Rename is going to be in 5 phases.
// 1st phase - Does a simple token replacement
Expand Down Expand Up @@ -238,7 +240,7 @@ await conflictResolution.CurrentSolution.GetRequiredDocument(_documentIdOfRename
{
var definitionLocations = _renameLocationSet.Symbol.Locations;
var definitionDocuments = definitionLocations
.Select(l => conflictResolution.OldSolution.GetRequiredDocument(l.SourceTree))
.Select(l => conflictResolution.OldSolution.GetRequiredDocument(l.SourceTree!))
.Distinct();

if (definitionDocuments.Count() == 1 && _replacementTextValid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class AdditionalDocumentState : TextDocumentState
private readonly AdditionalText _additionalText;

private AdditionalDocumentState(
SolutionServices solutionServices,
HostWorkspaceServices solutionServices,
IDocumentServiceProvider documentServiceProvider,
DocumentInfo.DocumentAttributes attributes,
SourceText? sourceText,
Expand All @@ -26,7 +26,7 @@ private AdditionalDocumentState(

public AdditionalDocumentState(
DocumentInfo documentInfo,
SolutionServices solutionServices)
HostWorkspaceServices solutionServices)
: base(documentInfo, solutionServices)
{
_additionalText = new AdditionalTextWithState(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal sealed class AnalyzerConfigDocumentState : TextDocumentState
private readonly ValueSource<AnalyzerConfig> _analyzerConfigValueSource;

private AnalyzerConfigDocumentState(
SolutionServices solutionServices,
HostWorkspaceServices solutionServices,
IDocumentServiceProvider documentServiceProvider,
DocumentInfo.DocumentAttributes attributes,
SourceText sourceTextOpt,
Expand All @@ -29,7 +29,7 @@ private AnalyzerConfigDocumentState(

public AnalyzerConfigDocumentState(
DocumentInfo documentInfo,
SolutionServices solutionServices)
HostWorkspaceServices solutionServices)
: base(documentInfo, solutionServices)
{
_analyzerConfigValueSource = CreateAnalyzerConfigValueSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal partial class DocumentState : TextDocumentState

protected DocumentState(
HostLanguageServices languageServices,
SolutionServices solutionServices,
HostWorkspaceServices solutionServices,
IDocumentServiceProvider? documentServiceProvider,
DocumentInfo.DocumentAttributes attributes,
ParseOptions? options,
Expand All @@ -54,7 +54,7 @@ public DocumentState(
DocumentInfo info,
ParseOptions? options,
HostLanguageServices languageServices,
SolutionServices services)
HostWorkspaceServices services)
: base(info, services)
{
_languageServices = languageServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal partial class ProjectState
{
private readonly ProjectInfo _projectInfo;
private readonly HostLanguageServices _languageServices;
private readonly SolutionServices _solutionServices;
private readonly HostWorkspaceServices _solutionServices;

/// <summary>
/// The documents in this project. They are sorted by <see cref="DocumentId.Id"/> to provide a stable sort for
Expand Down Expand Up @@ -68,7 +68,7 @@ internal partial class ProjectState
private ProjectState(
ProjectInfo projectInfo,
HostLanguageServices languageServices,
SolutionServices solutionServices,
HostWorkspaceServices solutionServices,
TextDocumentStates<DocumentState> documentStates,
TextDocumentStates<AdditionalDocumentState> additionalDocumentStates,
TextDocumentStates<AnalyzerConfigDocumentState> analyzerConfigDocumentStates,
Expand All @@ -93,7 +93,7 @@ private ProjectState(
_lazyChecksums = new AsyncLazy<ProjectStateChecksums>(ComputeChecksumsAsync, cacheResult: true);
}

public ProjectState(ProjectInfo projectInfo, HostLanguageServices languageServices, SolutionServices solutionServices)
public ProjectState(ProjectInfo projectInfo, HostLanguageServices languageServices, HostWorkspaceServices solutionServices)
{
Contract.ThrowIfNull(projectInfo);
Contract.ThrowIfNull(languageServices);
Expand Down
5 changes: 3 additions & 2 deletions src/Workspaces/Core/Portable/Workspace/Solution/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
Expand All @@ -38,15 +39,15 @@ private Solution(SolutionState state)
}

internal Solution(Workspace workspace, SolutionInfo.SolutionAttributes solutionAttributes, SolutionOptionSet options, IReadOnlyList<AnalyzerReference> analyzerReferences)
: this(new SolutionState(workspace.PrimaryBranchId, new SolutionServices(workspace), solutionAttributes, options, analyzerReferences))
: this(new SolutionState(workspace.PrimaryBranchId, workspace.Services, solutionAttributes, options, analyzerReferences))
{
}

internal SolutionState State => _state;

internal int WorkspaceVersion => _state.WorkspaceVersion;

internal SolutionServices Services => _state.Services;
internal HostWorkspaceServices Services => _state.Services;

internal BranchId BranchId => _state.BranchId;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ public CompilationTracker(ProjectState project)
private CompilationTrackerState ReadState()
=> Volatile.Read(ref _stateDoNotAccessDirectly);

private void WriteState(CompilationTrackerState state, SolutionServices solutionServices)
private void WriteState(CompilationTrackerState state, HostWorkspaceServices solutionServices)
{
if (solutionServices.SupportsCachingRecoverableObjects)
var cacheService = solutionServices.GetService<IProjectCacheHostService>();
if (cacheService != null)
{
// Allow the cache service to create a strong reference to the compilation. We'll get the "furthest along" compilation we have
// and hold onto that.
var compilationToCache = state.FinalCompilationWithGeneratedDocuments ?? state.CompilationWithoutGeneratedDocuments;
solutionServices.CacheService.CacheObjectIfCachingEnabledForKey(ProjectState.Id, state, compilationToCache);
cacheService.CacheObjectIfCachingEnabledForKey(ProjectState.Id, state, compilationToCache);
}

Volatile.Write(ref _stateDoNotAccessDirectly, state);
Expand Down Expand Up @@ -115,7 +116,7 @@ SymbolKind.NetModule or
/// compilation state as the now 'old' state
/// </summary>
public ICompilationTracker Fork(
SolutionServices solutionServices,
HostWorkspaceServices solutionServices,
ProjectState newProject,
CompilationAndGeneratorDriverTranslationAction? translate = null,
CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -395,7 +396,7 @@ private async Task<Compilation> GetCompilationSlowAsync(SolutionState solution,
return compilationInfo.Compilation;
}

private async Task<Compilation> GetOrBuildDeclarationCompilationAsync(SolutionServices solutionServices, CancellationToken cancellationToken)
private async Task<Compilation> GetOrBuildDeclarationCompilationAsync(HostWorkspaceServices solutionServices, CancellationToken cancellationToken)
{
try
{
Expand Down Expand Up @@ -562,7 +563,7 @@ private async Task<CompilationInfo> BuildCompilationInfoFromScratchAsync(
"https://github.com/dotnet/roslyn/issues/23582",
Constraint = "Avoid calling " + nameof(Compilation.AddSyntaxTrees) + " in a loop due to allocation overhead.")]
private async Task<Compilation> BuildDeclarationCompilationFromScratchAsync(
SolutionServices solutionServices,
HostWorkspaceServices solutionServices,
CompilationTrackerGeneratorInfo generatorInfo,
CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -627,7 +628,7 @@ private async Task<CompilationInfo> BuildFinalStateFromInProgressStateAsync(
}

private async Task<(Compilation compilationWithoutGenerators, Compilation? compilationWithGenerators, GeneratorDriver? generatorDriver)> BuildDeclarationCompilationFromInProgressAsync(
SolutionServices solutionServices, InProgressState state, Compilation compilationWithoutGenerators, CancellationToken cancellationToken)
HostWorkspaceServices solutionServices, InProgressState state, Compilation compilationWithoutGenerators, CancellationToken cancellationToken)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis
Expand Down Expand Up @@ -57,7 +58,7 @@ public bool ContainsAssemblyOrModuleOrDynamic(ISymbol symbol, bool primary)
}
}

public ICompilationTracker Fork(SolutionServices solutionServices, ProjectState newProject, CompilationAndGeneratorDriverTranslationAction? translate = null, CancellationToken cancellationToken = default)
public ICompilationTracker Fork(HostWorkspaceServices solutionServices, ProjectState newProject, CompilationAndGeneratorDriverTranslationAction? translate = null, CancellationToken cancellationToken = default)
{
// TODO: This only needs to be implemented if a feature that operates from a source generated file then makes
// further mutations to that project, which isn't needed for now. This will be need to be fixed up when we complete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;

namespace Microsoft.CodeAnalysis
{
Expand Down Expand Up @@ -33,7 +34,7 @@ private interface ICompilationTracker
/// any of the references of the <see cref="Compilation.References"/>.
/// </remarks>
bool ContainsAssemblyOrModuleOrDynamic(ISymbol symbol, bool primary);
ICompilationTracker Fork(SolutionServices solutionServices, ProjectState newProject, CompilationAndGeneratorDriverTranslationAction? translate = null, CancellationToken cancellationToken = default);
ICompilationTracker Fork(HostWorkspaceServices solutionServices, ProjectState newProject, CompilationAndGeneratorDriverTranslationAction? translate = null, CancellationToken cancellationToken = default);
ICompilationTracker FreezePartialStateWithTree(SolutionState solution, DocumentState docState, SyntaxTree tree, CancellationToken cancellationToken);
Task<Compilation> GetCompilationAsync(SolutionState solution, CancellationToken cancellationToken);

Expand Down
Loading

0 comments on commit b69612d

Please sign in to comment.