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

Allow empty/blank lines in object initializers #1110

Closed
Qtax opened this issue Jan 3, 2024 · 4 comments · Fixed by #1113
Closed

Allow empty/blank lines in object initializers #1110

Qtax opened this issue Jan 3, 2024 · 4 comments · Fixed by #1113
Milestone

Comments

@Qtax
Copy link

Qtax commented Jan 3, 2024

In larger object initializers it is helpful to group properties into logical groups/blocks separated by an empty line and/or a blank line followed by a comment.

Input:

var templateVars = new
{
	date_trunc = filter.AggregationType.GetDateTruncString(true),

	date_from = from.ToString("yyyy-MM-dd"),
	date_to = to.ToString("yyyy-MM-dd"),

	compare_from = compareFrom.ToString("yyyy-MM-dd"),
	compare_to = compareTo.ToString("yyyy-MM-dd"),

	// Pagination
	order_by = sortOrder switch
	{
		StreamGamesSortBy.HoursWatched => "watched_hours",
		StreamGamesSortBy.AverageCCV => "viewers_avg",
		StreamGamesSortBy.PeakCCV => "viewers_max",
		_ => throw new ArgumentOutOfRangeException()
	},
	order_direction = sortDir switch
	{
		OrderDirection.Desc => "DESC",
		OrderDirection.Asc => "ASC",
		_ => throw new ArgumentOutOfRangeException()
	},
	limit = filter.Take,
	offset = filter.Skip
};

Output:

var templateVars = new
{
	date_trunc = filter.AggregationType.GetDateTruncString(true),
	date_from = from.ToString("yyyy-MM-dd"),
	date_to = to.ToString("yyyy-MM-dd"),
	compare_from = compareFrom.ToString("yyyy-MM-dd"),
	compare_to = compareTo.ToString("yyyy-MM-dd"),
	// Pagination
	order_by = sortOrder switch
	{
		StreamGamesSortBy.HoursWatched => "watched_hours",
		StreamGamesSortBy.AverageCCV => "viewers_avg",
		StreamGamesSortBy.PeakCCV => "viewers_max",
		_ => throw new ArgumentOutOfRangeException()
	},
	order_direction = sortDir switch
	{
		OrderDirection.Desc => "DESC",
		OrderDirection.Asc => "ASC",
		_ => throw new ArgumentOutOfRangeException()
	},
	limit = filter.Take,
	offset = filter.Skip
};

Expected behavior:

Allow single empty lines in object initializers: between initialized properties and/or comments.
This would be consequent with how blank lines are allowed between lines of regular code and/or comments.

Does this seem like an acceptable change to you?

@belav
Copy link
Owner

belav commented Jan 6, 2024

I don't see a good reason to not allow it, it does keep things consistent with other places that allow new lines.

Your example was for an anonymous object, which is a different node than a regular object initializer. But I made changes to allow it in either place + other types of initializers (dictionaries, arrays, etc).

@belav
Copy link
Owner

belav commented Jan 6, 2024

I take that last part back. Dictionaries and arrays are a bit trickier. This will just be for objects (anonymous or not) for now.

belav added a commit that referenced this issue Jan 6, 2024
@belav belav added this to the 0.27.0 milestone Jan 6, 2024
@Qtax
Copy link
Author

Qtax commented Jan 8, 2024

Great, thanks! And as you pointed out it would be nice with same behaviour in dictionaries and arrays too.
We just started using csharpier in our project, so just starting to notice some things.

A small possibly related issue with object initializers that I don't really care about that maybe you'd like you know of:

When formatting this (in 0.26.7):

		var foo = new {
			foo = 1,


			// empty lines after a comment are not removed


			bar = 2,
		};

You get this:

		var foo = new
		{
			foo = 1,
			// empty lines after a comment are not removed


			bar = 2,
		};

Would prefer/expect:

		var foo = new
		{
			foo = 1,

			// empty lines after a comment are not removed

			bar = 2,
		};

Or at least that empty lines after comments are treated the same as before.

belav added a commit that referenced this issue Jan 12, 2024
belav added a commit that referenced this issue Jan 15, 2024
@belav
Copy link
Owner

belav commented Jan 15, 2024

The empty lines sticking around after a comment is a known issue I have somewhere in the backlog. I believe it happens with comments anywhere. No one has complained so it hasn't been prioritized yet. Thanks for letting me know!

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 a pull request may close this issue.

2 participants