scalameta / scalafmt

Code formatter for Scala
http://scalameta.org/scalafmt
Apache License 2.0
1.44k stars 277 forks source link

Force newline before extends regardless of class/trait definition #4542

Closed Facsimiler closed 2 weeks ago

Facsimiler commented 2 weeks ago

I want to be able to force a newline before an extends (or with or derives) clause, but there doesn't seem to be a way to achieve this.

For example, let's say I have a definition such as:

class SomeClass(a: Int, b: Double, c: String)
extends SomeBaseClass
with SomeTrait

No matter what formatting options I've used, I always seem to end up with:

class SomeClass(a: Int, b: Double, c: String) extends SomeBaseClass with SomeTrait

I can't seem to obtain the former without using "keep" to keep the formatting, or changing the maxColumn setting to force wrapping earlier.

That is, I can't seem to find a way to reformat the latter to look like the former.

This formatting option is supported by IntelliJ, for example,

Am I missing something? If not, can I raise this feature as an enhancement request?

Thanks for your assistance!

kitbellew commented 2 weeks ago

@Facsimiler are you reporting a bug, or are you asking for help in finding the right configuration parameter? if the latter, you should create a discussion or simply ask on discord.

Facsimiler commented 2 weeks ago

@kitbellew I apologize. I've reviewed a number of online help instances, and gone through all of the settings, and it seems what I want to do is not possible. I meant to phrase this more as an enhancement request, and have updated the issue description accordingly. If what I'm requesting can already be done with existing settings, then please point me in the right direction.

kitbellew commented 2 weeks ago

@kitbellew I apologize. I've reviewed a number of online help instances, and gone through all of the settings,

Yes, it can be done. It's documented, with examples. So, perhaps, not all of the settings. Please see https://scalameta.org/scalafmt/docs/configuration.html.

Facsimiler commented 2 weeks ago

@kitbellew If it's documented and there are examples, they are not on that page. Perhaps you could take a second to tell me what they are?

tgodzik commented 2 weeks ago

Something around https://scalameta.org/scalafmt/docs/configuration.html#binpackparentconstructors maybe ?

kitbellew commented 2 weeks ago

thank you, @tgodzik.

@Facsimiler please let us know if you still think the examples are not on that page.

Facsimiler commented 2 weeks ago

@tgodzik @kitbellew I have tried those settings and they do not do what I want. They only insert newlines if the max column is exceeded. I want unconditional newlines before extends, with, and derives. That doesn't seem possible.

tgodzik commented 2 weeks ago

Looks like the best we can do is binPack.parentConstructors = keep but that will not split it into lines. I guess we don't have that setting then

kitbellew commented 2 weeks ago

keep should work. re-opening.

Facsimiler commented 2 weeks ago

@kitbellew @tgodzik Thanks, guys! I appreciate it!

kitbellew commented 2 weeks ago

keep does work. please double-check, @Facsimiler .

tgodzik commented 2 weeks ago

I think @Facsimiler would want something along the lines of unfold here.

Facsimiler commented 2 weeks ago

@tgodzik Correct.

@kitbellew I mentioned in my original post that I could use keep to retain the intended formatting, but I can't then change the single-line form to he multi-line form. So yes, some kind of unfold here (although that might imply unfolding any constructor arguments, which I don't want to do).

Ideally, I'd like a property something like newlines.alwaysBeforeExtendsOrDerives. I guess some may want to keep with clauses on the same line or a separate line, with explicit control of that too, but I'm not sure what to suggest for that...

kitbellew commented 2 weeks ago

@kitbellew I mentioned in my original post that I could use keep to retain the intended formatting, but I can't then change the single-line form to he multi-line form.

@Facsimiler when you say keep without being more specific, this is understood to mean newlines.source = keep.

if you had actually meant binPack.parentConstructors = keep, saying so explicitly would have allowed us to avoid several iterations here.

Facsimiler commented 2 weeks ago

@kitbellew Since I can't use parentConstructors = keep unless I also have newlines.source = keep, I didn't think that distinction was necessary. Since I want to use newlines.source = unfold, I can't use parentConstructors = keep in any case. I apologize that I didn't make that clear earlier.

kitbellew commented 2 weeks ago

@kitbellew Since I can't use parentConstructors = keep unless I also have newlines.source = keep, I didn't think that distinction was necessary. Since I want to use newlines.source = unfold, I can't use parentConstructors = keep in any case. I apologize that I didn't make that clear earlier.

that is not accurate, you can use it with newline.source = null (or not specifying it).

there's a reason why the submission template requires providing current configuration. perhaps next time you will be able to follow it.