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

Closing parentheses placement for method calls does not match Kotlin style guide #29

Closed
cushon opened this issue Apr 15, 2020 · 11 comments
Labels
formatting-discussions Discussions about how formatting should look like, when there's no clear consensus.

Comments

@cushon
Copy link

cushon commented Apr 15, 2020

This came up in #10, but IIUC the changes made in that issue were only for method declarations, not method calls.

See e.g. https://kotlinlang.org/docs/reference/coding-conventions.html#method-call-formatting:

drawSquare(
    x = 10, y = 10,
    width = 100, height = 100,
    fill = true
)

One reason this might be worth reconsidering is the support for trailing commas that was added in Kotlin 1.4 (see KT-9476), which will allow writing:

 drawSquare(
     x = 10, y = 10,
     width = 100, height = 100,
-    fill = true
+    fill = true,
 )

Leaving the closing paren ) on the previous line seems like bad style:

 drawSquare(
     x = 10, y = 10,
     width = 100, height = 100,
     fill = true,)
@cushon
Copy link
Author

cushon commented Apr 15, 2020

Another related question is what to do about chained method calls if there's a line break before ).

Having a line break and continuation indent after . looks weird and takes up more vertical space. It might be better to have something like the following, but it would require some rectangle rule special-casing:

foo(
    bar,
).baz(
    bork,
)

@cgrushko
Copy link
Contributor

cgrushko commented Apr 28, 2020

This is the existing formatting:

func f() {
  computeBreaks(
      output.commentsHelper, maxWidth, State(0))
…

Which one do we prefer?

First option wastes vertical space:

…
  computeBreaks(
      output.commentsHelper,
      maxWidth,
      State(0)
  )
…

Second option looks weird:

…
  computeBreaks(
      output.commentsHelper, maxWidth, State(0)
  )
…

I don't like either of the options, but the first one seems more sane.

EDIT: For method parameters, we use the latter.

@cushon, @strulovich, what says you?

@strulovich
Copy link
Contributor

In any case, for a trailing comma we will do:

func1(
    arg1,
    arg2,
)

But I'd prefer keeping it for now as:

func1(
    arg1,
    arg2)

I'm against wasting vertical space. So if all args fit in one line I would keep them there.

Is any of these a deal breaker for anyone?

@cgrushko
Copy link
Contributor

Do you mean we output different formatting based on whether a trailing comma exists?

@strulovich
Copy link
Contributor

As long as we don't adjust the actual tokens (which for now we are not) I think that would be ok.

@strulovich
Copy link
Contributor

Doing round on some old issues today. I prefer same line if if there's no trailing comma, and next line if there is one. We do not add or remove commas for now.

@cushon, how does that sound to you?

@andrewparmet
Copy link

My vote is a new line, wasting the vertical space. IMO the consistency is what I value.

@hick209 hick209 added the formatting-discussions Discussions about how formatting should look like, when there's no clear consensus. label Jul 30, 2020
@bethcutler
Copy link
Contributor

At Google, we are using the first option (all parameters on separate lines, closing parens on its own line). This is also the style that ktlint enforces (all parameters must be on separate lines, unless they can all fit on the same line as the function name).

The closing parens being on its own line is also key to having no continuation indents, as recommended by both Intellij and Android style guides. (See also discussion at #14 )

@JavierSegoviaCordoba
Copy link
Contributor

I like the approach of trailing lambdas to force multiline params even if it fits in one line.

But I would like to force in a builder pattern to use multiline even if it fits in one line because of readability.

The only approach now is this?

fol.Builder().apply {
    bar()
    baz()
}

@cgrushko
Copy link
Contributor

cgrushko commented Jan 6, 2021

@JavierSegoviaCordoba I'm hesitant to introduce special formatting cases such as builder patterns. First, because I'm concerned it'll complicate the code and make it less maintainable, and second because it might be difficult to correctly identify the pattern.

@hick209
Copy link
Contributor

hick209 commented Sep 15, 2023

Closing this as it's an old issue and we currently by using trailing commas the closing parenthesis will wrap as suggested in the beginning of this discussion

@hick209 hick209 closed this as completed Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatting-discussions Discussions about how formatting should look like, when there's no clear consensus.
Projects
None yet
Development

No branches or pull requests

7 participants