Share raw multiline string literal formatting logic

このコミットが含まれているのは:
Sergio Pedri committed 2022-09-17 12:58:01 +02:00
1 親 b45629b839
コミット 080011fe8f
4ファイル変更
+44 -29

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

@@ -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" />
+35
ファイルの表示
@@ -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());
}
}