Skip to content

Commit

Permalink
Fix multiline values being truncated
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kzu committed Sep 17, 2024
1 parent 53878c2 commit c051752
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/ThisAssembly.Constants/CSharp.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{{- func summary -}}
/// <summary>
{{~ if $0.Comment ~}}
/// {{ $0.Comment }}
{{ $0.Comment }}
{{~ else ~}}
/// => @"{{ $0.Value }}"
{{~ end ~}}
Expand Down
19 changes: 8 additions & 11 deletions src/ThisAssembly.Constants/ConstantsGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics.Tracing;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/ThisAssembly.Constants/ThisAssembly.Constants.targets
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
Value="%(FileConstant.Value)"
Comment="%(FileConstant.Comment)" />
</ItemGroup>
<ItemGroup>
<!-- Normalize newlines to avoid losing content -->
<Constant Update="@(Constant)">
<Value>$([MSBuild]::ValueOrDefault('%(Constant.Value)', '').Replace($([System.Environment]::NewLine), '\n'))</Value>
<Comment>$([MSBuild]::ValueOrDefault('%(Constant.Comment)', '').Replace($([System.Environment]::NewLine), '\n'))</Comment>
</Constant>
</ItemGroup>
</Target>

<Target Name="_InjectConstantAdditionalFiles"
Expand Down
10 changes: 10 additions & 0 deletions src/ThisAssembly.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ with a newline and
// Some comments too.
""".ReplaceLineEndings(), ThisAssembly.Info.Description.ReplaceLineEndings());

[Fact]
public void CanUseMultilineProjectProperty()
=> 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);
Expand Down
1 change: 0 additions & 1 deletion src/ThisAssembly.Tests/ThisAssembly.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
<ProjectProperty Include="Foo" />
<ProjectProperty Include="Foo" />
<ProjectProperty Include="Description" />
<!-- Multiline values that go through .editorconfig will get truncated, unfortunately -->
<ProjectProperty Include="Multiline" />
<Constant Include="Foo.Raw" Value="$(Multiline)" Comment="$(Multiline)" />
<Constant Include="Foo.Bar" Value="Baz" Comment="Yay!" />
Expand Down

0 comments on commit c051752

Please sign in to comment.