rust-lang / rust-lang/rust-analyzer

postfix completion on a closure could probably work better.

Open
#11,922 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-completion S-actionable
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

consider the following code:

let x = || {};

This defines x as a closure that returns nothing. However, when doing:

let x = || {}.box;

and selecting the box postfix completion, currently, rust-analyzer does the following:

let x = || Box::new({});

However, what we probably want to do in this case is actually:

let x = Box::new(|| {});

Wonder if we should special case this. We could probably see if the direct parent to the BLOCK_EXPR is a CLOSURE_EXPR and pass that as the receiver instead?


I tried to take a stab at this, but ran into the issue where the AST when the . exists ends up being:

            CLOSURE_EXPR@12..18
              PARAM_LIST@12..14
                PIPE@12..13 "|"
                PIPE@13..14 "|"
              WHITESPACE@14..15 " "
              FIELD_EXPR@15..18
                BLOCK_EXPR@15..17
                  STMT_LIST@15..17
                    L_CURLY@15..16 "{"
                    R_CURLY@16..17 "}"
                DOT@17..18 "."

We would need to to fix this up a bit by creating some fake syntax so that it looks like this for the receiver:

            CLOSURE_EXPR@12..18
              PARAM_LIST@12..14
                PIPE@12..13 "|"
                PIPE@13..14 "|"
              WHITESPACE@14..15 " "
              BLOCK_EXPR@15..17
                STMT_LIST@15..17
                  L_CURLY@15..16 "{"
                  R_CURLY@16..17 "}"

Not quite sure how to do that yet, though 🤔 - any pointers for where to look?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in rust-analyzer's postfix completion handling and inspect how the CLOSURE_EXPR, BLOCK_EXPR, and FIELD_EXPR AST shape is used when the dot is present. Determine how the receiver syntax can be adjusted so the closure, rather than its block, receives the box completion; done means the example produces Box::new(|| {}) and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.