Reference Contributor and Code Completion

Hallo, I still have problems to completely understand the reference contributor extension point. I registered the following PsiReferenceContributor

public class SOLReferenceContributor extends PsiReferenceContributor {     @Override     public void registerReferenceProviders(PsiReferenceRegistrar registrar) {         registrar.registerReferenceProvider(PlatformPatterns.psiElement(SOLTypes.REFERENCE),                 new PsiReferenceProvider() {                     @NotNull                     @Override                     public PsiReference[] getReferencesByElement(@NotNull PsiElement element,                             @NotNull ProcessingContext context) {                         String text = element.getText();                         element.getTextRange();                         return new PsiReference[] {new SOLReferenceBase(element, element.getTextRange())};                     }                 });     } }

CTRL+B works great, but the code completion does not work always. In my language I allow two constructs. The file and the PSIStructure of the first look like this:

In this case the completion works. When I press CTRL-Space in my reference contributor getReferencesByElement is called as expected. IDEA seems to create a dummy REFERENCE-Element with the text "I" ... ???
The grammar describing this part looks like this:

propertyMapping ::= (reference EQUALS reference)|reference

I the second construct also a REFERENCE-Element is expected, but pressing CTRL-Space doesn't work. The file and the PSIStructure look in this case like this:
The grammar describing this part looks like this:

propertyDefinition ::=  PROPERTY_TYPE COMPARISON_TYPE PROPERTY_NAME COMMENT* OPENING_BRACKET (reference|languageDefinition|COMMENT)* CLOSING_BRACKET {mixin="com.sol.psi.impl.SOLNamedElementImpl" implements="com.sol.psi.SOLNamedElement, com.sol.psi.SOLIdentified" methods=[getName getNameIdentifier getPresentation setName]}

So after the opening bracket also a REFERENCE-Object is possible. However when I press CTRL-Space getReferencesByElement is not called and no dummy-Element is created.

I would be really thankful for any comment because that problem keeps me awake a long time now.
Regards, Sebastian

Comment actions Permalink

Is your code available somewhere publicly?

Comment actions Permalink

Now it is, you can get it here:

git clone

The structure is very similar to the structure of the language definition tutorial here:

in the folder exampleSOLFiles are examples of the language definition. Thank you and I hope this helps to see the problem.

Comment actions Permalink

I am not sure if this is the correct answer but it helped me out:
if CTRL-Space is pressed, IntelliJ inserts a DUMMY-Char in the docuiment. The document is parsed and if the dummy is recognized as a PsiElement (with getText() == 'I') the parsed PsiElement is recognized by ReferenceContributor and  the workflow continues as expected, i.e. the element is recognized as a PsiReference and getVariants() is called and completion works.

In my case I guess the problem was that the dummy element was never created or it was created but the parser never recognized it as the element that should be completed. So no reference was detected. I was able to solve this problem by adding constraints to my parser saying that an element should be even recognized as this element although child tokens are missing yet. I use GrammarKit to create my parser so this could be accomplished by adding {pin=\d+} to the rule that contains the pattern of desired autocompletion.

I don't know whether I am on the right track there. However, if you ever face the problem that reference resolvation works great, renaming works great but autocompletion of references does not, it may be your parser that does not recognize the DUMMY token created by IntelliJ.


Please sign in to leave a comment.