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

Router: apply select indent after a comment #1334 #1557

Closed

Conversation

kitbellew
Copy link
Collaborator

@kitbellew kitbellew commented Nov 20, 2019

Fixes #1334

@olafurpg
Copy link
Member

Thank you for this contribution! I really appreciate you taking a look at these long standing issues 🙏

At a quick glance this looks great but I won't be able to review properly until next week earliest. In the meantime, can you please update the PR descriptions to include before/after examples. I personally try to structure my PR descriptions in the following way

Previously, Scalafmt formatted code like this: EXAMPLE CODE. Now, Scalafmt formats the same code like this instead: NEW FORMAT OUTPUT.

You can also include "Fixes #ISSUE" in the description so that the issue is automatically closed when merged.

@@ -62,7 +62,7 @@ val lastToken = owner.body.tokens.filter {
case _: Whitespace | _: Comment => false
case _ => true
} // edge case, if body is empty expire on arrow.
.lastOption.getOrElse(arrow)
.lastOption.getOrElse(arrow)
Copy link
Collaborator

Choose a reason for hiding this comment

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

IMHO previous behavior was prettier 🤔
@tanishiking @olafurpg what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's acceptable to add an indent here 👍 , and it's more consistent if scalafmt adds an indent before methods for any objects (even if it's Block).

FYI prettier add an indent in this case

Copy link
Member

Choose a reason for hiding this comment

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

I like the new behavior.

Copy link
Member

@tanishiking tanishiking left a comment

Choose a reason for hiding this comment

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

I left some minor comments about testing, but others are LGTM! Great work!

@@ -1344,8 +1344,12 @@ class Router(formatOps: FormatOps) {
case FormatToken(_, c: T.Comment, between) =>
Seq(Split(newlines2Modification(between), 0))
// Commented out code should stay to the left
case FormatToken(c: T.Comment, _, between) if c.syntax.startsWith("//") =>
Seq(Split(Newline, 0))
case FormatToken(c: T.Comment, right, _) if c.syntax.startsWith("//") =>
Copy link
Member

Choose a reason for hiding this comment

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

Could you add some comments that explain the behavior with some code examples? So that people who read this code can easily understand the meaning.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will do

@@ -37,3 +37,15 @@ class ExtendTest
with D
with E =>
}
<<< #1334 1
Copy link
Member

Choose a reason for hiding this comment

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

It might be better to move these tests to test/resources/default/indent.stat or something because this behavior is nothing to do with continuationIndent?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no problem. perhaps i misunderstand the meaning of continuation; i thought that since we are indenting because of the .something which continues from previous line, it should go in this file.

@@ -37,3 +37,15 @@ class ExtendTest
with D
with E =>
}
<<< #1334 1
Copy link
Member

Choose a reason for hiding this comment

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

it'll be helpful if you name the test with more meaningful description (you can include #1334 to point the issue number of course :) )

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that was an oversight, thanks for noticing.

Copy link
Collaborator Author

@kitbellew kitbellew left a comment

Choose a reason for hiding this comment

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

i will implement the suggestions shortly.

but i do want to get your opinion: while this fix appears to work, is it possible that the original problem is due to :chain: no looking far enough to include that line? there's a rule which obtains the method chain and inserts newlines/indents.

@@ -1344,8 +1344,12 @@ class Router(formatOps: FormatOps) {
case FormatToken(_, c: T.Comment, between) =>
Seq(Split(newlines2Modification(between), 0))
// Commented out code should stay to the left
case FormatToken(c: T.Comment, _, between) if c.syntax.startsWith("//") =>
Seq(Split(Newline, 0))
case FormatToken(c: T.Comment, right, _) if c.syntax.startsWith("//") =>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will do

@@ -37,3 +37,15 @@ class ExtendTest
with D
with E =>
}
<<< #1334 1
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no problem. perhaps i misunderstand the meaning of continuation; i thought that since we are indenting because of the .something which continues from previous line, it should go in this file.

@@ -37,3 +37,15 @@ class ExtendTest
with D
with E =>
}
<<< #1334 1
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that was an oversight, thanks for noticing.

Albert Meltzer added 2 commits November 23, 2019 20:14
Currently, the code doesn't introduce newlines into select chains even
if their length exceeds the maximum line length.

It also results in missing indent when a comment is in the middle of
such a chain.

Fix both problems, by allowing a newline if one is present in the input.

Fixes scalameta#1334.
Copy link
Member

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

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

Did you close this PR intentionally?

val vv =
v.aaaaaaaaaa.bbbbbbbbbb.cccccccccc.dddddddddd.eeeeeeeeee.ffffffffff.gggggggggg.hhhhhhhhhh.iiiiiiiiii
}
<<< #1334 very long broken select chain
Copy link
Member

Choose a reason for hiding this comment

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

This change looks unrelated to comments, the current behavior of scalafmt is to format everything in a single line.

The current behavior of Scalafmt is to never break on select nodes a.b unless b is an "applied" node like a.b() or a.b[T] or a.b { ... }. This is an intentional decision for better or worse, and we're going to change it then we'll need careful performance testing to ensure the search state doesn't explode in cases that formatted fine before.

@@ -62,7 +62,7 @@ val lastToken = owner.body.tokens.filter {
case _: Whitespace | _: Comment => false
case _ => true
} // edge case, if body is empty expire on arrow.
.lastOption.getOrElse(arrow)
.lastOption.getOrElse(arrow)
Copy link
Member

Choose a reason for hiding this comment

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

I like the new behavior.

val noNewLine = !style.activeForEdition_2019_11 ||
newlines == 0 || rightOwner.isNot[Term.Select]
val newlinePenalty = if (noNewLine) 0 else 10 + treeDepth(rightOwner)
Seq(
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, doubling the search state here is likely to introduce new "search state exploded" errors for programs that formatted fine before.

@kitbellew kitbellew deleted the indent-chain-after-comment branch July 5, 2020 11:28
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.

Wired alignment when chain contains comment / no parenthesis call / with parenthesis call
4 participants