Skip to content

Commit

Permalink
Merge pull request #724 from Sergio0694/dev/remove-leftover-syntax
Browse files Browse the repository at this point in the history
Remove leftover syntax factory helpers
  • Loading branch information
Sergio0694 authored Dec 17, 2023
2 parents 212e974 + 180183e commit 2f6ab50
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 120 deletions.
90 changes: 0 additions & 90 deletions src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
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;
Expand Down Expand Up @@ -135,91 +132,4 @@ public string GetFullyQualifiedTypeName()

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 changes: 0 additions & 30 deletions src/ComputeSharp.SourceGeneration/Models/TypeInfo.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -28,31 +25,4 @@ public string GetTypeKeyword()
_ => "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)
};
}
}

0 comments on commit 2f6ab50

Please sign in to comment.