Unable to add AutoComplete values for JavaScript using a CompletionContributer and CompletionProvider
Note: I had to repost this as I accidentally deleted it
--
Hi everyone,
I am trying to develop a plugin (my first for a JetBrains IDE) for JavaScript that adds values to the CompletionResultSet. I have followed the small tutorial found here:http://confluence.jetbrains.com/display/IntelliJIDEA/Completion+Contributor, however I cannot actually get my test completion result to show up.
Basically, when I create the CompletionProvider instance, I use 'extend' and pass a PsiElementPattern for 'var' statements and a CompletionProvider that should add the test completion result. You can see what my code essentially looks like below:
// MyCompletionContributor.java
public class MyCompletionContributor extends CompletionContributor {
public MyCompletionContributor() {
super();
PsiElementPattern pattern = PlatformPatterns
.psiElement(JSElementTypes.REFERENCE_EXPRESSION);
MyCompletionProvider completionProvider = new MyCompletionProvider();
extend(CompletionType.BASIC, pattern, completionProvider);
}
}
// MyCompletionProvider.java
public class MyCompletionProvider extends CompletionProvider<CompletionParameters> {
@Override
protected void addCompletions(
@NotNull CompletionParameters completionParameters,
ProcessingContext processingContext,
@NotNull CompletionResultSet completionResultSet) {
completionResultSet.addElement(LookupElementBuilder.create("TESTING!"));
}
}
// plugin.xml
<extensions defaultExtensionNs="com.intellij">
<completion.contributor order="FIRST" language="any" implementationClass="MyCompletionContributor" />
</extensions>
------
I can see that MyCompletionContributor gets instantiated when I open a JS file. I can also see that the contributor 'fillCompletionVariants' method is called. However, the provider 'addCompletions' method does not seem to ever get called. I have tried many different JS PsiElementPattern types and none of them seem to work.
I checked the result of "CompletionParameters.getPosition().getContext()" in "CompletionContributor.fillCompletionVariants" and I get JSReferenceExpression instance. So it seems like the PsiElementPattern should be matching the current context. However the provider 'addCompletions' method is never invoked.
What could I be missing here? If anyone knows or can point me to documentation that I can use to find a solution, I would very much appreciate it.
Thanks,
James
Please sign in to leave a comment.
So with more investigation, I discovered that I had loaded the JavaScriptLanguage jar incorrectly. I had added it as a library and not to the class path in the Project Structure. Rookie mistake.
When I used 'instanceof JSReferenceExpression' to test the element at the current position I was getting "false", but when I just compared the class name strings, I would get "true". I had somehow been able to create a class a second time without seeing an error message. After fixing the Project Structure, it seems to work now. However I am still having problems matching some PsiElementPatterns that seem like they should match, but that is probably a different issue. I am at least able to match JSReferenceExpression, which is a start.