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

Configuration option for auto-rewrite of trailing closures #1787

Closed
woolsweater opened this issue Oct 27, 2024 · 2 comments
Closed

Configuration option for auto-rewrite of trailing closures #1787

woolsweater opened this issue Oct 27, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@woolsweater
Copy link
Contributor

woolsweater commented Oct 27, 2024

Description

Context

Change #1072 introduced automatic expansion of trailing closures. For example, when requesting completion for a call to the function func foo(bar: Bar, baz: @escaping () -> Void) sourcekit-lsp manipulates the function template to produce this (using Xcode placeholder syntax to avoid confusion with closure shorthand args):

foo(bar: <#Bar#>) {
  <#Void#>
}

While this multi-line expansion may be appropriate for callback-based (Timer.scheduledTimer) or scope-defining (DispatchQueue.async, String.withUTF8) functions, it is strictly worse in my opinion for simple predicate-based calls. Consider someArray.filter: this expands to three lines with two placeholders:

someArray.filter { <#Foo#> in
  <#Bool#>
}

But more often than not I was just going to type a short single-line expression like someArray.filter { $0.bar() == baz }, or even to use a key path: someArray.map(\.bar). Even for a callback it is not uncommon to use a named function reference rather than a literal: foo(bar: someBar, baz: handleBaz).

It is much more work now to correct the multi-line expanded form to the shorter version than it was to expand a callback-based method from foo(bar: <#Bar#>, <#() -> Void#>).

Proposed Change

Whether to expand trailing closures at all, let alone to multiple lines, is largely a stylistic decision; as such the language server should permit user control over it. A field should be added to the configuration options allowing this feature to be turned off (and, perhaps, to allow reducing its intensity: cf. #1788).

@ahoppen
Copy link
Member

ahoppen commented Oct 27, 2024

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

@ahoppen
Copy link
Member

ahoppen commented Jan 12, 2025

As discussed in #1789, we don’t want a configuration option here at the moment and you implemented a great middle-ground solution in #1831. So let’s close this issue.

@ahoppen ahoppen closed this as completed Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants