Skip to content

Commit 92ec31e

Browse files
Avoid checking the CanonicalName each time for the analyzer
If the user expands a node in a CPS project wanting to look at the diagnostics under an analyzer, but we haven't been told about that analyzer let, we stick a WorkspaceChanged handler there to find it once it comes back. We were hopping to the UI thread to see if the CanonicalName of the item could have changed, but that's not really going to happen ever for these items, so we can just grab it once during creation and be done with it.
1 parent 291e1e7 commit 92ec31e

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/VisualStudio/Core/Impl/SolutionExplorer/DiagnosticItem/CpsDiagnosticItemSource.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal sealed partial class CpsDiagnosticItemSource : BaseDiagnosticAndGenerat
1919
{
2020
private readonly IVsHierarchyItem _item;
2121
private readonly string _projectDirectoryPath;
22+
private readonly string? _analyzerFilePath;
2223

2324
private WorkspaceEventRegistration? _workspaceChangedDisposer;
2425

@@ -37,6 +38,8 @@ public CpsDiagnosticItemSource(
3738
_item = item;
3839
_projectDirectoryPath = Path.GetDirectoryName(projectPath);
3940

41+
_analyzerFilePath = CpsUtilities.ExtractAnalyzerFilePath(_projectDirectoryPath, _item.CanonicalName);
42+
4043
this.AnalyzerReference = TryGetAnalyzerReference(Workspace.CurrentSolution);
4144
if (this.AnalyzerReference == null)
4245
{
@@ -47,9 +50,7 @@ public CpsDiagnosticItemSource(
4750
// then connect to it.
4851
if (workspace.CurrentSolution.ContainsProject(projectId))
4952
{
50-
// Main thread dependency as OnWorkspaceChangedLookForAnalyzer accesses the IVsHierarchy
51-
// and fires the PropertyChanged event
52-
_workspaceChangedDisposer = Workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChangedLookForAnalyzer, WorkspaceEventOptions.RequiresMainThreadOptions);
53+
_workspaceChangedDisposer = Workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChangedLookForAnalyzer);
5354
item.PropertyChanged += IVsHierarchyItem_PropertyChanged;
5455

5556
// Now that we've subscribed, check once more in case we missed the event
@@ -118,14 +119,11 @@ private void OnWorkspaceChangedLookForAnalyzer(WorkspaceChangeEventArgs e)
118119
return null;
119120
}
120121

121-
var canonicalName = _item.CanonicalName;
122-
var analyzerFilePath = CpsUtilities.ExtractAnalyzerFilePath(_projectDirectoryPath, canonicalName);
123-
124-
if (string.IsNullOrEmpty(analyzerFilePath))
122+
if (string.IsNullOrEmpty(_analyzerFilePath))
125123
{
126124
return null;
127125
}
128126

129-
return project.AnalyzerReferences.FirstOrDefault(r => string.Equals(r.FullPath, analyzerFilePath, StringComparison.OrdinalIgnoreCase));
127+
return project.AnalyzerReferences.FirstOrDefault(r => string.Equals(r.FullPath, _analyzerFilePath, StringComparison.OrdinalIgnoreCase));
130128
}
131129
}

0 commit comments

Comments
 (0)