Sasha Shpota
Sasha Shpota

Reputation: 10280

IntelliJ Plugin Development: how to generate a new source file

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.

Upvotes: 2

Views: 2472

Answers (1)

Argb32
Argb32

Reputation: 1405

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.

Upvotes: 5

Related Questions