From e330bb53724ace3e3da9afe1186e335818cfcabb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 May 2024 12:53:35 +0200 Subject: [PATCH] Share 'SyntaxNodeExtensions' with common project --- .../Extensions/SyntaxNodeExtensions.cs | 32 +--------------- .../ComputeSharp.SourceGeneration.projitems | 1 + .../Extensions/SyntaxNodeExtensions.cs | 38 +++++++++++++++++++ 3 files changed, 41 insertions(+), 30 deletions(-) create mode 100644 src/ComputeSharp.SourceGeneration/Extensions/SyntaxNodeExtensions.cs diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs index fa7b4db0..104119d3 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs @@ -6,37 +6,9 @@ using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace ComputeSharp.SourceGeneration.Extensions; -/// -/// A with some extension methods for C# syntax nodes. -/// -internal static class SyntaxNodeExtensions +/// +partial class SyntaxNodeExtensions { - /// - /// Checks whether a given is a given type declaration with or potentially with any base types, using only syntax. - /// - /// The type of declaration to check for. - /// The input to check. - /// Whether is a given type declaration with or potentially with any base types. - public static bool IsTypeDeclarationWithOrPotentiallyWithBaseTypes(this SyntaxNode node) - where T : TypeDeclarationSyntax - { - // Immediately bail if the node is not a type declaration of the specified type - if (node is not T typeDeclaration) - { - return false; - } - - // If the base types list is not empty, the type can definitely has implemented interfaces - if (typeDeclaration.BaseList is { Types.Count: > 0 }) - { - return true; - } - - // If the base types list is empty, check if the type is partial. If it is, it means - // that there could be another partial declaration with a non-empty base types list. - return typeDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword); - } - /// /// Returns a with a block body. /// diff --git a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems index 3dc0426f..b932f8c9 100644 --- a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems +++ b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems @@ -19,6 +19,7 @@ + diff --git a/src/ComputeSharp.SourceGeneration/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/SyntaxNodeExtensions.cs new file mode 100644 index 00000000..28aad272 --- /dev/null +++ b/src/ComputeSharp.SourceGeneration/Extensions/SyntaxNodeExtensions.cs @@ -0,0 +1,38 @@ +using System.Linq; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace ComputeSharp.SourceGeneration.Extensions; + +/// +/// A with some extension methods for C# syntax nodes. +/// +internal static partial class SyntaxNodeExtensions +{ + /// + /// Checks whether a given is a given type declaration with or potentially with any base types, using only syntax. + /// + /// The type of declaration to check for. + /// The input to check. + /// Whether is a given type declaration with or potentially with any base types. + public static bool IsTypeDeclarationWithOrPotentiallyWithBaseTypes(this SyntaxNode node) + where T : TypeDeclarationSyntax + { + // Immediately bail if the node is not a type declaration of the specified type + if (node is not T typeDeclaration) + { + return false; + } + + // If the base types list is not empty, the type can definitely has implemented interfaces + if (typeDeclaration.BaseList is { Types.Count: > 0 }) + { + return true; + } + + // If the base types list is empty, check if the type is partial. If it is, it means + // that there could be another partial declaration with a non-empty base types list. + return typeDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword); + } +} \ No newline at end of file