How to complete insertion of PsiElements?

I created two intentions that allow you to hit alt-enter inside an "if", and let you add an "else if" block and an "else" block. I got it working for 99%.

The last hurdle is some weird phenomenon where it appears the document is not re-parsed after inserting new PsiElements. When I insert an "else if( | ) {  }" (caret at |), I would think I get an red underline at the ( ) but it doesn't show up. Only after I have typed something the error is displayed.

In my code I use PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument()); to make sure the document is updated before I set the caret at the new position between ( ). I also have tried to use PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); but that doesn't appear to help much.

I have extensively searched inside the IntentionPowerPak plugin project, but they don't even use  code that commits anything. What am I missing here?

Update: In this post it is said that reformatting also reparses the code. That's what's happening in IntentionPowerPak. Still, I reformat too, but still the parser doesn't complain while it should (for an empty else if ( )).

1 comment
Comment actions Permalink

Parsing is a process of building the PSI tree from text. Since you're modifying the PSI tree directly, there is nothing to reparse - IntelliJ IDEA already has an up-to-date PSI tree for the document. And since a red wavy underline in this case is produced by the Java parser which inserts a PsiErrorElement into the tree, and you don't insert it, the error is not displayed.

Hope this helps.


Please sign in to leave a comment.