IntelliJ/Rust : jump to a crate (in workspace) that uses this enum/struct

Suppose we have the following layout:

crate car_base:
  pub struct Car { ... }

crate car_collision:
  depends on crate car_base
  provides: ...::calc_collision(car: &Car, ...)

crate draw_car:
  depends on crate car_base
  provides ...::draw(&self, some_config, car: &Car)

crate paint_car:
  depends on crate car_base
  provides ...::paint_texture(&self, car: &Car)

Note here: the crates car_collision, draw_car, paint_car all depend on crate car_base

Question: In crate car_base, is there anyway to place some type of comment or something so that I can jump to relevant functions in car_collision, draw_car, paint_car ?

I.e. I want , in crate car_base

pub struct Car {}
// link to jump to particular func in crate car_collision::....
// link to jump to particular func in crate draw_car::...
// link to jump to particular func in crate paint_car::...

The problem here is that this is "inverted", foo is NOT jumping to something foo depends on; foo is jumping to something that depends on foo.

I'm using IntelliJ / Rust plugin, and for the life of me, I can't figure out a way to place these "file/func references"

Right now, the best I can do is put the cursor over "struct Car" and hit "goto impl / usages", which is not nearly as useful

There is nothing in Rust itself to create a mention of something in a downstream crate.

You could add a function which does nothing and is called in the locations of interest, so that viewing usages of that function is a relevant short list. Or, simply add a unique keyword in comments and a note telling yourself to text search for it.

1 Like

So /// (three on purpose) is a special type of comment right? With special comments, is there away to specify something of the form crate_name::module_path::func_name and have IntelliJ able to jump to it if it is in the same workspace?: That would be ideal for my situation.

So /// (three on purpose) is a special type of comment right?

Right - these are "doc comments" (https://doc.rust-lang.org/reference/comments.html#doc-comments).

With special comments, is there away to specify something of the form crate_name::module_path::func_name

No, I'm afraid that intra-doc links only support items in scope (see: https://doc.rust-lang.org/rustdoc/write-documentation/linking-to-items-by-name.html) and thus can't work for stuff that is downstream.

@ozkriff : IIRC, you're in charge of the IntelliJ/Rust plugin posts.

Are you saying you do not know of any way to do this ?

If you are claiming this is impossible, I'm going to stop wasting time searching and instead focus on writing a mini plugin in scala.

Pretty much all I want is to be able to put cursor over something of the form:

// crate_name::module_path::func_name/struct_name/enum_name

hit <leader>gd and jump to the definition, even for things not in current crate, but within the workspace

I can't guarantee that there's no way to do what you want but, yeah, I personally don't know any.

I'm going to stop wasting time searching and instead focus on writing a mini plugin in scala.

It may be easier to fork https://github.com/intellij-rust/intellij-rust and add this functionality (as an experimental feature?) since ijrust already has an internal index of all items in the workspace and you won't need to do any rust parsing yourself.

1 Like

Is there a way to follow doc comments in Rust ?

pub struct Foo {}

/// [`Foo`]

When I put the cursor over the Foo (in the doc) and hit goto def, it does not jump to the struct Foo.

This rustdoc syntax is called "intra-doc links" and, as I've said in a previous thread, it isn't properly supported in ijrust yet:

Thank you for repeating this. I think it is finally starting to make sense now.

In IntelliJ Rust plugin code, do you know which function does String -> (String, i32) where it takes as input a fully qualified name, say "foo::bar::CarBase" and then it returns the filename + line_number where it is defined ?

If I have this primitive, the Scala code is not too bad. It looks at the cursor, goes forward until first space, backward until first space, then passes that to said function, and jumps.

The closest thing that I know of is resolveStringPath that accepts a full path and returns RsNamedElement that you can navigate to.

1 Like

The thing you want (specify information about crates that depend on yours) goes very much against both the IntelliJ Rust's project model, and against the way Rust itself works. If such a feature were added, it would be very incompatible with everything else in the ecosystem. So the direct answer to your question is: there is no such feature, and I would be surprised if it were added.

One option is to search for usages of Car. This will include all functions, which mention Car in its arguments or return type. What you want looks like usages of the type in argument position, scoped to other crates. The plugin certainly can do that, and if your group results by Directory in the usages presentation settings, you can easily see which of the usages are in downstream crates.

The grouping by Usage Type, unfortunately, doesn't work well enough at the moment, so there is no easy way to filter just usages in function arguments.

A more rusty way would be to use traits. You can define a trait in car_base, which would define some class of actions. Downstream crates would implement that trait, and you would be able to quickly search for implementations. Alternatively, if there is no sensible trait to group all of calc_collision, draw and paint_texture, you could instead declare traits in downstream crates, and implement those traits for Car. Then you would be able to quickly see all trait implementations for Car by using the gutter icon near its definition.

1 Like

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.