【问题标题】:Intellij Completion ContributorIntellij 完成贡献者
【发布时间】:2015-12-17 06:38:52
【问题描述】:

我正在为 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 文件插件上启动标签时,应提供属性作为代码完成。我是这种自定义语言的新手。

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

【问题讨论】:

  • 你是如何在 plugin.xml 中注册这个贡献者的?
  • 我添加

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


【解决方案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>

【讨论】:

    【解决方案2】:

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

    在我的例子中,问题是language="Java",而所有大写都是预期的。

    【讨论】:

      猜你喜欢
      • 2016-05-12
      • 2011-09-13
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 2017-11-17
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多