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

Wrap supertypes if primary constructor wraps #1866

Merged
merged 2 commits into from
Mar 15, 2024

Conversation

JakeWharton
Copy link
Collaborator

@JakeWharton JakeWharton commented Mar 15, 2024

This means there's always a ") : " on a line which prefixes the supertypes, so force them to also wrap to multiple lines.

I decided to take a more opinionated approach that wasn't based on count, but instead is based on whether the primary constructor wraps to multiple lines. Closes #1865.

  • docs/changelog.md has been updated if applicable.
  • CLA signed.

This means there's always a ") : " on a line which prefixes the supertypes, so force them to also wrap to multiple lines.
Comment on lines +628 to +632
|public enum class Roshambo(
| private val handPosition: String,
|) : Runnable,
| Cloneable {
| SCISSORS("peace sign"),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ktlint wants this to be

Suggested change
|public enum class Roshambo(
| private val handPosition: String,
|) : Runnable,
| Cloneable {
| SCISSORS("peace sign"),
|public enum class Roshambo(
| private val handPosition: String,
|) : Runnable,
| Cloneable {
| SCISSORS("peace sign"),

which I absolutely hate. Visually there's no delineation between the supertypes and the body.

We could force a leading newline and do

Suggested change
|public enum class Roshambo(
| private val handPosition: String,
|) : Runnable,
| Cloneable {
| SCISSORS("peace sign"),
|public enum class Roshambo(
| private val handPosition: String,
|) : Runnable,
| Cloneable {
|
| SCISSORS("peace sign"),

which ktlint allows. It's not great, but I'd be okay with this.

Thoughts? We could also just keep what I've already done. Most people aren't formatting generated code and I prefer mine best.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The code style suggests that all supertypes should have the same indentation, so I think it's a bug in ktlint? I also prefer it this way, so let's keep it as is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They only mention horizontal alignment when you make a line break after the colon for non-primary constrictor cases.

The example with a wrapping primary constructor only aligns because they are doing a 4-space indent. So unfortunately I think ktlint is correct in what it does.

That being said, we can still leave it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The example with a wrapping primary constructor only aligns because they are doing a 4-space indent.

Good point.

Comment on lines +628 to +632
|public enum class Roshambo(
| private val handPosition: String,
|) : Runnable,
| Cloneable {
| SCISSORS("peace sign"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

The code style suggests that all supertypes should have the same indentation, so I think it's a bug in ktlint? I also prefer it this way, so let's keep it as is.

|import java.util.AbstractSet
|import kotlin.Comparable
|
|public abstract class Taco() : AbstractSet<Food>(), Serializable, Comparable<Taco>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will this currently simply overflow for longer lists? The suggestion in the style guide is to wrap after the :, but I believe it'll be quite hard to recognize this case in the code.

image

Copy link
Collaborator Author

@JakeWharton JakeWharton Mar 15, 2024

Choose a reason for hiding this comment

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

I want an API like

val mark = codeWriter.mark()
val originalLine = codeWriter.lineNumber

// Do normal write 
if (codeWriter.lineNumber > originalLine) {
  codeWriter.reset(mark)

  // Do wrapped write
}

Would be useful for so many constructs. Probably even deserves a helper function that just takes two lambdas.

@JakeWharton JakeWharton enabled auto-merge (squash) March 15, 2024 13:27
@JakeWharton JakeWharton merged commit ede321a into main Mar 15, 2024
8 checks passed
@JakeWharton JakeWharton deleted the jw.wrap-on-wrap.2024-03-14 branch March 15, 2024 13:34
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.

Wrap supertype/superinterfaces like parameters
2 participants