Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary type for wrapping analyzer references. #74739

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ internal sealed partial class ProjectSystemProject
private readonly List<ProjectReference> _projectReferencesAddedInBatch = [];
private readonly List<ProjectReference> _projectReferencesRemovedInBatch = [];

private readonly Dictionary<string, ProjectAnalyzerReference> _analyzerPathsToAnalyzers = [];
private readonly List<ProjectAnalyzerReference> _analyzersAddedInBatch = [];
private readonly Dictionary<string, AnalyzerReference> _analyzerPathsToAnalyzers = [];
private readonly List<AnalyzerReference> _analyzersAddedInBatch = [];

/// <summary>
/// The list of <see cref="ProjectAnalyzerReference"/> that will be removed in this batch. They have not yet
/// been disposed, and will be disposed once the batch is applied.
/// The list of <see cref="AnalyzerReference"/>s that will be removed in this batch.
/// </summary>
private readonly List<ProjectAnalyzerReference> _analyzersRemovedInBatch = [];
private readonly List<AnalyzerReference> _analyzersRemovedInBatch = [];

private readonly List<Func<SolutionChangeAccumulator, ProjectUpdateState, ProjectUpdateState>> _projectPropertyModificationsInBatch = [];

Expand Down Expand Up @@ -659,23 +658,15 @@ await _projectSystemProjectFactory.ApplyBatchChangeToWorkspaceMaybeAsync(useAsyn
}

// Analyzer reference adding...
solutionChanges.UpdateSolutionForProjectAction(
Id,
newSolution: solutionChanges.Solution.AddAnalyzerReferences(Id, _analyzersAddedInBatch.Select(a => a.GetReference())));
solutionChanges.UpdateSolutionForProjectAction(Id, solutionChanges.Solution.AddAnalyzerReferences(Id, _analyzersAddedInBatch));

// Analyzer reference removing...
foreach (var analyzerReference in _analyzersRemovedInBatch)
{
solutionChanges.UpdateSolutionForProjectAction(
Id,
newSolution: solutionChanges.Solution.RemoveAnalyzerReference(Id, analyzerReference.GetReference()));
}
solutionChanges.UpdateSolutionForProjectAction(Id, solutionChanges.Solution.RemoveAnalyzerReference(Id, analyzerReference));

// Other property modifications...
foreach (var propertyModification in _projectPropertyModificationsInBatch)
{
projectUpdateState = propertyModification(solutionChanges, projectUpdateState);
}

return projectUpdateState;
},
Expand All @@ -694,15 +685,7 @@ await _projectSystemProjectFactory.ApplyBatchChangeToWorkspaceMaybeAsync(useAsyn
ClearAndZeroCapacity(_projectReferencesAddedInBatch);
ClearAndZeroCapacity(_projectReferencesRemovedInBatch);
ClearAndZeroCapacity(_analyzersAddedInBatch);
if (_analyzersRemovedInBatch.Count > 0)
{
// Dispose of any analyzers that were removed now that we've applied the changes.
foreach (var analyzer in _analyzersRemovedInBatch)
{
analyzer.Dispose();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to Dispose, as Dispose ended up doing nothing. Will demosntrate later.

}
ClearAndZeroCapacity(_analyzersRemovedInBatch);
}
ClearAndZeroCapacity(_analyzersRemovedInBatch);

ClearAndZeroCapacity(_projectPropertyModificationsInBatch);

Expand Down Expand Up @@ -957,22 +940,17 @@ public void AddAnalyzerReference(string fullPath)
else
{
// Nope, we actually need to make a new one.
var visualStudioAnalyzer = new ProjectAnalyzerReference(
mappedFullPath,
_analyzerAssemblyLoader,
_hostInfo.DiagnosticSource,
Id,
Language);
var analyzerReference = new AnalyzerFileReference(mappedFullPath, _analyzerAssemblyLoader);

_analyzerPathsToAnalyzers.Add(mappedFullPath, visualStudioAnalyzer);
_analyzerPathsToAnalyzers.Add(mappedFullPath, analyzerReference);

if (_activeBatchScopes > 0)
{
_analyzersAddedInBatch.Add(visualStudioAnalyzer);
_analyzersAddedInBatch.Add(analyzerReference);
}
else
{
_projectSystemProjectFactory.ApplyChangeToWorkspace(w => w.OnAnalyzerReferenceAdded(Id, visualStudioAnalyzer.GetReference()));
_projectSystemProjectFactory.ApplyChangeToWorkspace(w => w.OnAnalyzerReferenceAdded(Id, analyzerReference));
}
}
}
Expand Down Expand Up @@ -1001,29 +979,20 @@ public void RemoveAnalyzerReference(string fullPath)

foreach (var mappedFullPath in mappedPaths)
{
var visualStudioAnalyzer = _analyzerPathsToAnalyzers[mappedFullPath];
var analyzerReference = _analyzerPathsToAnalyzers[mappedFullPath];

_analyzerPathsToAnalyzers.Remove(mappedFullPath);

if (_activeBatchScopes > 0)
{
// This analyzer may be one we've just added in the same batch; in that case, just don't add
// it in the first place.
if (_analyzersAddedInBatch.Remove(visualStudioAnalyzer))
{
// Nothing is holding onto this analyzer now, so get rid of it
visualStudioAnalyzer.Dispose();
}
else
{
_analyzersRemovedInBatch.Add(visualStudioAnalyzer);
}
// This analyzer may be one we've just added in the same batch; in that case, just don't add it in
// the first place.
if (!_analyzersAddedInBatch.Remove(analyzerReference))
_analyzersRemovedInBatch.Add(analyzerReference);
}
else
{
_projectSystemProjectFactory.ApplyChangeToWorkspace(w => w.OnAnalyzerReferenceRemoved(Id, visualStudioAnalyzer.GetReference()));

visualStudioAnalyzer.Dispose();
_projectSystemProjectFactory.ApplyChangeToWorkspace(w => w.OnAnalyzerReferenceRemoved(Id, analyzerReference));
}
}
}
Expand Down Expand Up @@ -1364,16 +1333,8 @@ public void RemoveFromWorkspace()

Contract.ThrowIfNull(remainingMetadataReferences);

foreach (PortableExecutableReference reference in remainingMetadataReferences)
{
foreach (var reference in remainingMetadataReferences.OfType<PortableExecutableReference>())
_projectSystemProjectFactory.FileWatchedReferenceFactory.StopWatchingReference(reference);
}

// Dispose of any analyzers that might still be around to remove their load diagnostics
foreach (var visualStudioAnalyzer in _analyzerPathsToAnalyzers.Values.Concat(_analyzersRemovedInBatch))
{
visualStudioAnalyzer.Dispose();
}
}

public void ReorderSourceFiles(ImmutableArray<string> filePaths)
Expand Down

This file was deleted.

Loading