Add 'ISymbol.FirstAncestorOrSelf' extension

このコミットが含まれているのは:
Sergio Pedri committed 2024-11-23 16:20:05 -08:00
1 親 150e2540ba
コミット a9a5b164d3
1ファイル変更
+21
+21
ファイルの表示
@@ -1,3 +1,4 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.CodeAnalysis;
@@ -155,4 +156,24 @@ internal static class ISymbolExtensions
return candidateNode is not null;
}
/// <summary>
/// Gets the first symbol of a specific type among the ancestors of a given symbol.
/// </summary>
/// <param name="symbol">The input symbol to start the search from.</param>
/// <param name="predicate">An optional predicate to filter symbols.</param>
/// <remarks>The resulting symbol, if a match was found.</remarks>
public static TSymbol? FirstAncestorOrSelf<TSymbol>(this ISymbol symbol, Func<TSymbol, bool>? predicate = null)
where TSymbol : class, ISymbol
{
for (ISymbol? parentSymbol = symbol; parentSymbol is not null; parentSymbol = parentSymbol.ContainingSymbol)
{
if (parentSymbol is TSymbol targetSymbol && (predicate?.Invoke(targetSymbol) is not false))
{
return targetSymbol;
}
}
return null;
}
}