Push NewLine setting into RazorCodeGenerationOptions

This change stops pushing the NewLine value into RazorCodeDocument.Items.
This commit is contained in:
Dustin Campbell 2024-08-14 13:03:27 -07:00
Родитель 28bfd61d38
Коммит 1eb601c417
9 изменённых файлов: 46 добавлений и 57 удалений

Просмотреть файл

@ -402,7 +402,7 @@ public class CSharpCodeWriterTest
o.IndentSize = 4;
});
using var writer = new CodeWriter(Environment.NewLine, options);
using var writer = new CodeWriter(options);
// Act
writer.BuildClassDeclaration(Array.Empty<string>(), "C", "", Array.Empty<string>(), Array.Empty<TypeParameter>(), context: null);
@ -428,7 +428,7 @@ public class CSharpCodeWriterTest
o.IndentSize = 4;
});
using var writer = new CodeWriter(Environment.NewLine, options);
using var writer = new CodeWriter(options);
// Act
writer.BuildClassDeclaration(Array.Empty<string>(), "C", "", Array.Empty<string>(), Array.Empty<TypeParameter>(), context: null);

Просмотреть файл

@ -15,8 +15,6 @@ public sealed class CodeRenderingContext : IDisposable
{
private readonly record struct ScopeInternal(IntermediateNodeWriter Writer);
internal static readonly object NewLineStringKey = "NewLineString";
public RazorCodeGenerationOptions Options { get; }
public CodeWriter CodeWriter { get; }
@ -62,9 +60,7 @@ public sealed class CodeRenderingContext : IDisposable
_linePragmas = ArrayBuilderPool<LinePragma>.Default.Get();
_sourceMappings = ArrayBuilderPool<SourceMapping>.Default.Get();
// Set new line character to a specific string regardless of platform, for testing purposes.
var newLineString = codeDocument.Items[NewLineStringKey] as string ?? Environment.NewLine;
CodeWriter = new CodeWriter(newLineString, options);
CodeWriter = new CodeWriter(options);
}
public void Dispose()

Просмотреть файл

@ -43,13 +43,13 @@ public sealed partial class CodeWriter : IDisposable
private int _currentLineCharacterIndex;
public CodeWriter()
: this(Environment.NewLine, RazorCodeGenerationOptions.Default)
: this(RazorCodeGenerationOptions.Default)
{
}
public CodeWriter(string newLine, RazorCodeGenerationOptions options)
public CodeWriter(RazorCodeGenerationOptions options)
{
SetNewLine(newLine);
SetNewLine(options.NewLine);
IndentWithTabs = options.IndentWithTabs;
TabSize = options.IndentSize;

Просмотреть файл

@ -8,14 +8,17 @@ namespace Microsoft.AspNetCore.Razor.Language;
public sealed class RazorCodeGenerationOptions(
RazorCodeGenerationOptionsFlags flags,
int indentSize,
string newLine,
string? rootNamespace,
string? suppressUniqueIds)
{
private readonly RazorCodeGenerationOptionsFlags _flags = flags;
public bool DesignTime => _flags.HasFlag(RazorCodeGenerationOptionsFlags.DesignTime);
public bool IndentWithTabs => _flags.HasFlag(RazorCodeGenerationOptionsFlags.IndentWithTabs);
public int IndentSize { get; } = indentSize;
public string NewLine { get; } = newLine;
/// <summary>
/// Gets the root namespace for the generated code.
@ -113,12 +116,14 @@ public sealed class RazorCodeGenerationOptions(
public static RazorCodeGenerationOptions Default { get; } = new RazorCodeGenerationOptions(
flags: RazorCodeGenerationOptionsFlags.DefaultFlags,
indentSize: 4,
newLine: Environment.NewLine,
rootNamespace: null,
suppressUniqueIds: null);
public static RazorCodeGenerationOptions DesignTimeDefault { get; } = new RazorCodeGenerationOptions(
flags: RazorCodeGenerationOptionsFlags.DefaultDesignTimeFlags,
indentSize: 4,
newLine: Environment.NewLine,
rootNamespace: null,
suppressUniqueIds: null);
@ -150,10 +155,13 @@ public sealed class RazorCodeGenerationOptions(
public RazorCodeGenerationOptionsBuilder ToBuilder()
{
var builder = new RazorCodeGenerationOptionsBuilder(_flags);
builder.IndentSize = IndentSize;
builder.RootNamespace = RootNamespace;
builder.SuppressUniqueIds = SuppressUniqueIds;
var builder = new RazorCodeGenerationOptionsBuilder(_flags)
{
IndentSize = IndentSize,
NewLine = NewLine,
RootNamespace = RootNamespace,
SuppressUniqueIds = SuppressUniqueIds
};
return builder;
}

Просмотреть файл

@ -1,11 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
namespace Microsoft.AspNetCore.Razor.Language;
public sealed class RazorCodeGenerationOptionsBuilder
{
private RazorCodeGenerationOptionsFlags _flags;
private string _newLine = Environment.NewLine;
public RazorConfiguration? Configuration { get; }
@ -13,6 +16,15 @@ public sealed class RazorCodeGenerationOptionsBuilder
public int IndentSize { get; set; } = 4;
public string NewLine
{
get => _newLine;
set
{
_newLine = value ?? Environment.NewLine;
}
}
public bool IndentWithTabs
{
get => _flags.IsFlagSet(RazorCodeGenerationOptionsFlags.IndentWithTabs);
@ -166,6 +178,7 @@ public sealed class RazorCodeGenerationOptionsBuilder
=> new(
_flags,
IndentSize,
NewLine,
RootNamespace,
SuppressUniqueIds);

Просмотреть файл

@ -10,7 +10,6 @@ using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.AspNetCore.Razor.Test.Common;
using Microsoft.AspNetCore.Razor.Test.Common.Mef;
@ -117,7 +116,7 @@ public class RazorToolingIntegrationTestBase : ToolingTestBase
if (LineEnding != null)
{
b.Phases.Insert(0, new ForceLineEndingPhase(LineEnding));
b.Features.Add(new SetNewLineOptionFeature(LineEnding));
}
b.Features.Add(new DefaultTypeNameFeature());
@ -430,18 +429,13 @@ public class RazorToolingIntegrationTestBase : ToolingTestBase
}
}
private class ForceLineEndingPhase : RazorEnginePhaseBase
private sealed class SetNewLineOptionFeature(string newLine) : RazorEngineFeatureBase, IConfigureRazorCodeGenerationOptionsFeature
{
public ForceLineEndingPhase(string lineEnding)
{
LineEnding = lineEnding;
}
public int Order { get; }
public string LineEnding { get; }
protected override void ExecuteCore(RazorCodeDocument codeDocument)
public void Configure(RazorCodeGenerationOptionsBuilder options)
{
codeDocument.Items[CodeRenderingContext.NewLineStringKey] = LineEnding;
options.NewLine = newLine;
}
}

Просмотреть файл

@ -26,7 +26,7 @@ public static class TestCodeRenderingContext
var codeDocument = RazorCodeDocument.Create(source);
if (newLineString != null)
{
codeDocument.Items[CodeRenderingContext.NewLineStringKey] = newLineString;
optionsBuilder.NewLine = newLineString;
}
if (suppressUniqueIds != null)
@ -64,7 +64,7 @@ public static class TestCodeRenderingContext
var codeDocument = RazorCodeDocument.Create(source);
if (newLineString != null)
{
codeDocument.Items[CodeRenderingContext.NewLineStringKey] = newLineString;
optionsBuilder.NewLine = newLineString;
}
if (suppressUniqueIds != null)

Просмотреть файл

@ -10,7 +10,6 @@ using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.AspNetCore.Razor.Language.Legacy;
using Microsoft.AspNetCore.Razor.Language.Syntax;
@ -299,8 +298,7 @@ public abstract class IntegrationTestBase
{
return RazorProjectEngine.Create(configuration, FileSystem, b =>
{
b.Features.Add(new ConfigureCodeGenerationOptionsFeature());
b.Phases.Insert(0, new ConfigureCodeRenderingPhase(LineEnding));
b.Features.Add(new ConfigureCodeGenerationOptionsFeature(LineEnding));
b.RegisterExtensions();
@ -783,31 +781,17 @@ public abstract class IntegrationTestBase
return Regex.Replace(content, "(?<!\r)\n", lineEnding, RegexOptions.None, TimeSpan.FromSeconds(10));
}
private sealed class ConfigureCodeGenerationOptionsFeature : RazorEngineFeatureBase, IConfigureRazorCodeGenerationOptionsFeature
private sealed class ConfigureCodeGenerationOptionsFeature(string lineEnding) : RazorEngineFeatureBase, IConfigureRazorCodeGenerationOptionsFeature
{
public int Order { get; }
public void Configure(RazorCodeGenerationOptionsBuilder options)
{
options.NewLine = lineEnding;
options.SuppressUniqueIds = "test";
}
}
private class ConfigureCodeRenderingPhase : RazorEnginePhaseBase
{
public ConfigureCodeRenderingPhase(string lineEnding)
{
LineEnding = lineEnding;
}
public string LineEnding { get; }
protected override void ExecuteCore(RazorCodeDocument codeDocument)
{
codeDocument.Items[CodeRenderingContext.NewLineStringKey] = LineEnding;
}
}
// 'Default' imports won't have normalized line-endings, which is unfriendly for testing.
private class NormalizedDefaultImportFeature : RazorProjectEngineFeatureBase, IImportProjectFeature
{

Просмотреть файл

@ -10,7 +10,6 @@ using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
using Microsoft.AspNetCore.Razor.Test.Common;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
@ -120,7 +119,7 @@ public class RazorIntegrationTestBase
if (LineEnding != null)
{
b.Phases.Insert(0, new ForceLineEndingPhase(LineEnding));
b.Features.Add(new SetNewLineOptionFeature(LineEnding));
}
b.Features.Add(new CompilationTagHelperFeature());
@ -488,18 +487,13 @@ public class RazorIntegrationTestBase
}
}
private class ForceLineEndingPhase : RazorEnginePhaseBase
private sealed class SetNewLineOptionFeature(string newLine) : RazorEngineFeatureBase, IConfigureRazorCodeGenerationOptionsFeature
{
public ForceLineEndingPhase(string lineEnding)
{
LineEnding = lineEnding;
}
public int Order { get; }
public string LineEnding { get; }
protected override void ExecuteCore(RazorCodeDocument codeDocument)
public void Configure(RazorCodeGenerationOptionsBuilder options)
{
codeDocument.Items[CodeRenderingContext.NewLineStringKey] = LineEnding;
options.NewLine = newLine;
}
}