Finding places where a method of a trait is used (using Rust Analyzer, or otherwise)


I'm working on a PR for a codebase that I don't know. I need to change certain places where methods of a certain trait are used.

Is there a way to find those places automatically? That would make my job much easier... :wink:

rust-analyzer has Structural Search & Replace (SSR), which might be able to handle this case:

Thanks, @jschievink. This would work for simple replacements, it seems. In my case, I need to find these places, and then change the code by hand as needed. It seems, Structural Search and Replace can only replace what is found (not merely show the locations).

I usually right-click on the symbol to look for and select Find All References when using rust-analyzer. I think it's also bound to Alt+Shift+F12 by default.

If you are just renaming the function and not touching anything else you can right-click and select Rename Symbol (F2). That won't work if you also change how the function is called though (add args, etc.).

Of course, you can always make the change, let the compiler emit a bunch of compile errors, then use the Go To Next Problem function built into VS Code (Alt+F8). It doesn't do replacement for you, but I'd normally record a macro using the VS Code Vim plugin and let Vim do replacement.

Thank you, @Michael-F-Bryan. The problem is, that there are many methods in this trait. When I execute Find All References on the imported trait, only for the trait symbol itself is search (it seems).

I ended up creating a search pattern for ripgrep:

rg '\.(fg|bg|color|on_color|truecolor|on_truecolor|bold|dimmed|italic|underline|blink|blink_fast|reversed|hidden|strikethrough|on_|bright_|black|red|green|yellow|blue|magenta|purple|cyan|white)'

That will hopefully find everything... :slight_smile:

It looks like there is a merged PR for a "Structured Search" feature. But for some reason, it's not available is the VSCode plugin. I've created a feature request, in case the feature was forgoten, or something like that.

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.