Merge pull request #724 from Sergio0694/dev/remove-leftover-syntax

Remove leftover syntax factory helpers
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-12-17 21:09:46 +01:00
コミット 2f6ab50b9e
2ファイル変更
-120

検索条件に一致するファイルはありません

-90
ファイルの表示
@@ -2,9 +2,6 @@ using System;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Microsoft.CodeAnalysis.SymbolDisplayTypeQualificationStyle;
namespace ComputeSharp.SourceGeneration.Models;
@@ -135,91 +132,4 @@ internal sealed partial record HierarchyInfo(string FullyQualifiedMetadataName,
return fullyQualifiedTypeName.ToString();
}
/// <summary>
/// Creates a <see cref="CompilationUnitSyntax"/> instance for the current hierarchy.
/// </summary>
/// <param name="memberDeclarations">The member declarations to add to the generated type.</param>
/// <returns>A <see cref="CompilationUnitSyntax"/> instance for the current hierarchy.</returns>
public CompilationUnitSyntax GetSyntax(params MemberDeclarationSyntax[] memberDeclarations)
{
return GetSyntax(memberDeclarations, []);
}
/// <summary>
/// Creates a <see cref="CompilationUnitSyntax"/> instance for the current hierarchy.
/// </summary>
/// <param name="memberDeclarations">The member declarations to add to the generated type.</param>
/// <param name="additionalMemberDeclarations">Additional top-level member declarations, if any.</param>
/// <returns>A <see cref="CompilationUnitSyntax"/> instance for the current hierarchy.</returns>
public CompilationUnitSyntax GetSyntax(MemberDeclarationSyntax[] memberDeclarations, params MemberDeclarationSyntax[] additionalMemberDeclarations)
{
// Create the partial type declaration with for the current hierarchy.
// This code produces a type declaration as follows:
//
// partial <TYPE_KIND> <TYPE_NAME>
// {
// <MEMBER_DECLARATIONS>
// }
TypeDeclarationSyntax typeDeclarationSyntax =
Hierarchy[0].GetSyntax()
.AddModifiers(Token(SyntaxKind.PartialKeyword))
.AddMembers(memberDeclarations);
// Add all parent types in ascending order, if any
foreach (TypeInfo parentType in Hierarchy.AsSpan().Slice(1))
{
typeDeclarationSyntax =
parentType.GetSyntax()
.AddModifiers(Token(SyntaxKind.PartialKeyword))
.AddMembers(typeDeclarationSyntax);
}
// Prepare the leading trivia for the generated compilation unit.
// This will produce code as follows:
//
// // <auto-generated/>
// #pragma warning disable
SyntaxTriviaList syntaxTriviaList = TriviaList(
Comment("// <auto-generated/>"),
Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true)));
CompilationUnitSyntax compilationUnitSyntax;
if (Namespace is "")
{
// If there is no namespace, attach the pragma directly to the declared type,
// and skip the namespace declaration. This will produce code as follows:
//
// <SYNTAX_TRIVIA>
// <TYPE_HIERARCHY>
compilationUnitSyntax =
CompilationUnit()
.AddMembers(typeDeclarationSyntax.WithLeadingTrivia(syntaxTriviaList));
}
else
{
// Create the compilation unit with disabled warnings, target namespace and generated type.
// This will produce code as follows:
//
// <SYNTAX_TRIVIA>
// namespace <NAMESPACE>;
//
// <TYPE_HIERARCHY>
compilationUnitSyntax =
CompilationUnit().AddMembers(
FileScopedNamespaceDeclaration(IdentifierName(Namespace))
.WithLeadingTrivia(syntaxTriviaList)
.AddMembers(typeDeclarationSyntax));
}
// Add any additional members, if any
if (additionalMemberDeclarations.Length > 0)
{
compilationUnitSyntax = compilationUnitSyntax.AddMembers(additionalMemberDeclarations);
}
// Normalize and return the tree
return compilationUnitSyntax.NormalizeWhitespace(eol: "\n");
}
}
-30
ファイルの表示
@@ -1,7 +1,4 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace ComputeSharp.SourceGeneration.Models;
@@ -28,31 +25,4 @@ internal sealed record TypeInfo(string QualifiedName, TypeKind Kind, bool IsReco
_ => "class"
};
}
/// <summary>
/// Creates a <see cref="TypeDeclarationSyntax"/> instance for the current info.
/// </summary>
/// <returns>A <see cref="TypeDeclarationSyntax"/> instance for the current info.</returns>
public TypeDeclarationSyntax GetSyntax()
{
// Create the partial type declaration with the kind.
// This code produces a class declaration as follows:
//
// <TYPE_KIND> <TYPE_NAME>
// {
// }
//
// Note that specifically for record declarations, we also need to explicitly add the open
// and close brace tokens, otherwise member declarations will not be formatted correctly.
return Kind switch
{
TypeKind.Struct => StructDeclaration(QualifiedName),
TypeKind.Interface => InterfaceDeclaration(QualifiedName),
TypeKind.Class when IsRecord =>
RecordDeclaration(Token(SyntaxKind.RecordKeyword), QualifiedName)
.WithOpenBraceToken(Token(SyntaxKind.OpenBraceToken))
.WithCloseBraceToken(Token(SyntaxKind.CloseBraceToken)),
_ => ClassDeclaration(QualifiedName)
};
}
}