idea提供很多模板从而提高编写代码的效率,比如说一些经常用的代码及生成文件时的模板。

第一节:实时代码模板
顾名思义,实时代码模板是指在编写程序中输入一段固定的字母可以快速生成自己想要的代码,例如:sout然后按Tab键会自动生成System.out.println();等等很多包括java、jsp、xml、css等。idea内置了很多模板这里都不在介绍了,idea还支持自定义模板,模板分组等,都在live templates下,自定义模板出来可以快速生成自己想要的代码外还可以比如定义方法注释模板文件注释模板等。
新建动态模板分组


定义自己的模板及模板说明还有需要模板输出的代码样式,参数用$$拼写。

选择动态模板的适用范围

编辑代码中的参数,后面选中表示鼠标不停留,idea自带了很多内置函数,可以参考官网介绍:https://www.jetbrains.com/help/idea/2017.1/creating-and-editing-template-variables.html

设定方法注释的模板,这个没有固定格式,可根据自己公司规定。

这里需要提一下,默认date和time函数式没有格式化的,但是输出的格式并不友好,所以这里可以根据自己喜好输出时间格式(对于强迫症患者很受用)

IntelliJ IDEA 的实时代码模板保存在/templates 目录下,在输入sys 后按Tab 键,即立即生成预设语句。如果按Ctrl + J 则会先提示与之匹配的实时代码模板介绍,然后还需按Enter 才可完成预设语句的生成。
第二节:文件模板
这个下面可以定义很多文件类型的模板,我们就以class为例

idea的内置变量,这里就不多介绍了。
${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.
第三节:Postfix Completion
Postfix Completion 功能本质上也是代码模板,只是它比 Live Templates 来得更加便捷一点点而已。也很通俗易懂,这里就不多做介绍了。

网友评论