diff --git a/src/ThisAssembly.Constants/CSharp.sbntxt b/src/ThisAssembly.Constants/CSharp.sbntxt index 77711f19..38c39e4c 100644 --- a/src/ThisAssembly.Constants/CSharp.sbntxt +++ b/src/ThisAssembly.Constants/CSharp.sbntxt @@ -13,7 +13,7 @@ {{~ if $0.Comment ~}} {{ $0.Comment }} {{~ else ~}} - /// => @"{{ $0.Value }}" + /// => @"{{ $0.EscapedValue }}" {{~ end ~}} /// {{- end -}} diff --git a/src/ThisAssembly.Constants/ConstantsGenerator.cs b/src/ThisAssembly.Constants/ConstantsGenerator.cs index 5c9da5cf..553e695a 100644 --- a/src/ThisAssembly.Constants/ConstantsGenerator.cs +++ b/src/ThisAssembly.Constants/ConstantsGenerator.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Xml.Linq; using Devlooped.Sponsors; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -93,9 +94,9 @@ void GenerateConstant(SourceProductionContext spc, } if (comment != null) - comment = "/// " + string.Join(Environment.NewLine + "/// ", comment.Trim().Replace("\\n", Environment.NewLine).Trim(['\r', '\n']).Split([Environment.NewLine], StringSplitOptions.None)); + comment = "/// " + string.Join(Environment.NewLine + "/// ", new XText(comment).ToString().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)); + comment = "/// " + string.Join(Environment.NewLine + "/// ", new XText(value).ToString().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, type ?? "string"),], root, rootComment); diff --git a/src/ThisAssembly.Constants/Model.cs b/src/ThisAssembly.Constants/Model.cs index 10f9ee13..d3b59c85 100644 --- a/src/ThisAssembly.Constants/Model.cs +++ b/src/ThisAssembly.Constants/Model.cs @@ -125,5 +125,6 @@ static Area GetArea(Area area, IEnumerable areaPath) [DebuggerDisplay("{Name} = {Value}")] record Constant(string Name, string? Value, string? Comment, string Type = "string") { + public string? EscapedValue => Value == null ? null : new XText(Value).ToString(); public bool IsText => Type.Equals("string", StringComparison.OrdinalIgnoreCase); } diff --git a/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj b/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj index c430c5fe..2082d2ef 100644 --- a/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj +++ b/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj @@ -23,6 +23,7 @@ CS0618;CS8981;TA100;$(NoWarn) false $([System.IO.File]::ReadAllText($(MSBuildProjectFullPath))) + $(ProjectFile) true true @@ -71,6 +72,7 @@ +