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?
...
All of the above didn't work. No popup window showed up. Any ideas?
Please sign in to leave a comment.
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)?
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?
please see http://devnet.jetbrains.com/message/5516303 for a solution
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...
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
<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).
Why are you using " <psi.referenceContributor>" ? This is different EP, you want to use <completion.confidence language="JAVA" implementationClass="yourclass" order="before javaSkipAutopopupInStrings"/>
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!
Great to hear it works now!