本文简单介绍IntelliJ IDEA中文件代码模板的使用,参考IntelliJ IDEA 简体中文专题教程,英文好的同学可以查看英文官网文档
文件代码模板的介绍
文件代码模板:我们在项目中创建某些类型文件时,已模板的形式预设代码内容在对应的这些新文件中
以预设的Singleton模板为例
新建Java Class时可以选择使用Singleton模板
新建的文件如下
文件代码模板的设置
文件代码模板的设置在File -> Settings -> Editor -> File and Code Templates下,如下图
- 基本常见的文件类型都都支持使用文件代码模板
- IntelliJ IDEA 的文件代码模板是可以使用 Velocity Template Language (VTL) 进行书写的。如图上的
#if ... #end和#parse都是 VTL 的语法 - 图示的操作按钮作用分别是
-
Create Template创建一个文件代码模板 -
Remove Template删除一个文件代码模板,预设模板是不允许删除的,只能删除预设之外的新增的 -
Copy Template复制一个文件代码模板 -
Reset To Default对被修改的预设文件代码模板,还原到默认状态
-
Other选项卡下有更多的文件类型,不用担心有不支持的文件类型,常用的基本都被涵盖了,如下
Includes选项卡下是供其他文件代码模板的头文件模板,如下
引用的File Header.java在Includes选项卡下
Code选项卡 中主要是 IntelliJ IDEA 自动帮我们生成某些代码的时候引用的模板,无法新建、删除 Code 里面的代码模板,如下
下面举两个制作文件代码模板的例子
xml文件模板
1.第一步,制作
2.第二步,使用
添加 Java Class 注释
1.第一步,制作
-
${DESCRIPTION}是我们自己定义的变量,自定义变量格式有两种方式:$变量名或${变量名} - 当我们需要用到一个固定值的自定义变量的时候并且该变量多个地方被引用,我们可以通过 VTL 语法的
#set( $变量名 = "变量值内容" ); 来设置
2.第二步,使用
文件代码模板预设的变量
-
${PACKAGE_NAME}- the name of the target package where the new class or interface will be created. -
${PROJECT_NAME}- the name of the current project. -
${FILE_NAME}- the name of the PHP file that will be created. -
${NAME}- the name of the new file which you specify in the New File dialog box during the file creation. -
${USER}- the login name of the current user. -
${DATE}- the current system date. -
${TIME}- the current system time. -
${YEAR}- the current year. -
${MONTH}- the current month. -
${DAY}- the current day of the month. -
${HOUR}- the current hour. -
${MINUTE}- the current minute. -
${PRODUCT_NAME}- the name of the IDE in which the file will be created. -
${MONTH_NAME_SHORT}- the first 3 letters of the month name. Example: Jan, Feb, etc. -
${MONTH_NAME_FULL}- full name of a month. Example: January, February, etc.