-
-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better print generic methods and constructors #284
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation and tests look good.
Feel free to update to the new pattern matching syntax if you think it's cool 😄
@@ -16,19 +16,40 @@ public static Doc Print(IEnumerable<TypeParameterConstraintClauseSyntax> constra | |||
{ | |||
return Doc.Null; | |||
} | |||
else if (constraintClausesList.Count == 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, this whole function can be replaced by C# 8/9's new pattern matching syntax 🚀
return constraintClauses.ToList() switch
{
{ Count: 0 } => Doc.Null,
{ Count: 1 } => /* stuff */,
var list when list[0].Parent is MethodDeclarationSyntax => /* stuff */,
var list => Doc.Indent(
Doc.HardLine,
Doc.Join(Doc.HardLine, list.Select(TypeParameterConstraintClause.Print))
)
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the short constructor long base/this call should get handled, but in general this all looks good.
There are a couple other edge cases I ran into that can probably just go into new issues.
There is no indent on the comment but used to be. Probably not super important.
public UsesRendererTypesInDeclarations()
/*MMInvocation*/: base(null, null) { }
// this is probably the same issue
public ServeCommand(CommandLineApplication parent)
// We pass arbitrary arguments through to the ASP.NET Core configuration
: base(throwOnUnexpectedArg: false)
This looks a little weird, but also looked weird before your changes and I'm not really sure how we'd want to format it.
// currently
private T InstantiateComponent<T>() where T : IComponent =>
(T)_renderer.InstantiateComponent<T>();
// used to be
private T InstantiateComponent<T>()
where T : IComponent => (T)_renderer.InstantiateComponent<T>();
A bigger version of the same thing
// currently
private static IList<T> GetComponents<T>(RenderTreeBuilder builder) where T : IComponent =>
builder.GetFrames()
.AsEnumerable()
.Where(x => x.FrameType == RenderTreeFrameType.Component)
.Select(x => (T)x.Component)
.ToList();
// used to be
private static IList<T> GetComponents<T>(RenderTreeBuilder builder)
where T : IComponent =>
builder.GetFrames()
.AsEnumerable()
.Where(x => x.FrameType == RenderTreeFrameType.Component)
.Select(x => (T)x.Component)
.ToList();
public ReallyLoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogMethodName() : base( | ||
false | ||
) { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one should probably change. Here's a real world example.
// now formats as
public DiagnosticLocation(int line, int column) : this(
$"{DiagnosticProject.DefaultFilePathPrefix}.cs",
line,
column
) { }
// used to format as this, which I think is preferable.
public DiagnosticLocation(int line, int column)
: this($"{DiagnosticProject.DefaultFilePathPrefix}.cs", line, column) { }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When they are both long, I think it looks good
public ServerInteropTest(
BrowserFixture browserFixture,
ToggleExecutionModeServerFixture<Program> serverFixture,
ITestOutputHelper output
) : base(
browserFixture,
serverFixture.WithServerExecution().WithAdditionalArguments(GetAdditionalArguments()),
output
) { }
Another example with a long base call which wouldn't fit on a single line. It isn't obvious all the parameters are going to a base call.
public TestRemoteRenderer(IServiceProvider serviceProvider, IClientProxy client) : base(
serviceProvider,
NullLoggerFactory.Instance,
new CircuitOptions(),
new CircuitClientProxy(client, "connection"),
NullLogger.Instance,
null
) { }
// I'm thinking it should break before : base to make it more clear
public TestRemoteRenderer(IServiceProvider serviceProvider, IClientProxy client)
: base(
serviceProvider,
NullLoggerFactory.Instance,
new CircuitOptions(),
new CircuitClientProxy(client, "connection"),
NullLogger.Instance,
null
) { }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
After looking at this again, maybe it doesn't need to change. I'm not sure what else we'd do with it, and I think the new way is better than the old. It might just look odd because I'm not used to seeing generic expression bodied methods. |
Yeah, I wouldn't be so sure of either one. I've seen both styles in |
If there is only one generic constraint, we try to keep it on the same line. If more one, they're broken up.
As for the constructor initialiser, it's kept on the same line as the ending brace of the arguments.
Closes #94