Stream: t-compiler/rust-analyzer

Topic: get syntax tree with metadata

Benedikt Mandelkow (Jul 14 2020 at 20:23, on Zulip):

Hello I would like to write a tool which parses a rust crate and generates metadata about fn/structs etc.. This should include the places they are used and at the place of their use I'd need to figure out where they were defined. So I basically would like an iterator over all items in a file and get their source location/ comments/ type/ metadata if applicable or else get where they are used. I think using rust analyzer this should be fairly easy and would make things like "go to definition" on sourcehut possible . I tried the ra_ide crate and ra_syntax crate but could not quite find the right part of the API. As far as I can see my use case is a bit unusual for RA because its kind of greedy whereas RA is more lazy/ on demand so if its not really supported API that would be a helpful answer as well. Thanks!

Mikail Bagishov (Jul 14 2020 at 23:31, on Zulip):

AFAIK you should look at ra_hir, it provides name resolution, type inference and so on.

Benedikt Mandelkow (Jul 15 2020 at 11:34, on Zulip):

So what I think I can do is something along the lines of using ra_syntax to iterate over all tokens or some other nodes of the tree and for the ones that are interesting (like e.g. function, struct definitions and uses) invoke ra_ide to get the information for that position in the file but maybe there is an existing iterator-like thing I'm missing that would make this easier.
For ha_hir I was not sure how to deal with the databases/ construct them/ there are a few of them. My approach was whenever I saw some struct I wanted to use, search where it was returned and this did not yield the information I wanted in these cases.

Florian Diebold (Jul 15 2020 at 12:20, on Zulip):

Using ra_syntax and ra_ide in that way is probably the right approach

Florian Diebold (Jul 15 2020 at 12:22, on Zulip):

you could also take a look at the syntax highlighting code ( for an example of something similar

Florian Diebold (Jul 15 2020 at 12:24, on Zulip):

and at for an example of doing semantic analysis on a crate, but you're probably better off with the IDE level abstractions unless you want to do something special that they don't provide

Florian Diebold (Jul 15 2020 at 12:25, on Zulip):

you get a database by calling raw_database() on an AnalysisHost

Benedikt Mandelkow (Jul 15 2020 at 14:52, on Zulip):

Ok thanks!

Last update: Jul 29 2021 at 22:15UTC