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 !!

0
5 comments
Avatar
Permanently deleted user

Hi Enrique,

IJ works as follows internally:

  1. All modifications are performed via utility methods that mark target elements to be reformatted (have a look into the 'CodeEditUtil.markToReformat()' and other methods at the same class);
  2. PsiDocumentManager.doPostponedOperationsAndUnblockDocument() is called when all changes are performed. It reformats all marked elements according to the current code style settings;


Denis

0
Avatar
Permanently deleted user

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:


extend(CompletionType.BASIC, everywhere, new CompletionProvider<CompletionParameters>() {
            public void addCompletions(@NotNull final CompletionParameters parameters,
                                       final ProcessingContext matchingContext,
                                       @NotNull final CompletionResultSet _result) {

     ...     

     LookupElement element = LookupElementBuilder.create("<" + name + "></" + name + ">")
                            .setPresentableText(name)
                            .setIcon(myIcon)
                            .setInsertHandler(QUOTE_EATER);          // I'm testing this but I don't know how to use it yet

     _result.addElement(element);

     }

});


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 !

0
Avatar
Permanently deleted user

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

0

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.

0
Avatar
Permanently deleted user

It's working now, Thank you so much Peter !!

0

Please sign in to leave a comment.