注意:本文适用于IntelliJ IDEA相关产品的所有IDEA

前言

相信大家在框架源码或者是开源项目中见过一下类似的头文件注释

/**
 * @time 2018-2-28 09:13:52 
 * @version 1.00
 * @author L_zhp
 * @see Action
 */
 public class Demo{
     public static void main(String [] args){
         //******
     }
 }

当然有些注释还会更加详细,但是大家有没有发现,基本上同一个项目中的头文件注释都基本相似,以程序员的思想咱们就应该想到应该有配置文件之类的东西了,好话不多少直接开搞。

配置流程

1、打开设置界面

通过点击File->Settings开启设置页面
设置页面

2、开启File and Code Templates菜单

分别依次点击Editor->File and Code Templates
File and Code Templates开启方式
开启后的File and Code Templates的菜单页基本是这样的
File and Code Templates菜单页面

3、添加公用的文件头模板

选择Includes标签页,并点击添加按钮。(PS:目前使用的是IntelliJ IDEA 2017.3版本的,可能添加按钮不在同一位置)
公用头文件模板
点击添加后,会自动添加一个新的条目。依次修改名字以及扩展名
点击添加后的页面

4、编辑公用头文件注释模板

这里我使用JSX文件做一个小例子
JSX文件模板样例
关于相关特殊命令的简介如下:


In this template, you can specify a code fragment to be included into file templates (Templates tab) with the help of the #parse directive. One template can be shared by several file templates. Along with static text, code and comments, you can also use predefined variables that will then be expanded like macros into the corresponding values. 
Predefined variables will take the following values:
${PACKAGE_NAME}

name of the package in which the new file is created
${USER}

current user system login name
${DATE}

current system date
${TIME}

current system time
${YEAR}

current year
${MONTH}

current month
${MONTH_NAME_SHORT}

first 3 letters of the current month name. Example: Jan, Feb, etc.
${MONTH_NAME_FULL}

full name of the current month. Example: January, February, etc.
${DAY}

current day of the month
${DAY_NAME_SHORT}

first 3 letters of the current day name. Example: Mon, Tue, etc.
${DAY_NAME_FULL}

full name of the current day. Example: Monday, Tuesday, etc.
${HOUR}

current hour
${MINUTE}

current minute
${PROJECT_NAME}

the name of the current project
 Apache Velocity template language is used
5、将公用的头文件模板,引用到相关文件模板中

选择Files标签,然后选择自己准备新建时自动添加注释的文件类型(PS:如果没有自己想要修改后缀名的文件,也可进行手动添加,添加方式参考上一步)
编写文件模板

6、创建具体的文件

单击右键,选择New你会惊奇的发现,你刚刚创建的文件模板,竟然在菜单中,哈哈
创建新文件

最后

感受下成果
这里写图片描述
新创建的文件
这里写图片描述

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐