Skip to content
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

Merged
merged 8 commits into from
Jun 12, 2021

Conversation

dishuk13
Copy link
Contributor

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

Copy link
Collaborator

@shocklateboy92 shocklateboy92 left a 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)
Copy link
Collaborator

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))
    )                                               
};

@dishuk13 dishuk13 changed the title Better print generic methods Better print generic methods and constructors Jun 11, 2021
@dishuk13 dishuk13 marked this pull request as draft June 11, 2021 12:43
Copy link
Owner

@belav belav left a 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();

Comment on lines 39 to 41
public ReallyLoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogMethodName() : base(
false
) { }
Copy link
Owner

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) { }

Copy link
Owner

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
                ) { }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@belav
Copy link
Owner

belav commented Jun 11, 2021

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();

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.

@dishuk13
Copy link
Contributor Author

After looking at this again, maybe it doesn't need to change

Yeah, I wouldn't be so sure of either one. I've seen both styles in csharpier-repos. I like the one in this PR just because it preserves a line.

@dishuk13 dishuk13 requested a review from belav June 11, 2021 23:08
@dishuk13 dishuk13 marked this pull request as ready for review June 11, 2021 23:10
@dishuk13 dishuk13 marked this pull request as draft June 11, 2021 23:15
@dishuk13 dishuk13 marked this pull request as ready for review June 12, 2021 10:24
@belav belav merged commit 0fec511 into belav:master Jun 12, 2021
@dishuk13 dishuk13 deleted the generic-method branch June 12, 2021 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve formatting of generic methods and constructors
3 participants