Fn/FnMut template parameters

Hi all,

When I use the rust-analyzer from emacs (with the eglot lsp client) for a function like a .map I tend to expect that it will fill in the template for me based on the type of the thing that I'm mapping over. e.g. if I have a HashMap and I .map over it, I expect the auto-filled template to give me

.map(|(a, b)| _ )

where I would be asked to enter the a, b and finally _ values (tabbing as I finish each, in typical emacs completion style). This is how autocomplete works in other typed languages with lambda functions, such as Scala.

However, in Rust, the template is only ever

.map(f)

which means I need to manually open the | then the parameter list, then the closing |. Obviously this is because it's doing exactly what is expected of the verbatim function

    fn map<B, F>(self, f: F) -> Map<Self, F>
    where
        Self: Sized,
        F: FnMut(Self::Item) -> B,
    {
        Map::new(self, f)
    }

but wouldn't it be cool if the analyzer was able to detect this (very common!) case and go one step further, expanding out on | Self::Item | instead of the f: F. It would already be of great benefit if the opening and closing | characters were inserted, without looking further into the Item to see if it is a tuple, as I tend to always expect Emacs to close parents off for me (old habits die hard).

Is there a setting for this that I've overlooked in User Manual or will I just have to wait for it to be implemented? I don't really want to make a feature request because the backlog is already almost 2k issues on github and I'm sure this will just get lost in the noise.

I thought this might be a dupe of Rust-analyzer autocompletes functions with an ellipsis instead of parameters but it's not, even setting

analyzer.completion.fullFunctionSignatures.enable to true as per the user manual, like so in emacs...

  (add-to-list 'eglot-server-programs
               '((rust-ts-mode rust-mode) .
                 ("rust-analyzer"
                  :initializationOptions
                  (:checkOnSave :json-false
                   :diagnostics (:enable :json-false)
                   :completion (:fullFunctionSignatures (:enable 't))))))

only enables

Whether to show full function/method signatures in completion docs.

but not for method completions.

It looks like there is a setting for this afterall, completion.callable.snippets which can be set to "fill_arguments" (default), "add_parentheses", or "none". But fill_arguments just doesn't work when the input parameter has a trait bound to be a function rather than just being an explicit function.

BTW it's also worth checking out completion.snippets.custom which show up in the completions list (although arguably yasnippet is better placed for this sort of thing).

1 Like

This is Complete closure expression by expected type · Issue #8676 · rust-lang/rust-analyzer · GitHub. It's not implemented because it's pretty hard.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.