dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.9k stars 4.01k forks source link

Feature request: Allow me to specify a desired max-column that code should be wrapped against. #15406

Open CyrusNajmabadi opened 7 years ago

CyrusNajmabadi commented 7 years ago

I'd like this both for code and for comments.

Right now there's nothing that helps me when code gets really long. This is problematic with features like "implement interface" where an interface method might end up super long, making it tough to use in the IDE and in PRs.

After generation, i have to go in and manually wrap things. I'd like to have a preference (ideally project scoped) that says what column we try not to go past. Features like formatting/generation should attempt to respect this wrapping column when possible when they have items they can wrap. For example, method parameters would be a suitable wrapping entity.

Thanks!

Pilchie commented 7 years ago

For reference, I implemented this back in VS2005, but we ended up pulling it out. It ends up leading to another explosion of options for how to do the wrapping:

I'm not saying it's impossible, just that it's not nearly as simple as a single option for that the line length should be.

JeffBail commented 4 years ago

It would be nice if this could be implemented so as to support the max_line_length editorconfig parameter already supported by many editors.

jmarolf commented 4 years ago

So enforcing a hard limit on line length is not actually possible in the language due to:

Here is a simple (and incomplete) proposal:

snebjorn commented 4 years ago

I feel these concerns have already been solved by prettier to a satisfactory degree

You can play around with it here https://prettier.io/playground/ (no it doesn't work with C#)

siegfriedpammer commented 4 years ago

So enforcing a hard limit on line length is not actually possible in the language due to:

  • existing indentation rules: what if the user is 20 levels of indentation deep and therefore goes over the desired column width? Basically anywhere enforcing a column limit would violate another style rule we would need to decide who take precedence
  • literals (especially string literals): while we could attempt to split string literals that exceed the column width it seems that this could potentially be re-writing the users code more than they intend

For me, there is no need to modify single tokens/literals. If a single token + indentation exceeds the column limit, I would expect the formatter to break after that token. The only exception would be semicolons, these should be kept on the same line as the last token.

I like your proposal a lot. Thank you!

jmarolf commented 4 years ago

@snebjorn prettier focuses on formatting in a single style for javascript. Whatever this formatting becomes it will need to interact with the many formatting options that already exist today for C#. I do not know how comparable to two situations are.

CyrusNajmabadi commented 4 years ago

Small point:

If a single token + indentation exceeds the column limit, I would expect the formatter to break after that token.

That's not what i woudl expect. I would expect to break before teh column except in the case that breaking before would put the token on the same column as the line it was on (and thus would still be in the same problematic state). i.e. we should only cross the column as a last resort.

snebjorn commented 4 years ago

@snebjorn prettier focuses on formatting in a single style for javascript. Whatever this formatting becomes it will need to interact with the many formatting options that already exist today for C#. I do not know how comparable to two situations are.

Sorry I mistook this as the https://github.com/dotnet/format repo

jmarolf commented 4 years ago

@snebjorn no problem. Part of this discussion is trying to define what to goals are and how they differ from what other successful formatters do.

snebjorn commented 3 years ago

Meanwhile a JAVA plugin have appeared for prettier. JAVA and C# have pretty similar formatting concerns so perhaps it can be used as an inspiration :) It's still early days, so it's not perfect.

Here's a playground https://shaolans.github.io/prettier-java-playground/

The link might change or go stale so here you can track changes https://github.com/jhipster/prettier-java/issues/193

f00-beard commented 2 years ago

I'd be happy if it just created a notification of long lines (suggestion/warning/error, etc.) - like other editorconfig suggestions; it wouldn't have to actually reflow the code.

tiesmaster commented 2 years ago

@f00-beard Roslynator actually has an analyzer to report on max line length. You might want to try that one out.

BTW If you just want to visualize a certain boundary in VS, then Editor Guidelines is your friend ;)

belav commented 2 years ago

If anyone is looking for something opinionated like prettier that will reflow your code - I ported portions of prettier to .net and adopted it for csharp. It is reasonably stable at this point. https://github.com/belav/csharpier

chucker commented 2 years ago

@f00-beard Roslynator actually has an analyzer to report on max line length. You might want to try that one out.

BTW If you just want to visualize a certain boundary in VS, then Editor Guidelines is your friend ;)

I kind of wish I could establish separate soft and hard boundaries, but this is a good start:

[*.{cs,vb}]
dotnet_diagnostic.rcs0056.severity = warning
roslynator_max_line_length = 120

Now, lines >= 80 are more noticeable due to Editor Guidelines, and lines >= 120 also get an IDE warning.

tiesmaster commented 2 years ago

visually, I have Editor Guidelines set at 80 and 120 @chucker Funny, I have mine set at 100 and 120 😅 But sounds like a good strategy, and good way to combine both options 👍

dmrickey commented 3 months ago

Does this still not work?

CyrusNajmabadi commented 3 months ago

@dmrickey There has been no progress on this.