From c051752363afd5ee3c31e7ea2626272cabd0646d Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Mon, 16 Sep 2024 21:30:57 -0300 Subject: [PATCH] Fix multiline values being truncated Just like we do for semicolon-separated values (encoding them prior to persistence to editorconfig/analyzer config), we now encode newlines as `\n` and decode them prior to emitting source code. This makes previously failing scenarios work (i.e. project properties) as well as fixing previously working ones in AssemblyInfo generator (i.e. Description property). Fixes #390 --- src/ThisAssembly.Constants/CSharp.sbntxt | 2 +- .../ConstantsGenerator.cs | 19 ++++++++----------- .../ThisAssembly.Constants.targets | 7 +++++++ src/ThisAssembly.Tests/Tests.cs | 10 ++++++++++ .../ThisAssembly.Tests.csproj | 1 - 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/ThisAssembly.Constants/CSharp.sbntxt b/src/ThisAssembly.Constants/CSharp.sbntxt index e0231946..98cd71d7 100644 --- a/src/ThisAssembly.Constants/CSharp.sbntxt +++ b/src/ThisAssembly.Constants/CSharp.sbntxt @@ -11,7 +11,7 @@ {{- func summary -}} /// {{~ if $0.Comment ~}} - /// {{ $0.Comment }} + {{ $0.Comment }} {{~ else ~}} /// => @"{{ $0.Value }}" {{~ end ~}} diff --git a/src/ThisAssembly.Constants/ConstantsGenerator.cs b/src/ThisAssembly.Constants/ConstantsGenerator.cs index 39275b2f..eb6e8cfc 100644 --- a/src/ThisAssembly.Constants/ConstantsGenerator.cs +++ b/src/ThisAssembly.Constants/ConstantsGenerator.cs @@ -1,4 +1,4 @@ -using System.Diagnostics.Tracing; +using System; using System.Globalization; using System.IO; using System.Linq; @@ -65,15 +65,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context) var options = context.GetStatusOptions(); context.RegisterSourceOutput(inputs.Combine(options), GenerateConstant); - //(spc, source) => - //{ - // var status = Diagnostics.GetOrSetStatus(source.Right); - // var warn = IsEditor && - // (status == Devlooped.Sponsors.SponsorStatus.Unknown || status == Devlooped.Sponsors.SponsorStatus.Expired); - - // GenerateConstant(spc, source.Left, warn ? string.Format( - // CultureInfo.CurrentCulture, Resources.Editor_Disabled, Funding.Product, Funding.HelpUrl) : null); - //}); } void GenerateConstant(SourceProductionContext spc, @@ -92,7 +83,13 @@ void GenerateConstant(SourceProductionContext spc, return; } - var rootArea = Area.Load([new(name, value, comment),], root); + if (comment != null) + comment = "/// " + string.Join(Environment.NewLine + "/// ", comment.Trim().Replace("\\n", Environment.NewLine).Trim(['\r', '\n']).Split([Environment.NewLine], StringSplitOptions.None)); + else + comment = "/// " + string.Join(Environment.NewLine + "/// ", value.Replace("\\n", Environment.NewLine).Trim(['\r', '\n']).Split([Environment.NewLine], StringSplitOptions.None)); + + // Revert normalization of newlines performed in MSBuild to workaround the limitation in editorconfig. + var rootArea = Area.Load([new(name, value.Replace("\\n", Environment.NewLine).Trim(['\r', '\n']), comment),], root); // For now, we only support C# though var file = parse.Language.Replace("#", "Sharp") + ".sbntxt"; var template = Template.Parse(EmbeddedResource.GetContent(file), file); diff --git a/src/ThisAssembly.Constants/ThisAssembly.Constants.targets b/src/ThisAssembly.Constants/ThisAssembly.Constants.targets index ca59b16d..0ecb711f 100644 --- a/src/ThisAssembly.Constants/ThisAssembly.Constants.targets +++ b/src/ThisAssembly.Constants/ThisAssembly.Constants.targets @@ -44,6 +44,13 @@ Value="%(FileConstant.Value)" Comment="%(FileConstant.Comment)" /> + + + + $([MSBuild]::ValueOrDefault('%(Constant.Value)', '').Replace($([System.Environment]::NewLine), '\n')) + $([MSBuild]::ValueOrDefault('%(Constant.Comment)', '').Replace($([System.Environment]::NewLine), '\n')) + + Assert.Equal( + """ + A Description + with a newline and + * Some "things" with quotes + // Some comments too. + """.ReplaceLineEndings(), ThisAssembly.Project.Multiline.ReplaceLineEndings()); + [Fact] public void CanUseConstants() => Assert.Equal("Baz", ThisAssembly.Constants.Foo.Bar); diff --git a/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj b/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj index d5b132b7..aeefda82 100644 --- a/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj +++ b/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj @@ -64,7 +64,6 @@ -