Replace text with executed code output

Hello,
     I've had the need for this feature only few times through my past, but today I decided to check it can be done with Idea.

     Imaginary problem: find all instances of integer assignments in the file and substract 10 from them.
     Imaginary solution: I open search&replace dialog, check "Regex" and check "Groovy replace". In the search box I type small Perly regex: "([0-9]+?)", and in the Replace box I type: "$1.toInteger() - 10".
     Imaginary result example: search found the expression "x = 20" and after executing replace, code looks like this "x = 10"

     Explanation, the code in the replace box should be executed for each encountered search (and crash if found variable couldn't be interpreted as correct type I guess), and then executed, and its output used as replace.

     What I've managed to do so far is to use Structural Search & Replace to find what do search. I realized that when new variables are defined in Replace box only, editing those variables offers Groovy script editing, and I managed to execute simple statements that become replacements - but only for constant values. I also realized that I'm able to create Dynamic Properties with intention action, but I have no idea what are they and how to use them (documentation is a bit scarce or I couldn't find it).
     Is it somehow possible to reference variables defined in the Search box inside Groovy script defined for Replace variable? If yes, this would solve my problem I guess.

     My practical problem: I have around 30 static final definitions of Rectangle that defines x/y/x2/y2 coordinates for GUI components in screen space. Instead of manually transforming coordinates into the container space and manually transforming x2/y2 to width/height, I would like to execute code with search/replace to do that for me - since screenspace coordinates of the container are known and can be constants in the search.


     Sorry for a bit longer post than necessary, I've done chat with 3 of my friends and I had difficulties explaining what exactly I want, so just wanted to be clear :-)
     Sincerely,

2 comments
Comment actions Permalink

SSR passes all search variables like $myvar$ to replacement script as
myvar, the type of this variable is ancestor type of PsiElement
Additionally __context__ variable of the same type is available for
complete matched expression

Deus wrote:

Hello,
     I've had the need for this feature only few times through my past, but today I decided to check it can be done with Idea.

     Imaginary problem: find all instances of integer assignments in the file and substract 10 from them.
     Imaginary solution: I open search&replace dialog, check "Regex" and check "Groovy replace". In the search box I type small Perly regex: "(+?)", and in the Replace box I type: "$1.toInteger() - 10".
     Imaginary result example: search found the expression "x = 20" and after executing replace, code looks like this "x = 10"

     Explanation, the code in the replace box should be executed for each encountered search (and crash if found variable couldn't be interpreted as correct type I guess), and then executed, and its output used as replace.

     What I've managed to do so far is to use Structural Search & Replace to find what do search. I realized that when new variables are defined in Replace box only, editing those variables offers Groovy script editing, and I managed to execute simple statements that become replacements - but only for constant values. I also realized that I'm able to create Dynamic Properties with intention action, but I have no idea what are they and how to use them (documentation is a bit scarce or I couldn't find it).
     Is it somehow possible to reference variables defined in the Search box inside Groovy script defined for Replace variable? If yes, this would solve my problem I guess.

     My practical problem: I have around 30 static final definitions of Rectangle that defines x/y/x2/y2 coordinates for GUI components in screen space. Instead of manually transforming coordinates into the container space and manually transforming x2/y2 to width/height, I would like to execute code with search/replace to do that for me - since screenspace coordinates of the container are known and can be constants in the search.


     Sorry for a bit longer post than necessary, I've done chat with 3 of my friends and I had difficulties explaining what exactly I want, so just wanted to be clear :)
     Sincerely,

---
Original message URL: http://www.jetbrains.net/devnet/message/5245741#5245741



--
Best regards,
   Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

After some short term bashing around to figure out how exactly to get Integer from PsiLiteralElement, I managed to save myself at least an hour of work manually calculating values for Rectangles.
Works as a charm, thanks.

0

Please sign in to leave a comment.