views:

356

answers:

2

I refactor my code and I am looking for a solution to grep my source files for something like

if ( user && user.name && user.name.length() < 128 ) ...

in order to replace it later with ruby's andand or groovy's ?. operator (safe navigation operator).

+1  A: 

IntelliJ idea has a "structural search & replace" that will let you do this. This understands the semantics of the language so you can do all sorts of interesting replace operations.

http://www.jetbrains.com/idea/documentation/ssr.html

krosenvold
+2  A: 

Here's something to get you started, I wonder if this can be generalized more without having to generate the regexp programatically

line = "user && user.name && user.name.length()"
p line.match(/(?:(\w*)(?:\s\&\&\s(\1\.(\w*)))(?:\s\&\&\s(\2\.(\w*))))/).to_a.
  reject {|m| m.match(/\./)}.join('.andand.')

=> "user.andand.name.andand.length"
krusty.ar