java code templates_IntelliJ IDEA:File and code templates

IntelliJ IDEA:File and code templates

File templates是一套在创建一个新的文件时被自动生成的内容的规范。取决于你将要创建的文件的种类,templates会在所有的那种文件中提供一套按照行业标准或你们团队协作的约定或考虑其它因素的初始化的格式化的代码。

IDEA 为所有支持的文件类型提供了已经定义好的templates。templates会在使用者创建一个新文件时提示用户使用。被建议使用的文件类型的集合取决于项目(module)和配置,还取决于在项目工具窗口(Project tool window)中,你当前(要创建/插入新文件)的位置的性质。比如说,IDEA将不会在Java source和test目录歪的位置提示你创建一个java class类型的文件。

作用域问题

File templates在 Editor | File and Code Templates page of IntelliJ IDEA settings (Ctrl+Alt+S) 中被管理。这一设置页面的设置有两个作用域:

保存在默认作用域下的设置管理存在于整个workspace中templates。这些设置存储在fileTemplates下的 IDE configuration directory

保存在项目作用域下的设置管理其对应项目下的templates。这些设置被存储在项目文件夹下的.idea/fileTemplates。这些templates可以在团队成员间共享。

一开始,templates列表中只有由IDEA提供的预先定义好的templates。其中一些是internal内置的templates的名字会用粗体展示。使用者调整过的和自定义的templates的名字以蓝色进行展示。

templates的分类

IDEA把templates分为以下几种进行使用,它们也分别对应设置页面中的几个tab:

Files tab:其中都是可以被用来创建新文件的file templates。

Includes tab:其中都是可以插入到file templates中的可被复用的内容的templates,比如说,设置一个文件头template,它能自动生成其被创建的时间,创建它的作者的名字。这个文件头就可被复用的嵌入到各种文件中。

Code tab:其中都是为被IDEA用来创造各种各样的结构的代码片段所准备的internal templates。你可以修改这个tab下的template,但你不能创建该分类下的新的templates.

Other tab:其中都是被各种各样的应用框架(如Android开发,EJB规范,Mybatis开发等等)所使用的templates。你可以编辑修改该tab下的已存在的内置的templates,但不能创建新的。

创建一个新的file/include template

下述步骤描述了如何创建一个新的file template。对于创建新的include templates来说,步骤十分相似。

从头新建一个新的file template

In the Settings/Preferences dialog (Ctrl+Alt+S), select Editor | File and Code Templates.

On the Files tab, click a8c45b31877d1c53391d33cfc57831fe.png and specify the name, file extension, and body of the template.

Apply the changes and close the dialog.

复制一个已经存在的file template

In the Settings/Preferences dialog (Ctrl+Alt+S), select Editor | File and Code Templates.

On the Files tab, click be8b92bc9683b825a9625f2ed3fe045a.png and modify the name, file extension, and body of the template as necessary.

Apply the changes and close the dialog.

把一个文件设置为一个file template

Open a file in the editor.

Choose Tools | Save File as Template from the menu.

In the Save File as Template dialog that opens, specify the new template name and edit the body, if necessary.

Apply the changes and close the dialog.

template的语法

File和code templates按照Velocity Template Language (VTL)语法书写。VLT语法使用了以下的构件:

写死的内容。包括代码,注释等等。这些内容会被原样展现。

变量(Variables),最后呈现时会被实际值替换。

命令:比如#parse#set, #if

下面这个例子是在IDEA创建一个.java文件时的默认模板

#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

#parse("File Header.java")

public class ${NAME} {

}

在这个模板中

#if命令被用来检查包名是否非空。如果非空,则为package声明增加由${PACKAGE_NAME}变量指明的包名。

#parse命令被用来在该template(或者说文件)中插入另一个名为File Header.java的template的内容。

然后这个template声明了一个public class并使用${NAME}变量(存储了新文件的名字)指明的类名。

我怀疑这里的internal指的是被IDEA依赖的。也就是说IDEA要正常工作必须依靠的。因为这个找不到合适的短词语进行翻译,故在此说明,此后遇到该词不作翻译。 ↩

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值