Provide completion from my language to another

Hello guys.
I have a code which provides references from PsiLiteral elements to the files from my custom language:

  def registerReferenceProviders(registrar: PsiReferenceRegistrar) {     registrar.registerReferenceProvider(PlatformPatterns.psiElement(classOf[PsiLiteral]), new PsiReferenceProvider() {       def getReferencesByElement(element: PsiElement, context: ProcessingContext) = {         val literal = element.asInstanceOf[PsiLiteral]         literal.getValue match {           case text: String =>             if (text != null && text.endsWith(".table")) {               Array[PsiReference](new XrlDecideReference(element, new TextRange(1, text.length() - ".table".length + 1), "table"))             } else Array[PsiReference]()           case _ => Array[PsiReference]()         }       }     })   }

Now I can go by Ctrl+Click from PsiLiteral elements directly to the *.table files

My question is how to add completion provider to provide completion for *.table files inside PsiLiteral elements?
I found CompletionProvider API but I don't understand how to enable it.
Thanks!

5 comments

The easiest way is to implement getVariants in your reference and return LookupElement[] from it.
If your completion might take a long time or you need some advanced stuff, you can implement CompletionContributor. Its Javadoc contains the necessary details.

0

Hi Peter,
Thank you wery much! I thought that contibutor can be only used inside your own custom language, because when it defiened in plugin.xml it also has "language" tag. But I found that language can be specified as "any":

<completion.contributor language="any" ... />


I also want to ask, why literal.getValue returns something like: "yourtextIntellijIdeaRulezzzzzz" in that place:

      new CompletionProvider[CompletionParameters]() {         def addCompletions(parameters: CompletionParameters, context: ProcessingContext, resultSet: CompletionResultSet) {             parameters.getPosition.getContext match {               case literal: PsiLiteral if literal.getValue.isInstanceOf[String] =>                 println(literal.getValue)               case _ => //just ignore             }         }       }


, but at the same time this code inside ReferenceContributor during tha call of literal.getValue returns normal value without "IntellijIdeaRulezzzzzz":

    registrar.registerReferenceProvider(PlatformPatterns.psiElement(classOf[PsiLiteral]), new PsiReferenceProvider() {       def getReferencesByElement(element: PsiElement, context: ProcessingContext) = {         val literal = element.asInstanceOf[PsiLiteral]         literal.getValue match {           case text: String =>             if (text != null && text.endsWith(".table")) {               Array[PsiReference](new XrlDecideReference(element, new TextRange(1, text.length() - ".table".length + 1), "table"))             } else Array[PsiReference]()           case _ => Array[PsiReference]()         }       }     })

0

Oh, I understood. Instead of using

parameters.getPosition.getContext.getValue

it should be used

parameters.getOriginalPosition.getContext.getValue


Thanks again!

0

Actually, using originalPosition won't probably always function as you imagine it to. This rulezzz thing is a dummy identifier inserted to make writing completion easier for you. See again the javadoc on CompletionContributor, search by keyword "dummy identifier".

0

Please sign in to leave a comment.