Autocomplete for default parameters

I tried using AppCode as my Swift IDE and I was surprised at how autocomplete offers multiple suggestions when there are default parameters in a function. It's so good. Is this related to SourceKit-LSP and there any plans to implement something like that?

The default behaviour in SourceKit today is to show two completions for each function that has default arguments: one with none of the default arguments, and one with all of them.

@rintaro

A slightly off topic, slightly ranty dream I have.

Xcode's current autocomplete behavior for defaulted values is one of the biggest sources of frustration, code pollution, and user confusion in Swift today. Some of its issues are part of Xcode's autocomplete behavior in general, but much of it comes down to the fact that Xcode never adapted to Swift's new capabilities here.

  1. Autocomplete frequently regresses between Xcode releases, even minor ones. Xcode 13 significantly regressed the fuzzy matching behavior so that previous completions aren't possible anymore. Additionally, some fuzzy results actually disappear and then reappear as you type which, as I understand fuzzy matching, should never really happen.
    0.1. Please allow us to ignore modules in the autocomplete. I'm pretty much never going to need anything from Darwin, or anything from libc, or any of the system frameworks, so turn them off by default! With Xcode's new ability to automatically import (which, while appreciated, is not a great implementation of that feature), it could automatically turn on those libraries if we really wanted them. But don't show every possible result by default! Not only is it visual pollution but it's probably the biggest cause of performance issues for that UX.
  2. While the posted screen shot at least allows the various combinations of defaulted values, I don't find the UX to be very good. What I'd like to see is the full method listed, with all of the possible completion combinations underneath to indicate that they're the same function.
  3. Allow fuzzy completion to complete some of those defaulted parameters as well, especially when I select the method in general. I shouldn't have to move my hands to further refine the suggestions.
  4. Please, please allow me to tab complete individual parameters in Swift methods! This has been broken since day one of Swift, and it's a huge regression over Obj-C, especially since Swift enables much longer function signatures to be ergonomic.
  5. Allow me to use the arrow keys to select a particular parameter! I shouldn't have to insert and then delete a bunch of parameters just to get the one I want.
  6. See the "insert ... replace" text in the screenshot's dialog? Xcode desperately needs that for Swift. If I have to to delete five additional parameters again because I wanted to insert a single, non-default parameter, I'm going to lose it. So much wasted time comes from dealing with Xcode's unaware code insertions. (This ignores the fact that Xcode desperately needs an automatic, extensible formatter to format those autocompletion insertions.)
  7. If I put my cursor in a function to add more to the name and select an autocomplete result, don't insert more parameters or parentheses! Xcode really needs to be aware of the context around where it's inserting suggestions.

In the end I'd take just presenting the same interface posted in this thread. So many, many times I see users select the first completion result, which just happens to be the full one, or the result with the one parameter they need, which then forces them to fill in every. single. parameter. when then don't need them. Not only does this lead to bad code, it leads to poor runtime behavior, as users provide suboptimal defaults for parameters they don't understand. Every year I hope that his has been fixed. Finally adding fuzzy matching years after an Alcatraz plugin already did was good, and the auto import was sort of nice (though too eager, I should have to work more for invisible results), but Xcode really, really needs to bring its autocomplete capabilities inline with virtually every other IDE. Please!

9 Likes

Another improvement would be to allow for API authors to indicate the order of completions in a given function context (or even omit the auto complete… like for things like .none to optionals). I am not sure how that all could be spelled but it could be a powerful way of indicating hints to API users.

2 Likes