fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.

Home Page:https://fzyzcjy.github.io/flutter_rust_bridge/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Let Rust Analyzer understand the marker proc macros

mezeipetister opened this issue · comments

Describe the bug

When I open the rust folder in VSCode (the project was generated by flutter_rust_bridge), in many cases I have no autocompletion from rust analyzer. Maybe this bug is related to somewhere else, but right now its really annoying working without any autocomplete.

VSCode output displays nothing, the same behavior after restarting my rust analyzer server.

Here is my simple.rs file:

use std::sync::{Mutex, OnceLock, RwLock};

static COUNTER: OnceLock<RwLock<i32>> = OnceLock::new();

#[flutter_rust_bridge::frb(sync)] // Synchronous mode for simplicity of the demo
pub fn greet(name: String) -> String {
    format!("Hello, {name}!")
}

#[flutter_rust_bridge::frb(sync)]
pub fn get_counter() -> i32 {
    let locked = COUNTER.get().unwrap();
    *locked.read().unwrap()
}

#[flutter_rust_bridge::frb(sync)]
pub fn increment() {
    let mut counter = COUNTER.get().unwrap();
    let mut c = counter.write().unwrap();
    *c += 1;

    let one: OnceLock<i32> = OnceLock::new();
    let m = Mutex::new(0);
}

#[flutter_rust_bridge::frb(init)]
pub fn init_app() {
    // Default utilities - feel free to customize
    flutter_rust_bridge::setup_default_user_utils();
    COUNTER.set(RwLock::new(0)).unwrap();
}

Working with the COUNTER static no autocomplete, if I create for example a mutex inside a function, no autocomplete for that at all.

Do you have any advice?

Steps to reproduce

Hint: A simple way to reproduce is to clone and modify the https://github.com/fzyzcjy/flutter_rust_bridge/tree/master/frb_example/dart_minimal example package according to your needs.

  1. ...
  2. ...
  3. ...

Logs

I have no error at all.

Expected behavior

No response

Generated binding code

No response

OS

No response

Version of flutter_rust_bridge_codegen

No response

Flutter info

No response

Version of clang++

No response

Additional context

No response

Hi! Thanks for opening your first issue here! 😄

If I remove the frb macro, everything works.

#[flutter_rust_bridge::frb(sync)]

Do you have any advice how to solve this issue?

If I remove the frb macro, everything works.

Hmm, then maybe it is because rust analyzer is not smart enough to understand the proc macros (though this proc macro does almost nothing - it is just a static marker).

One way may be somehow improve this proc macro. Since there is an ongoing PR related to this part #1676, maybe cc @Desdaemon - what do you think?

Btw, have you seen any macros (except for builtin ones) like such #[...], which makes your Rust analyzer still works well? If so, we can follow their best practices.

Or, maybe we can create a post to ask about this in the rust forum.

Another way may be add support for some comment-based syntax, like /// frb: sync (similar to cbindgen's way IIRC).

(This does not look like a bug, since it is known that rust analyzer sometimes gets confused about proc macros, so I relabel it. Feel free to relabel if you think not proper!)

Yeah #1676 would fix this issue, since span info is currently being tossed out when unparsing a TokenStream.

@Desdaemon That looks great! I edited the comment to link to this

Thank you for your super fast response. For now it seems to me a tiny workaround solves this issue:

fn _get_counter() -> i32 {
    *COUNTER.get().unwrap().read().unwrap()
}

#[flutter_rust_bridge::frb(sync)]
pub fn get_counter() -> i32 {
    _get_counter()
}

If I remove the frb macro, everything works.

Hmm, then maybe it is because rust analyzer is not smart enough to understand the proc macros (though this proc macro does almost nothing - it is just a static marker).

One way may be somehow improve this proc macro. Since there is an ongoing PR related to this part #1676, maybe cc @Desdaemon - what do you think?

Btw, have you seen any macros (except for builtin ones) like such #[...], which makes your Rust analyzer still works well? If so, we can follow their best practices.

Another way may be add support for some comment-based syntax, like /// frb: sync (similar to cbindgen's way IIRC).

(This does not look like a bug, since it is known that rust analyzer sometimes gets confused about proc macros, so I relabel it. Feel free to relabel if you think not proper!)

Comment based syntax looks awesome.