DizzyJump

Untitled

Nov 6th, 2025
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using System.Linq;
  2. using Microsoft.CodeAnalysis;
  3. using Microsoft.CodeAnalysis.CSharp;
  4. using Microsoft.CodeAnalysis.CSharp.Syntax;
  5. using Microsoft.CodeAnalysis.Text;
  6.  
  7. namespace SourceGenerators.Utils;
  8.  
  9. public static class ClassDeclarationSyntaxExtensions
  10. {
  11.     public static bool IsAbstract(this ClassDeclarationSyntax classDeclaration)
  12.     {
  13.         return classDeclaration.Modifiers.Any(SyntaxKind.AbstractKeyword);
  14.     }
  15.  
  16.     public static bool IsStatic(this ClassDeclarationSyntax classDeclaration)
  17.     {
  18.         return classDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword);
  19.     }
  20.  
  21.     public static bool HasAttributes(this ClassDeclarationSyntax classDeclaration)
  22.     {
  23.         return classDeclaration.AttributeLists.Count > 0;
  24.     }
  25.  
  26.     public static bool HasAttribute(this ClassDeclarationSyntax classDeclaration, string attributeName)
  27.     {
  28.         return classDeclaration.AttributeLists
  29.             .SelectMany(al => al.Attributes)
  30.             .Any(attr => attr.Name.ToString().Contains(attributeName));
  31.     }
  32. }
  33.  
  34. using System.Collections.Generic;
  35. using System.Linq;
  36. using Microsoft.CodeAnalysis;
  37.  
  38. namespace SourceGenerators.Utils;
  39.  
  40. public static class SymbolExtensions
  41. {
  42.     public static string GetFullyQualifiedName(this INamedTypeSymbol symbol)
  43.     {
  44.         return symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
  45.     }
  46.  
  47.     public static bool HasAttributes(this INamedTypeSymbol symbol)
  48.     {
  49.         return !symbol.GetAttributes().IsEmpty;
  50.     }
  51.    
  52.     public static bool ImplementsInterface(this INamedTypeSymbol symbol, string interfaceName)
  53.     {
  54.         return symbol.AllInterfaces.Any(i => i.ToDisplayString() == interfaceName);
  55.     }
  56.  
  57.     public static bool HasAttribute(this ISymbol symbol, string attributeName)
  58.     {
  59.         return symbol.GetAttributes()
  60.             .Any(attr => attr.AttributeClass?.ToDisplayString() == attributeName);
  61.     }
  62.    
  63.     public static IEnumerable<AttributeData> GetAttributeDataByType(this ISymbol symbol, string attributeName)
  64.     {
  65.         return symbol.GetAttributes()
  66.             .Where(attr => attr.AttributeClass?.ToDisplayString() == attributeName);
  67.     }
  68. }
  69.  
  70. using System.Collections.Concurrent;
  71. using System.Text;
  72.  
  73. namespace SourceGenerators;
  74.  
  75. public static class StringBuilderPool {
  76.     private const int MAX_POOL_SIZE = 64;
  77.  
  78.     private static readonly ConcurrentStack<StringBuilder> pool = new();
  79.  
  80.     public static StringBuilder Get() => pool.TryPop(out var sb) ? sb : new StringBuilder();
  81.  
  82.     public static void Return(StringBuilder sb) {
  83.         if (pool.Count >= MAX_POOL_SIZE) {
  84.             return;
  85.         }
  86.  
  87.         sb.Clear();
  88.         pool.Push(sb);
  89.     }
  90.  
  91.     public static string ToStringAndReturn(this StringBuilder sb) {
  92.         var result = sb.ToString();
  93.         Return(sb);
  94.         return result;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment