Intellij完成贡献者

时间:2015-12-17 06:38:52

标签: java xml intellij-idea code-completion intellij-plugin

我正在为intellij开发一个插件,我想基于xsd向xml编辑器添加自定义建议。到目前为止,我可以从xsd文件中获得所需的建议。

我已经为xml实现了一个完成贡献者,如下所示

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

但这没有提供任何建议。但是当我实现自定义语言时,它可以工作我的目标是查看光标位置的上下文并根据它提供建议。例如,当用户在xml文件插件上启动标记时,应该提供属性作为代码完成。我是这种自定义语言的新手。

那么有人可以帮助我完成这个完成贡献者吗?

2 个答案:

答案 0 :(得分:1)

最后我找到了解决这个问题的方法

这是我的代码

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) {
                        }
                    }

                }
            }
    );
    }
    }

在这种情况下,我们可以通过完成参数获得与光标位置相关的光标位置和标记,我们可以使用cpmpletion结果集注入建议。这也可以用scala语言实现。

在插件xml中注册完成贡献者

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

答案 1 :(得分:1)

com.intellij.codeInsight.completion.CompletionContributor的JavaDoc包含常见问题解答。 last question解决了调试无法完成的问题。

在我的案例中,问题是language="Java",而所有上限都是预期的。

相关问题