Share raw multiline string literal formatting logic
このコミットが含まれているのは:
1 親
b45629b839
コミット
080011fe8f
4ファイル変更
+44
-29
検索条件に一致するファイルはありません
+4
-13
@@ -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
|
||||
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslSource</c> method.</returns>
|
||||
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))))));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\DiagnosticDescriptors.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\StringExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SyntaxNodeExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SyntaxTokenHelper.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownFields.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownKeywords.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownMethods.cs" />
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// Helpers to create and work with <see cref="SyntaxToken"/>-s.
|
||||
/// </summary>
|
||||
internal static class SyntaxTokenHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="SyntaxToken"/> representing a raw multiline string literal with the given text.
|
||||
/// </summary>
|
||||
/// <param name="text">The text to use for the created raw multiline string literal token.</param>
|
||||
/// <param name="indentationLevel">The current indentation level to use.</param>
|
||||
/// <returns>A <see cref="SyntaxToken"/> representing a raw multiline string literal with the value of <paramref name="text"/>.</returns>
|
||||
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());
|
||||
}
|
||||
}
|
||||
+4
-16
@@ -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();
|
||||
}
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする