Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we write our own code template file in IntelliJ IDEA

I need to create a sample code template in my IntelliJ IDEA project so that everybody in the team can also kind of import it in their IDEs and use it.

I am able to do it on my own machine by changing the "class" template myself, but i just don't have any way to make it available to my team like exporting it to a file that can be used, in Eclipse it is possible to have one sampleCodeFormatter.xml file that everybody can import in their eclipse workspace.

How does the same thing works in IntelliJ IDEA?

like image 241
ashish Avatar asked Oct 31 '13 21:10

ashish


People also ask

How do I create a project template in IntelliJ?

Create a project from a templateClick New Project on the Welcome screen or select File | New Project from the main menu. In the dialog that opens, click the required template in the Templates section on the left.

How do I save a template in IntelliJ?

Save a file as a template Open a file in the editor. From the main menu, select File | Save File as Template. In the Save File as Template dialog, specify the new template name and edit the body, if necessary. Apply the changes and close the dialog.

How do I create a custom completion code in IntelliJ?

By default, IntelliJ IDEA displays the code completion popup automatically as you type. If automatic completion is disabled, press Ctrl+Shift+Space or choose Code | Code Completion | Type-Matching from the main menu. If necessary, press Ctrl+Shift+Space once again.


2 Answers

First, in IntelliJ, open Settings (ctrl-alt-s), under IDE Settings find File and Code Templates and have a look at the Templates tab.

You will see some templates there already, for example there is one called Class:

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #parse("File Header.java") public class ${NAME} { } 

Note that it includes File Header, which is found under the Includes tab.

Now, you can easily create your own file templates here. Under the Templates tab, just hit the green "Add" button, give your new template a name (Foo ?),ensure the extension is java and write a valid java class.

Save, and your newly configured template exists here:

C:\Users\{USER}\.IntelliJIdea12\config\fileTemplates\Foo.java 

You should be able to share this file with your team.

A neat feature is that from within the Project pane of a java project, under a source root, hit alt-enter, choose a New Java Class and under the Kind drop-down - voila! You can see your Foo template as a valid option.

like image 192
vikingsteve Avatar answered Sep 28 '22 04:09

vikingsteve


Here's the contents of my File Header.java

/**  * Project: ${PROJECT_NAME}  * Package: ${PACKAGE_NAME}  * <p>  * User: ${USER}  * Date: ${DATE}  * Time: ${TIME}  * <p>  * Created with IntelliJ IDEA  * To change this template use File | Settings | File Templates.  */ 
like image 24
ranma2913 Avatar answered Sep 28 '22 04:09

ranma2913