Custom java method code completion

I want to provide additional choices for code completion - for example, when I type "myVariableName." the popup dialog which normally would display the objects attributes and methods, should also include my additional custom choices. I wrote this short extension and I feel I am missing something because after I build and install the plugin, it does absolutely nothing - I cannot see "Hello" on any suggestion list.

public class VoComplete extends CompletionContributor
{
public VoComplete()
{
extend( CompletionType.BASIC,
PlatformPatterns.psiElement( JavaElementType.METHOD ).withLanguage( JavaLanguage.INSTANCE ),
new CompletionProvider<CompletionParameters>()
{
public void addCompletions( @NotNull CompletionParameters parameters,
ProcessingContext context,
@NotNull CompletionResultSet resultSet )
{
resultSet.addElement( LookupElementBuilder.create( "Hello" ).bold() );
}
}
);
}
}

<extensions defaultExtensionNs="com.intellij">
<completion.contributor language="JAVA" implementationClass="VoComplete"/>
</extensions>

Could someone point me to a complete example of how intelisense has been extended to include additional method completion choices in IntelliJ for Java?

1
5 comments
Avatar
Permanently deleted user

You need to include the entire fully-qualified class name in the implementationClass attribute.  For example: 

<extensions defaultExtensionNs="com.intellij">
<completion.contributor language="JAVA" implementationClass="com.intellij.plugins.vo.ide.VoComplete"/>
</extensions>
0
Avatar
Permanently deleted user

Dough... right - so I changed it but still no joy. I changed my package even to look exactly as in your example:

<extensions defaultExtensionNs="com.intellij">
<completion.contributor language="JAVA" implementationClass="com.intellij.plugins.vo.ide.VoComplete"/>
</extensions>

I added code to create a file if the constructor and the method ever get called. The constructor did, the method didn't. I am wondering if this line is correct

PlatformPatterns.psiElement( JavaElementType.METHOD ).withLanguage( JavaLanguage.INSTANCE ),

 

0
Avatar
Permanently deleted user

I always have trouble with patterns, myself.  In the case of the completion, you are probably getting the id, not the method element.  So, try the pattern:

PlatformPatterns.psiElement( JavaTokenTypes.IDENT ).withParent( JavaElementType.METHOD );

or 

PlatformPatterns.psiElement( PsiIdentifier.class ).withParent( PsiMethod.class)

I don't think you need the withLanguage clause, unless there are language plugins out there that use the JavaElementType as a base type.  It probably doesn't hurt, but I'd get things working before I added that.

0
Avatar
Permanently deleted user

Thank you - good ideas to try.

psiElement doesn't want an int though (JavaTokenType.IDENT).

I tried the second suggestion, but couldn't get it to produce anything until I removed withParent - I guess that's just saying put anything through :). I can probably narrow it down from here.

0
Avatar
Permanently deleted user

The second version should have worked.  :/

The first thing is to figure out what element is being passed.  Then, we can figure out how it fits in the PSI structure.  For the method class, I used the PSI viewer to find the parent :

You can see here that the PsiMethod is the parent of the "getFileNames" identifier.  That is what I based the suggestion on.  If you set a breakpoint in your contributor, you can see the element that is being tested.

If you want something more sophisticated to debug what's going on, you can look at what I ended up using:  https://github.com/HaxeFoundation/intellij-haxe/blob/develop/src/common/com/intellij/plugins/haxe/ide/completion/HaxeCommonCompletionPattern.java  For me, how the patterns actually worked wasn't exactly what I was expecting.

 

0

Please sign in to leave a comment.