Replace one statement with n others
Hi
If, for example, I have a reference to a PsiDeclarationStatement (which is part of the file) and would like to replace this statement with n other statements that are also part of that file, what is the recommended way to do it?
There is PsiElement.replace(), but it takes PsiElement and I need to pass in PsiElement[]... Maybe there is some PsiElement that I can use for 'aggregation'?
--Etienne
Please sign in to leave a comment.
It's a bit of a hassle, but you need to do a "replace" on the first statement, followed by a bunch of "addAfter" calls to add the remaining statements. You'll also almost certainly want to trigger a relayout on the parent of the replaced code, as there's no way you're going to end up with something decent looking.
You can find a few examples of this in the InspectionGadgets codebase, although I never did abstract it out.
--Dave Griffith
Hi Dave
Thanks for the hints.
It works fine using your suggested approach.
It took me a while to figure out that I have to copy all statements first, before starting to do the replace/addAfter, otherwise I ended up with assertion errors somewhere down in the IDEA code.
--Etienne
That's usually not possible for IG inspections, as the statements aren't copied, but are generated from text.
--Dave Griffith