Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij Completion Contributor

I am developing a plugin for intellij and I want to add custom suggestions to xml editor based on a xsd. Up to now I can get required suggestions from 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 this did not provide any suggestion. but when I implement custom language it works. My objective is to view the context of the cursor position and provide suggestion based on it. as an example when user starts a tag on xml file plugin should provide attributes as code completion. I'm new to this Custom language.

So can anyone help me with this completion contributor?

like image 281
Hasintha Samith Randika Avatar asked Dec 17 '15 06:12

Hasintha Samith Randika


People also ask

How do I enable autocomplete in IntelliJ?

By default, IntelliJ IDEA displays the code completion popup automatically as you type. If automatic completion is disabled, press Ctrl+Shift+Space or choose Code | Code Completion | Type-Matching from the main menu. If necessary, press Ctrl+Shift+Space once again.

What is code completion in PyCharm?

Basic completion Basic code completion helps you complete the names of classes, methods, and keywords within the visibility scope. When you invoke code completion, PyCharm analyses the context and suggests the choices that are reachable from the current caret position (suggestions also include Live templates).


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 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>
like image 177
Hasintha Samith Randika Avatar answered Oct 21 '22 21:10

Hasintha Samith Randika


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.

like image 1
user2418306 Avatar answered Oct 21 '22 21:10

user2418306