Use Rust Analyzer API to generate robust completions

Open
#71 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
rust

Research direction

Start with the experimental implementation in bon-macros/src/util/ide.rs and research the rust-analyzer syntax APIs and the proposed cfg(rust_analyzer) dependency setup. Determine how incomplete macro input, including struct-field and function-argument attributes, can be parsed. Done means completions and required use statements are generated for all attributes while remaining aligned with the real macro implementation.

Written by the indexing model from the issue text.

Description

feature request

Context

I wrote some experimental code to generate completions for the builder macro here. It was showcased in the 2.0 update blog post.

However, the current impl is rather limited. It does some custom manual parsing trying to be resilient to incomplete/errored syntax. The goal for this effort is that the macro must be able to parse any incomplete syntax and always generate the use statements with completions for all attributes passed to the macro. This must include attributes on struct fields and on function arguments (current dirty impl ignores those).

Summary

The idea is to use rust-analyzer's APIs (RA publishes its crates to crates.io) to parse the tokens handed to the macro instead of using the custom impl I wrote, which is very limited. The RA crates need to be added as dependencies under cfg(rust_analyzer) condition only. For example:

[target.'cfg(rust_analyzer)'.dependencies]
ra_ap_syntax = "=0.0.232"

The cfg(rust_analyzer) is an internal CFG used by RA when it analyzes Rust's code. We can work with RA maintainers to guarantee this CFG is stable, or discuss alternatives (maybe use a more generic name for the CFG).

This effort requires a lot of research and the nuances are undiscovered yet. I'm not yet sure how hard this will be to use RA to provide good completions for users, and also keep it in sync with the real macro implementation.

Dominant language
Rust
Stars
2.1k
Forks
46
Avg merge
53m
Merged PRs (30d)
5

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.

More from elastio/bon

All issues in elastio/bon

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.