IntelliJ如何实现块注释回车后自动添加每行前置*?
在IntelliJ中实现块注释自动填充星号的方法
在IntelliJ中,我可通过CmdOpt/快捷键创建块注释,但该操作不会为所有行自动填充
*;且按下回车键换行时,新行不会自动带上*。请问是否有办法实现自动填充,达到如下示例的效果?/* * Copyright (c) 2022 by Bob Jones * * The MIT license. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions:
方法一:开启IDE自带的注释自动格式化
打开IntelliJ设置(快捷键Cmd,),按以下路径操作:
- 进入
Editor→Code Style,选择你当前使用的编程语言(比如Java、Python等) - 切换到
Code Generation标签页 - 在
Comment code区域,勾选Add a space at comment start和Reformat block comment on Enter选项
设置完成后,用CmdOpt/创建块注释时,IDE会自动给每行补全*;按下回车换行时,新行也会自动带上*和前置空格,完全符合你要的效果。
方法二:自定义Live Template生成规范注释
如果需要更个性化的注释模板(比如固定的版权声明、许可证内容),可以用Live Template来实现:
- 打开设置(Cmd,),进入
Editor→Live Templates - 点击右侧
+号,选Template Group,新建一个自定义组(比如叫My Comments) - 选中新建的组,再次点
+号,选Live Template - 设置一个缩写(比如
block),在模板文本框里输入:
/* * $END$ */
- 在
Applicable in里勾选你需要的语言,点击Edit variables还可以添加动态变量(比如自动填充当前日期、作者名) - 保存后,输入缩写按Tab就能生成规范块注释,换行时同样会自动补
*
内容的提问来源于stack exchange,提问作者David542
相关产品推荐
相关产品推荐

