程序媛阿历终于要写第一篇关于开发的文章了!!!
阿历用Intellij Idea开发,版本是Ultimate 2017.2.2。这周写代码的时候,发现编辑注释很麻烦,不但要写注释符,还要写名字、日期、描述等等,很繁琐。工具是用来提高效率的,为了提高开发的效率,我去网上搜索了Intellij Idea自动添加注释的方法。因为现有总结不够全面,而且有的方法对于我的版本不太奏效,于是我整理了自动添加注释的方法(很抱歉忘记记录参考的网址了),亲测有效。
自动添加注释有两类方法:1、创建文件时,idea自动生成注释;2、在编辑源文件时,插入注释。下面具体介绍。
1、创建文件时,idea自动生成注释
第一种很简单,一张图就可以说清楚。过程是,在设置中找到“File and Code Templates”,在类文件的模板中添加注释。具体如下图(图1)所示:
(1)首先打开设置,点File,Settings即可打开下图界面,或者直接用快捷键“Ctrl+Alt+S”(我特别喜欢用快捷键^^)打开设计界面。
(2)在搜索框中输入“File and Code Templates”,打开“File and Code Templates”。
(3)在右侧的Files一栏中,找到Class,编辑注释。下面的Description介绍了可以使用的系统变量,按需索取。
(4)最后点击Apply和OK就完成啦。

至此,你可以新建一个class文件,测试一下啦!
2、在编辑源文件时,插入注释
这个步骤有点多,但是同样很简单。大致过程是打开“Live Templates”,新建我们自己的template,编辑注释文本,定义应用文件,编辑变量。具体看下面:
(1)打开Settings,找到“Live Templates”,点右侧“+”,新建一个“Template Group...”,见图2.1、2.2。


(2)选中刚建好的Group,再点右侧“+”,创建一个“Live Template”,见图2.3。

(3)编辑Template的缩写(插入注释时用),在Template text中编辑注释模板(如图2.4)。注意变量的定义方式,用两个$包围变量名,例如变量user的定义为$user$。
(4)定义应用这个Template的环境,点击Define。我选择了Java(如图2.5)。


(5)编辑变量,点击“Edit variables”,如图2.6、2.7。给变量选择表达式或者定义默认值。


(6)最后点击“Apply”和“OK”(图),就完成啦。

试试这个方法吧(图2.9、2.10):在class源文件编辑器中,输入步骤(3)设置的模板缩写,例如我设置的是“cmmt”。Idea会显示提示,然后回车,就插入注释了。


结语:
成功设置了自动添加注释,感觉还是很神奇的,佩服开发工具的工程师。
虽然研究设置花费一定的时间,但是成功之后用得就很爽了,简直是一劳永逸!
以后会尽量多写跟技术相关的文章~~毕竟是个程序媛,哈哈。加油!
希望本文对你有帮助。
网友评论