Reformat code after inserting it with CodeCompletion
I'm trying to implement my custom code completion contributor, at the moment I've managed to register my custom contributor and it's inserting my custom code fine.
The problem is the code is inserted without format, I'm inserting XML nodes, and I want to reformat the code after inserting them.
Example:
<root>
<mynode></mynode>
</root>
<mynode></mynode> is inserted with my code completion contributor, but I want to have this instead:
<root>
<mynode>
| // the caret here
</mynode>
</root>
Can you help me to do that? is there any example to see the code?
Thanks !!
Please sign in to leave a comment.
Hi Enrique,
IJ works as follows internally:
Denis
Hi Denis, I think I need more help with this, it's really difficult to follow the code because I can't debug the local variables (I'm getting always "debug info is not available").
I'm using this right now:
That is inserting my text, but how I said, I don't know how to reformat it automatically, and how to move the caret (I think I can move the caret wi the InsertHandler...).
Where should I execute CodeEditUtl.markToReformat?
also, it needs an ASTNode, and I'm inserting an String.
public static void markToReformat(final ASTNode node, boolean value) {
Please can you provide more info on this?
Thanks !
Hi,
I'll ask our guy who supports completion infrastructure to take a look here.
Btw, regarding the debugging - you always can build IJ from sources. All necessary information will be included to the byte code then.
Denis
To reformat the code in your InsertHandler:
1. Commit the document via context.commitDocument to make sure PSI and document are synchronized.
2. Move the caret (editor.getCaretModel().moveToOffset) where it's supposed to be.
3. Use any applicable CodeStyleManager reformat* methods to reformat.
4. The caret position should be updated automatically and moved into some meaningful place. It it's not that meaningful, you may skip step 2 and move it now. Note that PSI structure may be destroyed after reformatting due to changed offsets, and you'll need to anchor your element first via SmartPointerManager to be able to find its new incarnation.
It's working now, Thank you so much Peter !!