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

Trailing comma gets removed if there is only one parameter and it is formatted to be on its own line #461

Open
hongping-quora opened this issue Apr 27, 2024 · 2 comments

Comments

@hongping-quora
Copy link

Using the ktfmt IntelliJ plugin 1.1.0.47 in Android Studio, setting "Code style" to "Google (internal)"
image
image

  1. The following code with a single parameter will get the trailing comma removed.
import android.content.SharedPreferences

private class AllowlistedDiskReadsSharedPreferences(
  private val wrappedPreferences: SharedPreferences,
) : SharedPreferences by wrappedPreferences
  1. This code with two parameters will not.
import android.content.SharedPreferences

private class AllowlistedDiskReadsSharedPreferences(
  private val wrappedPreferences: SharedPreferences,
  private val wrappedPreferences2: SharedPreferences,
) : SharedPreferences by wrappedPreferences 

Since one of the reasons of using trailing comma is to avoid having to modify an existing line when adding another parameter (https://kotlinlang.org/docs/coding-conventions.html#trailing-commas), perhaps even when there is only one parameter but it has to be on its own line, it should also have a trailing comma?

Thanks!

@hick209
Copy link
Contributor

hick209 commented May 3, 2024

@nreid260, do you think this is a bug or within your expectations?

@nreid260
Copy link
Contributor

nreid260 commented May 3, 2024

This is working as intended, at least for now. The Google Kotlin style committee preferred that single-element lists never got trailing commas. However, there's no technical reason to do this, only aesthetic, so they could be convinced otherwise.

One technical constraint was that I explicitly wanted to remove the "hinting" behaviour or trailing commas. --google-style totally manages trailing commas, based on the rest of the code. If you really want to force a line-break, // is still available.

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

No branches or pull requests

3 participants