How to complete string literal expressions?

I want to do auto-completion with string literal expressions. This can be useful when I want to automatically insert some extra literals, for example, system file paths.

I tried to write my own CompletionContributor, but found it difficult to trigger the completion action. Here's the extend() statement:

        extend(CompletionType.BASIC, PlatformPatterns.psiElement(PsiLiteralExpression.class), new MyCompletionProvider());

        extend(CompletionType.BASIC, PsiJavaPatterns.literalExpression(), new MyCompletionProvider());
        ...

All of the above didn't work. No popup window showed up. Any ideas?
9 comments
Comment actions Permalink

try

PlatformPatterns.psiElement().inside(PsiJavaPatterns.literalExpression())

Did you also consider using PsiReferenceContributor instead to provide navigation/resolving (just thinking when you write about paths as relevant items)?

0
Comment actions Permalink

Hi, Yann. It worked! Thank you!

To make it more flexible, I started to use PsiReferenceContributor following your advice. However, there's still a little problem. The user has to manullay press Ctrl+Space to trigger auto-completion. Is there any way to make it automatic?

0
Comment actions Permalink

Hi, Yann! I've read that post and added order="before SkipAutopopupInStrings" to the plugin.xml, since there seems to be no "javaSkipAutopopupInStrings" class any more. Sadly, still no luck. The popup window wouldn't show up automatically in idea-IC 135.1230, while it would automatically show up in the newest jetbrains c++ ide EAP. I don't know why...

0
Comment actions Permalink

The registration is

    <completion.confidence language="JAVA" implementationClass="com.intellij.codeInsight.completion.SkipAutopopupInStrings" id="javaSkipAutopopupInStrings"/>

So it should work with "before javaSkipAutopopupInStrings" for your plugin.xml

0
Comment actions Permalink

  <extensions defaultExtensionNs="com.intellij">
    <!-- Add your extensions here -->
      <psi.referenceContributor implementation="com.example.mycompletion" order="before javaSkipAutopopupInStrings"/>
  </extensions>

Here's my plugin registration. I've tried "first", "first, before default", "first, before javaSkipAutopopupInStrings", "before javaSkipAutopopupInStrings", etc. None of those worked, while ALL did work for the jetbrains c++ ide(based on idea 138.1213).

0
Comment actions Permalink

Why are you using " <psi.referenceContributor>" ? This is different EP, you want to use <completion.confidence language="JAVA" implementationClass="yourclass" order="before javaSkipAutopopupInStrings"/>

0
Comment actions Permalink

Hi, Yann! I followed your advice, implemented a CompletionConfidence class and registered it in the plugin.xml file. Everything worked! Thanks for your great patience!

0
Comment actions Permalink

Great to hear it works now!

0

Please sign in to leave a comment.