Automated semantic analysis and modification of Julia code

Is there a package or tool available that allows to automate the semantic analysis and modification of Julia code, e.g., in a file or in an entire module? I am thinking about use cases like where we have lots of code with both variables and functions prefixed with foo_ and I want to change this prefix to bar_ but only for variables. Or, e.g, I would like to find out how many local variables are defined that start with int_ etc.

Is there something available that already fits the bill (or to a large degree, at least) and if not, what would be a good way to do this? I realize that this is probably too complex a question to be answered in a Discourse thread, but I’d appreciate any pointers to similar packages or some keywords I can be looking for to figure it out myself.

1 Like

The Julia VSCode plugin has something like this in the “Rename Symbol” command, which does a semantic rename. Its not always perfect but usually surprises me how good it is, e.g. it gets this totally right regardless what you rename:

function foo()
    foo = 2
    foo
end

foo()

and works across multiple included files in a module.

3 Likes

Thanks for this hint. Would you happen to know how the plugin does this internally and whether it relies on a dedicated package or whether it’s deeply integrated in the plugin code somewhere?

the VSCode plugin uses CSTParser.jl, which is also used by Vimes.jl that does some mutation like you mention an interest in (although I don’t know if that package is maintained, unfortunately). JuliaFormatter.jl also uses CSTParser.jl, by the way.

2 Likes

Sorry for the late answer, but thanks a lot for the information on CSTParser.jl, I will look into it!

1 Like