Intellij Completion Contributor Intellij Completion Contributor xml xml

Intellij Completion Contributor


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 obtain cursor position and tokens related to curser position by completion parameters and we can inject suggestions using cpmpletion resultset. this can be implemented in scala language too.

to register completion contributor in plugin xml

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


JavaDoc for com.intellij.codeInsight.completion.CompletionContributor contains FAQ.The last question addresses debugging not working completion.

In my case issue was language="Java", whereas all caps expected.