how to override default basic code completion behaviour

hello,
i'm trying to write a plugin that will override the default basic code completion behaviour - after pressing Ctrl + Space in the context of a Java file and with the carret placed within opening and closing braces of a method call (e.g. MyClass.myMethod(<caret>)) I would like to display a custom value list, retrieved for instance from the called method's annotation (e.g. @Vallist({"value1", "value2", "value3"}) . I'm not quite sure how to handle it :
1. can the described behaviour be achieved by a custom CompletionContributor or do I need to use an action for that ? IDEA's sources seem to suggest that a custom contributor may issue a 'veto' (result.stopHere()) to prevent further contributors from being asked for their suggestions.
Is it possible to somehow influence the position my contributor is registered at so its' veto can effectively influence the code completion process ?
2. What is the proper usage of the 'use-shortcut-of' action element attribute of plugin.xml plugin descriptor ? I tried to hijack the default basic code completion key shortcut (Ctrl + Space) using this code snippet (from plugin.xml) :

<action id="MyPlugin.HijackCodeCompletionAction" 
              text="Hijack CC" use-shortcut-of="CodeCompletion"/>


the code of HijackCodeCompletionAction is as follows :

public class HijackCodeCompletionAction extends AnAction {
    public void actionPerformed(AnActionEvent e) {
        System.out.println("-- HijackCodeCompletionAction entered, forwarding to default code completion action --");
        AnAction ccAction = ActionManager.getInstance().getAction("CodeCompletion");
        ccAction.actionPerformed(e);
    }
}


Nothing fancy - I only try to establish that the action gets executed (instead of the BaseCodeCompletionAction which seems to be the base CC implementation) and forward the execution to the proper actionPerformed() method of the target action (target action id taken from IdeActions).
But when I press Ctrl + Space nothing happens (no welcome message from Hijack's actionPerformed method gets printed to the standard output). What am I doing wrong ?
3. If there is some other approach to this problem, I would be grateful if someone capable could elaborate on that
THX in advance,
Simon

6 comments

IntelliJ IDEA already has an annotation for this purpose, with completion support. You can either smply use our annotation (@MagicConstant) or look at how the support is implemented:
https://github.com/JetBrains/intellij-community/blob/master/java/java-impl/src/com/intellij/codeInspection/magicConstant/MagicCompletionContributor.java

0

thank you for your reply, I might just do that.
What about 'use-shortcut-of' attribute of an 'action' plugin.xml element ?
Am I missing something in my code snippet above ? I tried adding 'keymap' attribute and setting it to '$default' but it didn't change anything - my HijackCodeCompletion action gets registered with ActionManager (verified that using a project-level component), it seems to be associated with the proper key shortcut (ctrl + space), but when I use this key sequence (regardles of the caret position) - only the default code completion action seems to fire; there's no stdout welcome message from my HijackCodeCompletionActio.actionPerformed().
Any advice on how to cope with that?

0

The standard code completion action gets triggered before your action and handles the keyboard shortcut, so your action does not get triggered. The use-shortcut-of attribute is intended to be used when actions are enabled in different contexts. Replacing the code completion action is not an appropriate way to add additional code completion variants.

0

hello Dmitry,
I'd hate to impose upon you but could you please elaborate on your last reply ?
If I understand it correctly I cannot override the default cc behaviour using custom CompletionContributor because it only allows to ADD some variants to the list; it cannot prevent other contributors from adding theirs - even using stopHere() method of CompletionResultSet object (registered contributors are processed in unspecified order; I cannot put mine in front of the others so its' veto would be effective)
Custom actions on the other hand are invoked after the default ones so in case of a partially-overlapping contexts they don't stand a chance with the 'core' ones, right ?
So how can I get ahead of the default code completion so I could either return my own variant list or forward code completion to the default addressee - depending on the invocation context ?
thanks once again for your assistance

0

You can control the order of completion contributors using the "order" attribute in plugin.xml when registering your extension.

For actions, there is no difference between default and non-default ones; actions are registered in the order of plugin.xml loading and there is no possibility to control the order of execution.

0

thank you for your assistance Dmitry - I managed to mimic the MagicCompletionContributor's behaviour in my own custom contributor and my value list appears at the top of the suggestion list.

0

Please sign in to leave a comment.