Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- using Microsoft.CodeAnalysis;
- using Microsoft.CodeAnalysis.CSharp;
- using Microsoft.CodeAnalysis.CSharp.Syntax;
- using Microsoft.CodeAnalysis.Text;
- namespace SourceGenerators.Utils;
- public static class ClassDeclarationSyntaxExtensions
- {
- public static bool IsAbstract(this ClassDeclarationSyntax classDeclaration)
- {
- return classDeclaration.Modifiers.Any(SyntaxKind.AbstractKeyword);
- }
- public static bool IsStatic(this ClassDeclarationSyntax classDeclaration)
- {
- return classDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword);
- }
- public static bool HasAttributes(this ClassDeclarationSyntax classDeclaration)
- {
- return classDeclaration.AttributeLists.Count > 0;
- }
- public static bool HasAttribute(this ClassDeclarationSyntax classDeclaration, string attributeName)
- {
- return classDeclaration.AttributeLists
- .SelectMany(al => al.Attributes)
- .Any(attr => attr.Name.ToString().Contains(attributeName));
- }
- }
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.CodeAnalysis;
- namespace SourceGenerators.Utils;
- public static class SymbolExtensions
- {
- public static string GetFullyQualifiedName(this INamedTypeSymbol symbol)
- {
- return symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
- }
- public static bool HasAttributes(this INamedTypeSymbol symbol)
- {
- return !symbol.GetAttributes().IsEmpty;
- }
- public static bool ImplementsInterface(this INamedTypeSymbol symbol, string interfaceName)
- {
- return symbol.AllInterfaces.Any(i => i.ToDisplayString() == interfaceName);
- }
- public static bool HasAttribute(this ISymbol symbol, string attributeName)
- {
- return symbol.GetAttributes()
- .Any(attr => attr.AttributeClass?.ToDisplayString() == attributeName);
- }
- public static IEnumerable<AttributeData> GetAttributeDataByType(this ISymbol symbol, string attributeName)
- {
- return symbol.GetAttributes()
- .Where(attr => attr.AttributeClass?.ToDisplayString() == attributeName);
- }
- }
- using System.Collections.Concurrent;
- using System.Text;
- namespace SourceGenerators;
- public static class StringBuilderPool {
- private const int MAX_POOL_SIZE = 64;
- private static readonly ConcurrentStack<StringBuilder> pool = new();
- public static StringBuilder Get() => pool.TryPop(out var sb) ? sb : new StringBuilder();
- public static void Return(StringBuilder sb) {
- if (pool.Count >= MAX_POOL_SIZE) {
- return;
- }
- sb.Clear();
- pool.Push(sb);
- }
- public static string ToStringAndReturn(this StringBuilder sb) {
- var result = sb.ToString();
- Return(sb);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment