Correctly insert attribute after built-in D2D attributes
このコミットが含まれているのは:
1 親
1013cfb6f3
コミット
112a4af860
4ファイル変更
+138
-3
検索条件に一致するファイルはありません
@@ -0,0 +1,44 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for the <see cref="SymbolInfo"/> type.
|
||||
/// </summary>
|
||||
internal static class SymbolInfoExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to get the resolved attribute type symbol from a given <see cref="SymbolInfo"/> value.
|
||||
/// </summary>
|
||||
/// <param name="symbolInfo">The <see cref="SymbolInfo"/> value to check.</param>
|
||||
/// <param name="typeSymbol">The resulting attribute type symbol, if correctly resolved.</param>
|
||||
/// <returns>Whether <paramref name="symbolInfo"/> is resolved to a symbol.</returns>
|
||||
/// <remarks>
|
||||
/// This can be used to ensure users haven't eg. spelled names incorrecty or missed a using directive. Normally, code would just
|
||||
/// not compile if that was the case, but that doesn't apply for attributes using invalid targets. In that case, Roslyn will ignore
|
||||
/// any errors, meaning the generator has to validate the type symbols are correctly resolved on its own.
|
||||
/// </remarks>
|
||||
public static bool TryGetAttributeTypeSymbol(this SymbolInfo symbolInfo, [NotNullWhen(true)] out INamedTypeSymbol? typeSymbol)
|
||||
{
|
||||
ISymbol? attributeSymbol = symbolInfo.Symbol;
|
||||
|
||||
// If no symbol is selected and there is a single candidate symbol, use that
|
||||
if (attributeSymbol is null && symbolInfo.CandidateSymbols is [ISymbol candidateSymbol])
|
||||
{
|
||||
attributeSymbol = candidateSymbol;
|
||||
}
|
||||
|
||||
// Extract the symbol from either the current one or the containing type
|
||||
if ((attributeSymbol as INamedTypeSymbol ?? attributeSymbol?.ContainingType) is not INamedTypeSymbol resultingSymbol)
|
||||
{
|
||||
typeSymbol = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
typeSymbol = resultingSymbol;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする