Code template

Answered

Hi there,

I would like to add a template which would be editable via Editor / File and Code Templates / Code

 

If I add file to resources/fileTemplates they will be added to "new file" menu, which is not what I want.

If I add them resources/fileTemplates/internal, the user will not be able to modify them.

 

2 comments
Comment actions Permalink

See org.jetbrains.idea.devkit.DevKitFileTemplatesFactory as sample, the file templates are located under /fileTemplates/j2ee

0
Comment actions Permalink

Thank you Yann!

So, steps to create subj:

 

1. place templates to `resources/fileTemplates/j2ee/`

Files should have extension "*.ft", i.e. if referencing file name is "Module Command.php", it should be located at "resources/fileTemplates/j2ee/Module Command.php.ft"

 

2. Create the templates factory, i.e.

package com.developer.component.plugin

import com.intellij.ide.fileTemplates.FileTemplateDescriptor
import com.intellij.ide.fileTemplates.FileTemplateGroupDescriptor
import com.intellij.ide.fileTemplates.FileTemplateGroupDescriptorFactory
import com.jetbrains.php.lang.PhpFileType

class FileTemplatesFactory : FileTemplateGroupDescriptorFactory {

override fun getFileTemplatesDescriptor(): FileTemplateGroupDescriptor {
val descriptor = FileTemplateGroupDescriptor("My plugin", Icons.myPluginIcon);

descriptor.addTemplate(object : FileTemplateDescriptor("Edl Event.proto", Icons.myPluginIcon) {
override fun getDisplayName(): String = "Edl Event"
})
descriptor.addTemplate(object : FileTemplateDescriptor("Module Command.php", PhpFileType.INSTANCE.icon) {
override fun getDisplayName(): String = "Module Command"
})
descriptor.addTemplate(object : FileTemplateDescriptor("Module Command Method.php", PhpFileType.INSTANCE.icon) {
override fun getDisplayName(): String = "Module Command Method"
})
return descriptor;
}
}

 

3. Refer to the template in code using getJ2eeTemplate:

val methodTemplate = FileTemplateManager.getInstance(project).getJ2eeTemplate("Module Command Method.php")
val methodContent = methodTemplate.getText(
mapOf(
Pair("NEW_CLASS_NAME", newClassName),
Pair("ARGUMENT_LIST", arguments)
)
)
0

Please sign in to leave a comment.