display completions in plugin
Answered
I have some class names stored in an array which I need to display as completions. I want to trigger these completions on typing some trigger characters like " and '. The completions should be displayed in any file at any place in the document. Can someone please guide me as Code Completions part was missing in the Editing part of official documentation. And if triggering completions using trigger characters is not possible then I am also fine with displaying completions in any other way but still, I want to display in any file and at any place.
Thanks in advance!!!
Please sign in to leave a comment.
I assume that you have also checked the Completion Contributor article, right? That example is still valid, but you may try the following snippet:
psiElement() allows you to invoke the completion on any PSI Element - so no filtering is applied.
When adding elements to the resultSet, add quotation characters explicitly.
Thanks Jakub!!
I am already using that article.
This is what I am using and by using this I am able to get this in case of a txt file.

But not in case of a JavaScript file as below.
can you please guide me
Did you add <completion.contributor> for JavaScript in your plugin.xml file?
This is what I did.
Does this help??
Just to make sure I am getting correct completions in case of a text file
but not getting correct completions in case of javascript file
Thanks
With
it should work for all file types. Have. you tried to debug it and check if it stops in the addCompletions method?
Thanks Jakub
it does not stop in addCompletions method.
I am able to get multiple completions in a single line in a JS file like this and these completions are displayed automatically when I start typing.
But when I try to get completions in this format className = "completion1Here completion2Here"
Then i am able to get completion completion1Here by invoking completions manually using ctrl+space but not automatically,
and not able to get completions2Here even after invoking completions manually
in this case, I am able to get actions__container by invoking completions manually but able to get the second completion even after invoking completions manually.
Just to make sure both the screenshots here are of JavaScript file and I want to insert multiple completions in one line.
Can you share your code as a minimal reproducible example project so I may debug it locally?
Thanks, Jakub
public class CompletionContributor extends com.intellij.codeInsight.completion.CompletionContributor {
public CompletionContributor() {
extend( CompletionType.BASIC,
PlatformPatterns.psiElement(),
new CompletionProvider<CompletionParameters>() {
public void addCompletions(@NotNull CompletionParameters parameters,
@NotNull ProcessingContext context,
@NotNull CompletionResultSet resultSet) {
resultSet.addElement(LookupElementBuilder.create("actions"));
resultSet.addElement(LookupElementBuilder.create("actions--container"));
resultSet.addElement(LookupElementBuilder.create("dropdown"));
resultSet.addElement(LookupElementBuilder.create("input"));
}
}
);
}
}
Is this sufficient??
Seems to be working as expected - isn't it?
Thanks, Jakub
Yes, it is working perfectly on your system.
I think there is some system-specific problem. I will try to look for it.
Just one last thing. Can you tell me if there is any problem with my build.gradle file or any other thing which can cause a issue.
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.4.21'
}
group 'org.example'
version '1.2.2'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2020.1.2'
type 'IU'
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}