From 080011fe8fb2dd41563bab7ebc5307eef5a652ff Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Sep 2022 12:58:01 +0200 Subject: [PATCH] Share raw multiline string literal formatting logic --- ...ator.CreateBuildHlslSourceMethod.Syntax.cs | 17 +++------ ...mputeSharp.SourceGeneration.Hlsl.projitems | 1 + .../Helpers/SyntaxTokenHelper.cs | 35 +++++++++++++++++++ ...ator.CreateBuildHlslSourceMethod.Syntax.cs | 20 +++-------- 4 files changed, 44 insertions(+), 29 deletions(-) create mode 100644 src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SyntaxTokenHelper.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs index e50afcf4..675fd0e5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs @@ -1,5 +1,5 @@ using ComputeSharp.D2D1.__Internals; -using Microsoft.CodeAnalysis; +using ComputeSharp.SourceGeneration.Helpers; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; @@ -22,17 +22,6 @@ partial class ID2D1ShaderGenerator /// The resulting instance for the BuildHlslSource method. public static MethodDeclarationSyntax GetSyntax(string hlslSource, int hierarchyDepth) { - // Create a token to represent the raw multiline string literal expression with the HLSL source. Here some spaces are - // also added to properly align the resulting text with one indentation below the declaring string constant. - // The spaces are: 4 for each containing type, 4 for the containing method, and 4 for the one additional indentation. - string indentation = new(' ', 4 * hierarchyDepth + 4 + 4); - SyntaxToken hlslSourceLiteralToken = Token( - TriviaList(), - SyntaxKind.MultiLineRawStringLiteralToken, - $"\"\"\"\n{indentation}{hlslSource.Replace("\n", $"\n{indentation}")}\"\"\"", - hlslSource, - TriviaList()); - // This code produces a method declaration as follows: // // readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.BuildHlslSource(out string hlslSource) @@ -49,7 +38,9 @@ partial class ID2D1ShaderGenerator AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, IdentifierName("hlslSource"), - LiteralExpression(SyntaxKind.StringLiteralExpression, hlslSourceLiteralToken))))); + LiteralExpression( + SyntaxKind.StringLiteralExpression, + SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth)))))); } } } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems index 829cb1e5..d609eae6 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems @@ -12,6 +12,7 @@ + diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SyntaxTokenHelper.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SyntaxTokenHelper.cs new file mode 100644 index 00000000..48415734 --- /dev/null +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SyntaxTokenHelper.cs @@ -0,0 +1,35 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace ComputeSharp.SourceGeneration.Helpers; + +/// +/// Helpers to create and work with -s. +/// +internal static class SyntaxTokenHelper +{ + /// + /// Creates a representing a raw multiline string literal with the given text. + /// + /// The text to use for the created raw multiline string literal token. + /// The current indentation level to use. + /// A representing a raw multiline string literal with the value of . + public static SyntaxToken CreateRawMultilineStringLiteral(string text, int indentationLevel) + { + // Create a token to represent the raw multiline string literal expression. Here some spaces are + // also added to properly align the resulting text with one indentation below the declaring string constant. + // The spaces are: 4 for each containing type, 4 for the containing method, and 4 for the one additional indentation. + // An extra newline and indentation has to be added to the raw text when there is no trailing newline, as not doing + // so would otherwise case the terminating """ token to fall on the same line, which is invalid syntax. This is only + // needed to make the code valid, as the actual literal context of the string is not affected and remains the same. + string indentation = new(' ', 4 * indentationLevel + 4 + 4); + + return Token( + TriviaList(), + SyntaxKind.MultiLineRawStringLiteralToken, + $"\"\"\"\n{indentation}{text.Replace("\n", $"\n{indentation}")}{(text.EndsWith("\n") ? "" : $"\n{indentation}")}\"\"\"", + text, + TriviaList()); + } +} diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs index 524e7fd5..8a8f4556 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs @@ -2,8 +2,8 @@ using System.Collections.Immutable; using System.Text; using ComputeSharp.__Internals; +using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGenerators.Models; -using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; @@ -117,25 +117,13 @@ partial class IShaderGenerator sizeHint += textBuilder.Length; - // Create a token to represent the raw multiline string literal expression. Here some spaces are - // also added to properly align the resulting text with one indentation below the declaring string constant. - // The spaces are: 4 for each containing type, 4 for the containing method, and 4 for the one additional indentation. - // An extra newline and indentation has to be added to the raw text when there is no trailing newline, as not doing - // so would otherwise case the terminating """ token to fall on the same line, which is invalid syntax. This is only - // needed to make the code valid, as the actual literal context of the string is not affected and remains the same. - string indentation = new(' ', 4 * hierarchyDepth + 4 + 4); - SyntaxToken textToken = Token( - TriviaList(), - SyntaxKind.MultiLineRawStringLiteralToken, - $"\"\"\"\n{indentation}{text.Replace("\n", $"\n{indentation}")}{(text.EndsWith("\n") ? "" : $"\n{indentation}")}\"\"\"", - text, - TriviaList()); - statements.Add( ExpressionStatement( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("builder"), IdentifierName("Append"))) - .AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, textToken))))); + .AddArgumentListArguments(Argument(LiteralExpression( + SyntaxKind.StringLiteralExpression, + SyntaxTokenHelper.CreateRawMultilineStringLiteral(text, hierarchyDepth)))))); textBuilder.Clear(); }