Scripting opens up an entire new world for working with ProRefactor and the syntax tree. As of version 1.7.2, the Groovy scripting engine has been added to ProRefactor's bundled libraries.
A menu item has been added to the context menu on the Navigator view: ProRefactor -> Tool Devel Utils -> Groovy Console. If you have a .p, .w, or .cls file selected when you launch the Groovy console, then within your console the File variable selectedFile
is available for your script.
For example, you could select a .p, launch the Groovy Console, and then do something like this:
import org.prorefactor.core.JPNode import org.prorefactor.core.TokenTypes import org.prorefactor.treeparser.ParseUnit indent = 0 def walker(node) { if (!node) return println ' '*indent + TokenTypes.getTokenName(node.getType()) + ' ' + node.getText() indent++ walker(node.firstChild()) indent-- walker(node.nextSibling()) } ParseUnit pu = new ParseUnit(selectedFile) pu.loadOrBuildPUB() walker(pu.topNode)
To see what variables are available in your Groovy Console, select Actions -> Inspect Variables from the console menu. The following little script shows something similar:
binding.variables.each {key, value -> if (key[0] == '_') return println "$key : $value" }
I found a way to get my own scripts directory into the classpath when using the console:
this.class.classLoader.addClasspath('/home/john/scripts') println this.class.classLoader.classPath