使IntelliJ知道XML文件中Java元素的链接 [英] Make IntelliJ aware of links to Java elements in XML files

查看:211
本文介绍了使IntelliJ知道XML文件中Java元素的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接到Java资源的自定义XML格式。为简单起见,我们假设我的XML文件如下所示:

I have a custom XML format that links to Java resources. For the sake of simplicity let's assume my XML file would look like this:

<root>
  <java-class>my.fully.qualified.class.name</java-class>
</root>

最终我的推荐会更复杂一些。它不会直接包含完全限定的类名,我需要一些逻辑来解析正确的类,但我想在这里保持示例尽可能简单。

Eventually my references will be somewhat more complicated. It will not contain the fully qualified class name directly and I will need some logic to resolve the correct class, but I want to keep the example as simple as possible here.

现在我希望它可以Strg +单击元素的文本,并希望IntelliJ将我带到.java文件,就像在Spring-XML文件中一样。在 IDEA插件开发常见问题解答中是一个名为如何在XML文件中添加对Java元素的自定义引用?这听起来就像我需要的那样。不幸的是,它链接到一个讨论,其中有人或多或少地完成了这样的事情,有一些小问题。不过我明白我可能需要编写com.intellij.psi.PsiReference接口的实现。谷歌搜索PsiReference和IntelliJ或IDEA遗憾的是没有提出任何关于如何使用它的教程,但我找到了类 XmlValueReference 听起来很有用。然而再次谷歌搜索XmlValueReference没有发现任何有用的如何使用该类。至少 PSI Cookbook 告诉我,我能找到使用 JavaPsiFacade.findClass()的Java类。我会感谢任何有关正确用法的教程,提示等。

Now I want it to be possible to Strg+Click on the element's text and want IntelliJ to carry me to the .java file, just like it is possible in Spring-XML files. In the IDEA Plugin Development FAQ there is a link called "How do I add custom references to Java elements in XML files?" which so much sounds like exactly what I need. Unfortunately it links to a discussion where someone is more or less done implementing something like this, having some minor problems. Nevertheless I understood that I probably need to write an implementation of the interface com.intellij.psi.PsiReference. Googling for "PsiReference" and "IntelliJ" or "IDEA" unfortunately did not bring up any tutorials on how to use it, but I found the class XmlValueReference which sounds useful. Yet again googling for "XmlValueReference" did not turn up anything useful on how to use the class. At least the PSI Cookbook tells me that I can find the Java class by using JavaPsiFacade.findClass(). I'd be thankful for any tutorials, hints and the like, that tell the correct usage.

以上链接的讨论提到我需要调用 registry.registerReferenceProvider(XmlTag.class,provider)为了在我最终设法实现它之后注册我的提供者,但是哪种类型是注册表,我从哪里获取它?

The above linked discussion mentions that I need to call registry.registerReferenceProvider(XmlTag.class, provider) in order to register my provider once I eventually managed to implement it, but of which type is "registry" and where do I get it from?

推荐答案

首先,这是一个精彩教程,解释了IntelliJ插件开发的基础知识(你应该看一下参考贡献者)。

First of all, here's a nice tutorial that came up a few days ago, which explains the basics of IntelliJ plugin development (you should take a look at the section Reference Contributor).

你可能需要定义你的拥有 PsiReferenceContributor ,它将在 plugin.xml 中引用,如下所示:

You will likely have to define your own PsiReferenceContributor, which will be referenced in your plugin.xml like this:

<psi.referenceContributor implementation="com.yourplugin.YourReferenceContributor"/>

在你的参考文献中,有一个方法 registerReferenceProviders(PsiReferenceRegistrar)您可以在哪里调用 registry.registerReferenceProvider(XmlTag.class,provider)

In your reference contributor, there's a method registerReferenceProviders(PsiReferenceRegistrar) where you will be able to call registry.registerReferenceProvider(XmlTag.class, provider).

最后,在 PsiReferenceProvider 的实例中,您必须测试标记名称以过滤掉不包含类引用的标记,然后找到正确的Java类使用 JavaPsiFacade.findClass()

Finally, in your instance of PsiReferenceProvider, you will have to test the tag name to filter out tags which don't contain class references, then find the right Java class using JavaPsiFacade.findClass().

根据我的经验,获得IntelliJ插件开发帮助的最佳地点是 JetBrains的论坛。

From my experience, the best place to get help regarding IntelliJ plugin development is JetBrains' forums.

这篇关于使IntelliJ知道XML文件中Java元素的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆