I want to create a plugin for Intellij IDEA. I need to add an action (AnAction
) that will create a class
in Kotlin (not Java) in a custom package
. I have two questions:
class
(from some base class) in a custom package
?One possible way to accomplish this is to use PsiFileFactory.createFileFromText()
to create an in-memory Kotlin file and then to pass that file as a parameter to PsiDirectory.add()
to save it to the filesystem.
Although yole's answer is correct, I would like to see more details about mistery PsiDirectory class.
//get directory by VirtualFile
PsiDirectory directory = PsiManager.getInstance(project).findDirectory((getKotlinSourceDir(project))
Get kotlin source dir:
private VirtualFile getKotlinSourceDir(Project project) {
return project.getBaseDir().findChild("src").findChild("main").findChild("kotlin");
}
And than you can create subdirectories:
//someDirectoryName it is simple name
//(i.e if you post "com.some.package" - it does not create com/some/package folder)
PsiDirectory newDirectory = psiDirectory.createSubdirectory(someDirectoryName);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With