rf: rename all variables with matching name
One thing I would like to do in my refactoring is change a local variable naming convention. e.g., in package runtime, rename all variables of type *m with name _m_ to mp.
This can obviously already be done one-by-one (e.g., rf 'mv canpanic._m_ canpanic.mp'), but I still need to manually identify all instances to construct mv commands. Ideally rf could help make this easier.
The first question is whether this is even a feature that makes sense for rf? To me it seems nice if it can fit in nicely. My use-case is renaming the final remaining uses of old naming conventions in the runtime that are left over from the C-to-Go conversion. Another theoretical use-case I could imagine is a codebase that has been naming their context.Context variables c, and now wants to change to the more standard ctx.
Secondly, how should this be expressed? A few approaches I've considered:
ex command
ex {
named var _m_ *m
named var mp *m
_m_ -> mp
}
This is actually the first thing I tried (without the named keyword) before realizing that the names in an ex command are relevant only for references within the block. For pattern matching, only the types matter. Thus is example (without named) would in theory run but be a no-op since the types are the same.
The named keyword would add the variable name as part of the pattern matching key (i.e., only match variables of type *m that are named _m_). mp won't already exist in the matched program, so it might also need another keyword (implicit?).
This approach is nice because it explicitly takes type into account. In the context.Context case this could be particularly important, as there are likely to be other unrelated variables named c.
mv -all command
Right now, mv takes the addr to change, and an addr uniquely defines a single thing in the package (right?). A fuzzy addr could match all instances of something. e.g., mv -all _m_ mp or perhaps mv *._m_ *.mp matches all addrs that end in _m_ and renames them in the same scope.
This is perhaps simpler to write for simple renames, but doesn't take type into account, which is unfortunate.
list helper
Perhaps rf shouldn't even try to directly support this case, but could instead provide an rf -list helper that lists the addr for all identifiers, plus their kind (Func, Type, etc) and type (if applicable). A simple script could then be used to filter this output to look for all instances of _m_ I care about and construct a set of trivial mv commands to perform the renames.
A quick comment: list seems highly desirable (and is something I had been wishing for... 😅 )
A simple example is if you want to take a large .go file or package and break it into separate files or packages, it would be nice to easily get a list that you then manipulate in an editor or script to do the mv commands you want, and in general make it easier to get the raw materials to then optionally manipulate and feed back into the rf workflow.
Of course, there are other ways to do something like that, but having an address-aware list command seems like a very nice general purpose enhancement, and seems it would be a natural fit.
I'd probably start with just adding a new command like rename FromIdent ToIdent [TypeSpec] (e.g., rename _m_ mp *m). We can always revise the functionality later.