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!!!

10 comments
Comment actions Permalink

I assume that you have also checked the Completion Contributor article, right? That example is still valid, but you may try the following snippet:

extend(CompletionType.BASIC, psiElement(),
new CompletionProvider<CompletionParameters>() {
public void addCompletions(@NotNull CompletionParameters parameters,
@NotNull ProcessingContext context,
@NotNull CompletionResultSet resultSet) {
for (String element : elements) {
resultSet.addElement(LookupElementBuilder.create("\"" + element + "\""));
resultSet.addElement(LookupElementBuilder.create("'" + element + "'"));
}
}
}
);

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.

0
Comment actions Permalink

Thanks Jakub!!

I am already using that article.

extend( CompletionType.BASIC,
PlatformPatterns.psiElement(),
new CompletionProvider<CompletionParameters>() {
public void addCompletions(@NotNull CompletionParameters parameters,
@NotNull ProcessingContext context,
@NotNull CompletionResultSet resultSet)

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

0
Comment actions Permalink

Did you add <completion.contributor> for JavaScript in your plugin.xml file?

0
Comment actions Permalink

This is what I did.

<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<completion.contributor language="any" implementationClass="com.completions.CompletionContributor" />
</extensions>

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

0
Comment actions Permalink

With

language="any"

it should work for all file types. Have. you tried to debug it and check if it stops in the addCompletions method?

0
Comment actions Permalink

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.

0
Comment actions Permalink

Can you share your code as a minimal reproducible example project so I may debug it locally?

0
Comment actions Permalink

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"));
}
}
);
}

}

<idea-plugin>
<id>completions</id>
<name>completions_provider</name>
<vendor email="abc@xyz.com" url="xyz.com">completions provider</vendor>

<description><![CDATA[
plugin for providing completions plugin for providing completions
]]></description>


<depends>com.intellij.modules.platform</depends>
<extensions defaultExtensionNs="com.intellij">
<completion.contributor language="any" implementationClass="com.completions.CompletionContributor" />
</extensions>

<actions>
<!-- Add your actions here -->
</actions>
</idea-plugin>

Is this sufficient??

0
Comment actions Permalink

Seems to be working as expected - isn't it?

 

0
Comment actions Permalink

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>"""
}
0

Please sign in to leave a comment.