Rename a symbol from the command-line for an automated fix of multiple commits

I'm wondering whether it's possible to rename a symbol in a stack of commits using rust-analyzer and git-branchless's git test fix or a similar tool. What is the best way to do so? Presumably, I'd want to tell rust-analyzer to rename an identifier from the command line or from a shell script.

I looked at the docs a little, and it seems like I might have to start rust-analyzer and send an LSP request to it. I'm not sure what the request should be exactly, but it seems like it involves calculating the cursor position for the desired symbol.

Is this really the best one can do, or is there a way to (for example) refer to the symbol by name? In any case, are there docs, examples, or other pointers on how to do it?

Thank you!

P.S. Just for completeness, another tool that seemed promising was https://ast-grep.github.io/. It's based on Treesitter, so it could work in many cases, but AFAICT it wouldn't know about anything beyond what a parser can tell, e.g. if symbols of the same name shadow each other or whether symbols of the same name in different files refer to the same thing or not.

Update: Another sort-of-an-option I found is Coccinelle for Rust - Rust for Linux. It is rust-analyzer based but is under-documented and seems to have been abandoned some months ago.

I responded to Ilya's question in the jj Discord. I'll reproduce it here:

The rust-analyzer ssr subcommand might be what you're looking for. It works, but it has a few rough spots in terms of error reporting. This is the help output:

rust-analyzer ssr

  ARGS:
    <rule>...
      A structured search replace rule (`$a.foo($b) ==>> bar($a, $b)`)

Here's some additional documentation: User Manual and an issue discussing next steps.

2 Likes

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.