Intellij Completion Contributor Member

I am developing a plugin for intellij and I want to add special offers to the xsd based xml editor. So far, I can get the required sentences from the xsd file.

I have implemented a completion contributor for xml as follows

import com.intellij.codeInsight.completion.*; import com.intellij.codeInsight.lookup.LookupElementBuilder; import com.intellij.patterns.PlatformPatterns; import com.intellij.psi.xml.XmlElementType; import com.intellij.util.ProcessingContext; import com.intellij.lang.xml.*; import org.jetbrains.annotations.NotNull; public class SimpleCompletionContributor extends CompletionContributor { public SimpleCompletionContributor() { extend(CompletionType.BASIC,PlatformPatterns.psiElement(XmlElementType.XML_ATTRIBUTE_VALUE).withLanguage(XMLLanguage.INSTANCE), new CompletionProvider<CompletionParameters>() { public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) { resultSet.addElement(LookupElementBuilder.create("Hello")); } } ); } } 

but it did not give any suggestions. but when I implement a custom language, it works. My goal is to look at the context of the cursor position and suggest based on this sentence. as an example, when a user launches a tag in an XML file, the plugin should provide attributes as code completion. I am new to this custom language.

So can anyone help me with this add-on?

+8
java xml intellij-idea intellij-plugin code-completion
source share
2 answers

finally i found a way to solve this problem

here is my code

 import com.intellij.codeInsight.completion.*; import com.intellij.codeInsight.lookup.LookupElementBuilder; import com.intellij.patterns.PlatformPatterns; import com.intellij.util.ProcessingContext; import org.jetbrains.annotations.NotNull; public class ScalaXMLCompletionContributor extends CompletionContributor { public ScalaXMLCompletionContributor() { final RelativeNodes rlt = new RelativeNodes();//this is a class to get siblings and children from a sample xml file generated by a given xsd /*if the parameter position is an xml attribute provide attributes using given xsd*/ extend(CompletionType.BASIC, PlatformPatterns.psiElement(), new CompletionProvider<CompletionParameters>() { public void addCompletions(@NotNull CompletionParameters parameters,//completion parameters contain details of the curser position ProcessingContext context, @NotNull CompletionResultSet resultSet) {//result set contains completion details to suggest if (parameters.getPosition().getContext().toString() == "XmlAttribute") {//check whether scala text editors position is an xml attribute position eg: <name | try { String[] suggestions = rlt.getAttribute(parameters.getPosition().getParent().getParent().getFirstChild().getNextSibling().getText().replaceFirst("IntellijIdeaRulezzz", ""));//extract text from completion parameter and get required suggestions from RelativeNodes int i = 0; do { resultSet.addElement(LookupElementBuilder.create(suggestions[i]));//add suggestions to resultset to suggest in editor i++; } while (suggestions[i] != null); } catch (NullPointerException e) { } } } } ); } } 

in this case, we can get the cursor position and markers associated with the cursor position by the completion parameters, and we can enter sentences using the cpmpletion result set. this can also be implemented in scala.

register add-on contributor to xml plugin

  <extensions defaultExtensionNs="com.intellij"> <completion.contributor language="Scala" implementationClass="com.hsr.ScalaXMLCompletionContributor"/> </extensions> 
+1
source share

The JavaDoc for com.intellij.codeInsight.completion.CompletionContributor contains a FAQ. the last question allows you to debug a non-working completion.

In my case, the problem was language="Java" , while all caps were expected.

+1
source share

All Articles