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

Automatically manage trailing commas when running with --google-style #427

Closed
wants to merge 1 commit into from

Conversation

nreid260
Copy link
Contributor

@nreid260 nreid260 commented Oct 7, 2023

Commas are added/removed according to the following rules:

  • Lambda param lists => remove
  • Single element lists => remove
  • Lists that fit on one line => remove
  • All other lists (multiline lists) => add

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 7, 2023
@nreid260
Copy link
Contributor Author

nreid260 commented Oct 7, 2023

@davidtorosyan @hick209 @cgrushko

I know we talked about this idea a bit several months ago. I've limited the impl to --google-style, but it could easily be expanded to more styles, or possibly a separate flag.

@nreid260 nreid260 force-pushed the trailing_comma branch 2 times, most recently from 5aee7c8 to 663b6b9 Compare October 7, 2023 08:52
@nreid260
Copy link
Contributor Author

nreid260 commented Nov 7, 2023

@hick209 Is anyone available to review?

@hick209
Copy link
Contributor

hick209 commented Nov 7, 2023

Hey @nreid260, sorry I'm out on paternity leave.

I'll ping the group internally to get into this though

@facebook-github-bot
Copy link
Contributor

@strulovich has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link
Contributor

@strulovich strulovich left a comment

Choose a reason for hiding this comment

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

Generally looks ok, and terribly sorry you had to wait for so long. A bunch of mostly style issues and requirement for extra documentation so it's easier to jump to and we should be good to go.

.let { addRedundantElements(it, options) }
.let { convertLineSeparators(it, Newlines.guessLineSeparator(kotlinCode)!!) }

return if (shebang.isNotEmpty()) { shebang + "\n" + formatted } else { formatted }
Copy link
Contributor

Choose a reason for hiding this comment

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

Style nit: If we're restyling this, maybe add this check to the let chain as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 58 to 65
/** Remove and insert trialing commas based on how the code would format without them. */
val manageTrailingCommas: Boolean = false,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please elaborate more? The best way would be with 1-2 code examples that explain what the user can expect to see.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines +102 to +107
file.accept(
object : KtTreeVisitorVoid() {
override fun visitKtElement(element: KtElement) {
trailingCommaSuggestor.takeElement(element)
super.visitElement(element)
}
})
Copy link
Contributor

Choose a reason for hiding this comment

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

You can just use forEachDescendantOfType or collectDescendantsOfType for shorter and simpler code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

takeElement decides which elements to track. It would be error prone to duplicate that knowledge into the traversal.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think I understood your comment, I was suggesting:

file.forEachDescendantOfType<KtElement> {
  trailingommaSuggestor.takeElement(element)
}


fun takeElement(element: KtElement) {
if (!element.text.contains("\n")) {
return // Only suggest trailing commas where there is already a line break
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, a bunch of code examples above the method would really help people maintaining this years from now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}
}

private fun PsiElement.leftLeafIgnoringCommentsAndWhitespace(): PsiElement {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add examples code and what it means

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 76 to 78
// TODO(nickreid): What about:
// - function-type param lists
// - destructuring declarations
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not Meta style, so let's try to stick to something that's more applicable: Can you open issues and link to them from the TODO instead? You can have the issues assigned to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

result.replace(element.startOffset, element.endOffset, replacement)
}

return result.toString()
}

fun addRedundantElements(code: String, options: FormattingOptions): String {
// TODO(nickreid): Generalize this for addtional sugested elements.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment here re: TODOs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines +90 to +97
return kotlinCode
.let { convertLineSeparators(it) }
.let { sortedAndDistinctImports(it) }
.let { dropRedundantElements(it, options) }
.let { prettyPrint(it, options, "\n") }
.let { addRedundantElements(it, options) }
.let { convertLineSeparators(it, Newlines.guessLineSeparator(kotlinCode)!!) }
.let { if (shebang.isEmpty()) it else shebang + "\n" + it }
Copy link
Contributor

Choose a reason for hiding this comment

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

love it

Commas are added/removed according to the following rules:
  - Lambda param lists => remove
  - Single element lists => remove
  - Lists that fit on one line => remove
  - All other lists (multiline lists) => add
Copy link
Contributor

@strulovich strulovich left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +102 to +107
file.accept(
object : KtTreeVisitorVoid() {
override fun visitKtElement(element: KtElement) {
trailingCommaSuggestor.takeElement(element)
super.visitElement(element)
}
})
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think I understood your comment, I was suggesting:

file.forEachDescendantOfType<KtElement> {
  trailingommaSuggestor.takeElement(element)
}

@facebook-github-bot
Copy link
Contributor

@strulovich has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@nreid260
Copy link
Contributor Author

Ooooh, yeah that seems fine.

@facebook-github-bot
Copy link
Contributor

@strulovich merged this pull request in fa78077.

lancethomps added a commit to lancethomps/ktfmt that referenced this pull request Feb 2, 2024
* upstream/main:
  Bump version to 0.47-SNAPSHOT
  Bump version to 0.47
  Reformat Kotlin files
  Automatically manage trailing commas when running with --google-style (facebook#427)
lancethomps added a commit to lancethomps/ktfmt that referenced this pull request Feb 2, 2024
* upstream/main:
  Bump version to 0.47-SNAPSHOT
  Bump version to 0.47
  Reformat Kotlin files
  Automatically manage trailing commas when running with --google-style (facebook#427)
@nreid260 nreid260 deleted the trailing_comma branch April 23, 2024 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants