Skip to content

Rust Analyzer's auto-completion is weakened by the #[component] macro #1781

Closed
@pikaju

Description

@pikaju

Describe the bug

The #[component] proc macro makes Rust Analyzer less capable at auto-completing code when a syntax error is present. Consider the following example code:

#[component]
pub fn Example(cx: Scope) -> impl IntoView {
    let // <-- Causes a syntax error

    let (value, set_value) = create_signal(cx, initial_value);

    set_value. // <-- Place your cursor after this dot and try auto-completing with Rust Analyzer.
}

When placing the cursor in the specified location, r-a does not give me auto-complete suggestions. However, if I remove the #[component] attribute, r-a is smart enough to provide suggestions, despite the syntax error in line 3.

Explanation

I believe this issue occurs because of the way the component proc-macro is implemented. It uses the syn crate to parse the whole function body and then serializes it again. If an error occurs during parsing, the proc-macro will exit with only a compile error, and no actual code.

How it could be better (I think)

Rust Analyzer can work with code that is emitted from a proc-macro even if it is prefixed or followed up with a compile error. In the case of this (relatively) simple attribute macro, it would be sufficient to emit the input token stream in the case of a parsing error. Since I've noticed you are using the proc_macro_error crate, this could be done with

proc_macro_error::set_dummy(input)

at the beginning of the component macro. This might even remove the necessity to output the error generated by syn for the function body.

See also:

Rust Analyzer setup

I use the following Neovim configuration for Rust Analyzer. Notably, I have macro expansion enabled:

lspconfig.rust_analyzer.setup {
  cmd = { "rustup", "run", "stable", "rust-analyzer" },
  on_attach = on_attach,
  capabilities = capabilities,
  settings = {
    ["rust-analyzer"] = {
      procMacro = {
        enable = true,
      },
      checkOnSave = {
        command = "clippy",
      },
    },
  },
}

Leptos Dependencies

leptos = "0.4.10"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions