Skip to content

Commit f447f73

Browse files
Group by document to avoid relying on caching within the library
1 parent 50e2e6b commit f447f73

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

ICSharpCode.CodeConverter/CSharp/UsageTypeAnalyzer.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@ public static async Task<bool> IsNeverWritten(this Solution solution, ISymbol sy
1919
public static async Task<bool> ContainsWriteUsagesFor(Solution solution, ISymbol symbol, Location outsideLocation = null)
2020
{
2121
var references = await SymbolFinder.FindReferencesAsync(symbol, solution);
22-
var operationsReferencing = await references.SelectMany(r => r.Locations).SelectAsync(async l => {
23-
if (l.Location.SourceTree == outsideLocation?.SourceTree && l.Location.SourceSpan.OverlapsWith(outsideLocation.SourceSpan)) return null;
24-
var semanticModel = await l.Document.GetSemanticModelAsync();
25-
var syntaxRoot = await l.Document.GetSyntaxRootAsync();
26-
var syntaxNode = syntaxRoot.FindNode(l.Location.SourceSpan);
27-
return semanticModel.GetOperation(syntaxNode);
22+
var operationsReferencing = references.SelectMany(r => r.Locations).GroupBy(l => (Doc: l.Document, Tree: l.Location.SourceTree)).Select(async g => {
23+
var document = g.Key.Doc;
24+
var locations = g.Where(l => l.Location.SourceTree != outsideLocation?.SourceTree || !l.Location.SourceSpan.OverlapsWith(outsideLocation.SourceSpan)).ToArray();
25+
if (locations.Length == 0) return Enumerable.Empty<IOperation>();
26+
27+
var semanticModel = await document.GetSemanticModelAsync();
28+
var syntaxRoot = await document.GetSyntaxRootAsync();
29+
return g.Select(l => syntaxRoot.FindNode(l.Location.SourceSpan))
30+
.Select(syntaxNode => semanticModel.GetOperation(syntaxNode));
2831
});
29-
if (operationsReferencing.Any(IsWriteUsage)) return true;
32+
foreach (var documentUsages in operationsReferencing) {
33+
var usages = await documentUsages;
34+
if (usages.Any(IsWriteUsage)) return true;
35+
}
3036
return false;
3137
}
3238

0 commit comments

Comments
 (0)