How to share the File Templates via IntelliJ IDEA plugin

Answered

Unfortunately, I did not find some manuals how to share the file templates via IntelliJ IDEA plugin.
I know there are other people which has troubles with it too (for example, here: https://stackoverflow.com/q/51187319/4818123).

The source code of the Julia language plugin reccomended in mentioned above topics does not obeys to
recommended project structure of IntelliJ IDEA plugin (the file templates are in `res/fileTemplates/` directory - I suppose is should be below `src/main/resources`).
Please teach me how to share the file templates via my plugin.

0
9 comments

Hi Tokugawa,

For File Templates, please take a look at how the example plugins implement com.intellij.fileTemplateGroup extension:

We will cover it in docs soon.

0

Karol Lewandowski,

Thank you for the anwer!

I checked some packages, but unfortunately, there are a lot of various methodologies.
I am feeling uncofortable copying these code without understanding what I doing.

May I ask you to share the code example (I suppose, it will be one-line code) of the correct file template declaration inside `<extensions defaultExtensionNs="com.intellij">` of src/main/resources/META-INF/plugin.xml for the file src/main/resources/fileTemplates/example.xml? I suppose, it will be something like


<fileTemplate path="fileTemplates/test.xml">

0

Hi Takeshi,

It's not the file template that should be referenced but FileTemplateGroupDescriptorFactory implementation. Also, you used the wrong EP name, it's "fileTemplateGroup", not "fileTemplate". See this example (randomly taken from https://jb.gg/ipe):

0

Karol Lewandowski

Dear mr. Lewandowski,
thank you for the answer.

I checked the references which you suggested.

What I did not understand is how FileTemplateFactory.kt referes to Template Application.java.ft, Template AucApiClass.java.ft, etc. In FileTemplateFactory.kt actually MainActivity.java, Manifest.xml and 2 more files which does not present in resources/fileTemplates has been mentioned.

Would you please to teach me how to associate the FileTemplateFactory.kt with below my own 4 files?

0

Hi Takeshi,

Let's do it for a single template = "Layout (Yamato Daiwa Frontend).pug"

  1. The first issue is that the template file has an incorrect extension. File templates should have ".ft" extension. Rename it to "Layout (Yamato Daiwa Frontend).pug.ft".
  2. Create FiletemplateGroupDescriptorFactory (icon and names are just examples):
    class TestFileTemplateGroupFactory : FileTemplateGroupDescriptorFactory {
    override fun getFileTemplatesDescriptor(): FileTemplateGroupDescriptor {
    val groupDescriptor = FileTemplateGroupDescriptor("Test Template Group", AllIcons.FileTypes.Xml)
    groupDescriptor.addTemplate(FileTemplateDescriptor("Layout (Yamato Daiwa Frontend).pug")) // notice lack of ".ft" extension, it is important
    return groupDescriptor
    }
    }
  3. Register factory in plugin.xml:
    <fileTemplateGroup implementation="com.example.TestFileTemplateGroupFactory"/>
    Now you should see your template in Editor | File and Code Templates.
  4. If you want to have possibility of creating new files from template, you can extend com.intellij.ide.actions.CreateFileFromTemplateAction:
    class TestCreateFromTemplateAction : CreateFileFromTemplateAction(
    "Test Template",
    "Creates file from Test Template",
    AllIcons.FileTypes.Xml) {

    override fun buildDialog(project: Project, directory: PsiDirectory, builder: CreateFileFromTemplateDialog.Builder) {
    builder
    .setTitle("New Test Template")
    .addKind("Test Template", AllIcons.FileTypes.Xml, "Layout (Yamato Daiwa Frontend).pug")
    }

    override fun getActionName(psiDirectory: PsiDirectory?, s: String, s1: String?): String {
    return "Test Template"
    }

    override fun createFile(name: String?, templateName: String?, dir: PsiDirectory): PsiFile? {
    val template = FileTemplateManager.getInstance(dir.project).getTemplate(templateName!!)
    return createFileFromTemplate(name, template, dir)
    }
    }
  5. Register the action in plugin.xml:
    <actions>
    <action id="Test.TestTemplate"
    class="com.github.tokugawatakeshi.temp.TestCreateFromTemplateAction">
    <add-to-group group-id="NewGroup"/> <!-- "NewGroup" is important -->
    </action>
    </actions>
  6. And that's it:
    New action in New group:

    Provide name:

    And the file is created:
1

Karol Lewandowski

Dear mr. Lewandowski,
thank you for the detailed explantions.

I believe it will work. 
I'll report once check above sequence of actions to finally actually close this ticket.

0

Hi Takeshi,

We've just published new articles on File and Code Templates:
https://plugins.jetbrains.com/docs/intellij/file-and-code-templates.html

It should clarify the topic, and if it does not, please let us know.

1

Karol Lewandowski

Dear mr. Lewandowski,
thank you for the answers and documentation.
I have carefully studied both your answer and documetantion, but I am very sorry about I still have some questions.

About template groups

In

val groupDescriptor = FileTemplateGroupDescriptor("Test Template Group", AllIcons.FileTypes.Xml)

does "group" mean the groups in "Other" tab of "File -> Settings -> Editor -> File and Code Templates"? If so maybe I should my templates below existing "Web" group?

I hope I'll understand what to do with related (child) template Layout (Yamato Daiwa Frontend).pug.child.0.styl.kt once will be answered to above question. Currently from the documentation I knew that it is currently not possible to keep this template as child in relation to Layout (Yamato Daiwa Frontend).pug.kt inside plugin).

Please note that adding templates with child/multiple files in custom plugins is currently not supported.

The lack of Kotlin examples in documentation

The MavenFileTemplateGroupFactory has been written by Java. May I ask you to add the Kotlin example to documentation?

I am sorry about I have no expirence with Kotlin, the great creation of your company, but because the plugin template has been suggested with Kotlin logic, I will not migrate my plugins to familiar for me Java. But I faces with next problems:

1. How I can use icons.OpenapiIcons.RepositoryLibraryLogo; in Kotlin?
2. How I can solve Cannot access 'java.lang.Object' which is a supertype of 'com.github.tokugawatakeshi.temp.TestFileTemplateGroupFactory. Check your module classpath for missing or conflicting dependencies error in factory class?

About code listing in steps 4 and 5

1. Where "Test Template" from line 2 and "Creates file from Test Template" will be used/displayed? 
2. Why we need to impelemnt `getActionName` method?
3. On what "action.id" referes?
4. Is "NewGroup" static value, not the value for exmaple?

I am very sorry about lot of question.
I believe it will be the good feedback for your documetantion.

0

Hi Takeshi,

About template groups:
You are correct about the "Other" tab. Groups should be created separately for each plugin/technology, so you can't add it to the existing one.

The lack of Kotlin examples in docs:
We rely on existing implementations and we choose the ones that explain the mechanisms best. You can copy this Java file and convert to Kotlin in IDE: https://www.jetbrains.com/help/idea/get-started-with-kotlin.html#b13357a
1. Sorry, I have no idea what are you referring to.
2. Check File | Project Structure... | Project | SDK - maybe you have JRE selected, but it should be JDK. Try e.g. corretto-11 if available or some other JDK.

About code listing:
I suggest relying on documentation more, as I wasn't 100% right about everything in my answer. Please read it carefully and check mentioned examples for better understanding.

1. It's an action name displayed on menus/buttons.
2. Because it is an abstract method and you have to implement it. You can customize it based on the parameters.
3. Action ID is just an ID that you can use to reference your action in other places, e.g., when you want to add existing action to multiple groups/places in IDE. See https://plugins.jetbrains.com/docs/intellij/basic-action-system.html for more information.
4. Yes.

Also, please keep this topic focused on file templates. If anything else comes up, please create a new topic (search for potential existing answers before you do it).

1

Please sign in to leave a comment.