Intellij完成贡献者

Has*_*ika 9 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"));
                }
            }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

Has*_*ika 5

终于我找到了解决这个问题的方法

这是我的代码

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

                }
            }
    );
    }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

 <extensions defaultExtensionNs="com.intellij">
 <completion.contributor language="Scala"    implementationClass="com.hsr.ScalaXMLCompletionContributor"/>
 </extensions>
Run Code Online (Sandbox Code Playgroud)