Skip to content

Commit

Permalink
Merge pull request #306 from kindermannhubert/formatting-problem-with…
Browse files Browse the repository at this point in the history
…-types-defined-inside-REPL

Fixes of two formatting issues with types defined inside REPL
  • Loading branch information
kindermannhubert committed Sep 28, 2023
2 parents f5af0da + 89617fc commit 9b1f059
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 7 additions & 3 deletions CSharpRepl.Services/Roslyn/Formatting/TypeNameFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ private StyledString FormatNonGenericTypeName(TypeInfo typeInfo, TypeNameFormatt
else
{
sb.Append(FormatTypeName(typeInfo.DeclaringType, options));
sb.Append('.');
if (sb.Length > 0) sb.Append('.');
AppendNameWithoutGenericPart(typeInfo, sb);
}
return sb.ToStyledString();

void AppendNameWithoutGenericPart(TypeInfo typeInfo, StyledStringBuilder builder)
{
var typeStyle = GetTypeStyle(typeInfo, highlighter);
var name = typeInfo.Name;

if (SubmissionRegex().IsMatch(name)) //https://github.com/waf/CSharpRepl/issues/194)
if (IsSubmissionType(typeInfo)) //https://github.com/waf/CSharpRepl/issues/194)
{
return;
}

var name = typeInfo.Name;
int backtick = name.IndexOf('`');
if (backtick > 0)
{
Expand Down Expand Up @@ -203,6 +203,7 @@ private StyledString FormatGenericTypeName([MaybeNull] TypeInfo typeInfo, TypeNa
var nestedTypes = ArrayBuilder<TypeInfo>.GetInstance();
do
{
if (IsSubmissionType(typeInfo)) break; //https://github.com/waf/CSharpRepl/issues/305
nestedTypes.Add(typeInfo);
typeInfo = typeInfo.DeclaringType?.GetTypeInfo();
}
Expand Down Expand Up @@ -322,6 +323,9 @@ private StyledString FormatGenericTypeName([MaybeNull] TypeInfo typeInfo, TypeNa
};
}

private static bool IsSubmissionType(TypeInfo typeInfo)
=> SubmissionRegex().IsMatch(typeInfo.Name);

[GeneratedRegex("Submission#[0-9]+")]
private static partial Regex SubmissionRegex();
}
21 changes: 21 additions & 0 deletions CSharpRepl.Tests/ObjectFormatting/CustomObjectFormattersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using CSharpRepl.Services.Roslyn;
using CSharpRepl.Services.Roslyn.Formatting;
using CSharpRepl.Services.Roslyn.Formatting.CustomObjectFormatters;
using CSharpRepl.Services.Roslyn.Scripting;
using Xunit;

namespace CSharpRepl.Tests.ObjectFormatting;

public class CustomObjectFormattersTests : IClassFixture<RoslynServicesFixture>
{
private readonly TestFormatter formatter;
private readonly RoslynServices services;

public CustomObjectFormattersTests(RoslynServicesFixture fixture)
{
formatter = TestFormatter.Create(fixture.ConsoleStub);
services = fixture.RoslynServices;
}

#region TypeFormatter
Expand All @@ -30,6 +35,22 @@ public void TestTypeFormatting(Type value, string expectedOutput_0, string expec
Assert.Equal(expectedOutput_0, formatter.Format(TypeFormatter.Instance, value, Level.FirstDetailed));
Assert.Equal(expectedOutput_1, formatter.Format(TypeFormatter.Instance, value, Level.FirstSimple));
}

[Theory]
[InlineData("class Class1 { } new Class1()", "Class1")] //https://github.com/waf/CSharpRepl/issues/287
[InlineData("class Class2<T> { } new Class2<int>()", "Class2<int>")] //https://github.com/waf/CSharpRepl/issues/305
public async Task TypeDefinedInsideReplFormattingBug(string input, string expectedOutput)
{
var eval = await services.EvaluateAsync(input);
if (eval is EvaluationResult.Success { ReturnValue.Value: object obj })
{
Assert.Equal(expectedOutput, formatter.Format(TypeFormatter.Instance, obj.GetType(), Level.FirstSimple));
}
else
{
Assert.Fail();
}
}
#endregion

#region MethodInfoFormatter
Expand Down

0 comments on commit 9b1f059

Please sign in to comment.