Help building a code browser

I'm toying with building a Rust code browser in Rust/wasm32.

We can assume that the to-be-browsed code builds fine under cargo build.

The feature I want most is jump to definition / jump to usage.

So if we have something like:

pub struct Foo {
  a: ...
  b: ...

pub fn main() {
  let c: Foo = ...


then putting the cursor over the c of c.b should jump us to the let c: Foo = ..., and over the b of c.b should jump us to b: ....

This is nothing special, IntelliJ has this already.

My question is: do any existing open source crates do this? I want this data for all identifiers in all *.rs files in all crates. [I want to dump this data, not query it at runtime.]

Is there a simple (and fast) way to acquire this? I want something fast enough I can auto run after every successful cargo build. (so perferably it should not take much longer than a cargo build itself)


You can use rust-analyzer for this.

Either spin up the server and interact with it like a normal language client or use the ra_ap_hir crate to interact with it like a library.

The ra_ap_hir approach will probably be the most fruitful (you'll probably start at ra_ap_hir::Crate), but it requires more effort and a bit of understanding around rust-analyzer's architecture. I think you should be able to start things by creating your own database type and calling ra_ap_base_db::SourceDatabase::set_crate_graph() to tell it where your crates are.

A nice thing about this approach is you can keep the database instance around between builds (e.g. by making a long-running server that you ping after every build or change) and rust-analyzer's query caching will mean it doesn't need to re-analyze your entire dependency tree.


I hope this does not come off as entitled: Do you have sample code ? It does not have to do what you describe above, it just needs to use ra_ap_hir and does something .

I googled around, and not only could I not find sample code, I could not even find the github repo of ra_ap_hir.

Does that perform type checking? I don't think the requirement to discover a type definition based on a field access can be achieved without type checking.

No, ast-grep only searches ast nodes through tree-sitter without knowing the types.

The ra_ap_hir crate is part of the rust-analyzer repo.

Rust-analyzer has a pretty comprehensive test suite and a lot of the tests will run the entire pipeline. You should be able to find some good examples in there.

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.