swiftlang / swift-format

Formatting technology for Swift source code
Apache License 2.0
2.38k stars 216 forks source link

Wrapping of long function signatures #725

Open ahoppen opened 2 months ago

ahoppen commented 2 months ago

Formatting this to 80 columns

func foo(someArgument: Int, andAnother: Int, andYetAnother: Int) -> Array<Substring> {
}

// produces

func foo(someArgument: Int, andAnother: Int, andYetAnother: Int) -> Array<
  Substring
> {}

Which is definitely not the most intuitive way of formatting it. I would have expected

func foo(
  someArgument: Int,
  andAnother: Int,
  andYetAnother: Int
) -> Array<Substring> {}

In a less extreme case, I would also expect the following to wrap the argument list instead of the return type

func foo(someArgument: Int, andAnother: Int, andYetAnother: Int, and: Int) -> Substring {}

// produces

func foo(someArgument: Int, andAnother: Int, andYetAnother: Int, and: Int)
  -> Substring
{}
func foo(someArgument: Int, andAnother: Int, andYet: Int, and: Int) -> Substring {}

// produces

func foo(someArgument: Int, andAnother: Int, andYet: Int, and: Int) -> Substring 
{}
ahoppen commented 2 months ago

Synced to Apple’s issue tracker as rdar://126967203

allevato commented 2 months ago

IIRC, the reason for this was that we wanted to allow tuple return values to break after the open paren, like:

func foo(someArgument: Int, andAnother: Int, andYetAnother: Int) -> (
  element: SomeType,
  element2: SomeOtherType
) {}

So we never grouped around the return type at all. We should probably use more knowledge of the return type's node kind here to determine whether we want to wrap it in a group or not.


In a less extreme case, I would also expect the following to wrap the argument list instead of the return type

This was the original behavior, which someone else at Apple (I think, it was years ago) argued was too aggressive. 🙂 It can be enabled by setting prioritizeKeepingFunctionOutputTogether in the configuration.

ahoppen commented 2 months ago

Oh, I didn’t know about prioritizeKeepingFunctionOutputTogether. Nice

ahoppen commented 2 months ago

Enabling prioritizeKeepingFunctionOutputTogether for the sourcekit-lsp codebase caused an assertion failure, I filed https://github.com/apple/swift-format/issues/726 for that.