IntelliJ插件开发:如何生成一个新的源文件(IntelliJ Plugin Development: how to generate a new source file)

编程入门 行业动态 更新时间:2024-10-24 22:21:52
IntelliJ插件开发:如何生成一个新的源文件(IntelliJ Plugin Development: how to generate a new source file)

我正在研究一个将在打开的类内部和外部生成代码的插件。

我扩展了GenerateMembersHandlerBase类来GenerateMembersHandlerBase类的成员 - 它工作得很好。

现在我需要生成一个完整的类(新的源文件),这个文件将放在我工作的类中。

问题:我找不到任何允许我创建新的Java源文件的API。 请提示我API,或者在IntelliJ IDEA源代码中实现这种功能的地方提示我。

更多的上下文:假设我们有com.mycompany.User类。 我正在实现一个插件,它将为下一个格式生成一个构造函数:

public User(UserInfo info) { //... }

并且需要在航班上生成UserInfo类。 所以在代码生成后,我应该在类User有一个构造函数,并为com.mycompany.UserInfo分开源文件。

I'm working on a plugin which will generate code inside and outside of the opened class.

I extended GenerateMembersHandlerBase class to generate members of the class - it works quite fine.

Now I need to generate a full class (new source file) which will be placed aside from the class I working on.

Problem: I can't find any API that would allow me to create new java source file. Please hint me the API, or hint me the place in IntelliJ IDEA source code where such thing is implemented.

More context: Let's say we have com.mycompany.User class. I'm implementing a plugin that will generate a constructor for it with the next format:

public User(UserInfo info) { //... }

And the UserInfo class needs to be generated on the flight. So after code generation, I should have a constructor in class User and separate source file for com.mycompany.UserInfo.

最满意答案

您可以使用VirtualFile.createChildData()方法创建一个新的虚拟文件。

在NewFileAction.createNewFile()方法中可以找到IDEA自身创建文件的例子。

它调用调用上述createChildData()方法的FileSystemTreeImpl.createNewFile createChildData()方法。

You can create a new virtual file using VirtualFile.createChildData() method.

An example of how IDEA itself creating files can be found in NewFileAction.createNewFile() method.

It's calling the FileSystemTreeImpl.createNewFile() method which calling the aforementioned createChildData() method.

更多推荐

本文发布于:2023-08-07 05:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:源文件   插件   Plugin   IntelliJ   source

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!