前言
虽然VSCode写代码是真的方便,还有各种插件,最喜欢的就是一键注释插件,但是有时候需要用其他编译器,虽然也能用VSCode编辑,不过开多个软件,还要来回切换,感觉还是太麻烦了,换一个环境,又要下载这个下载那个。
用AHK可以实现一键注释的功能,且不依赖环境,携带方便,打包成exe丢U盘或者云盘,去哪里都能直接用。
主要功能:
- 双击"/":插入单行注释
- 输入"/"或“#”+名字+空格:一键插入指定注释。
1)/file:插入文件头注释,通常在.c文件开头使用,
2)/head:插入.h文件模板
3)/func:插入函数注释 - 输入"#"+类型+空格:一键插入代码模板
1)#type:插入结构体模板
2)#if:插入if模板
3)#sw:插入switch模板
内容可以自行修改,更多功能也可以自行添加
代码
#Requires AutoHotkey v2.0
; Doxygen风格注释热键
; 文件信息
file_info := '
(
/**
* @file #filename#
* @brief This is a brief description.
* @details This is the detail description.
* @author 霖夜妖妖
* @date date
* @version V0.01
* @par Copyright (c):
* XXX公司
* @par History:
* version: author, date, desc
*/
)'
; 头文件
head_info := '
(
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef __cplusplus
}
#endif
)'
; 函数注释
func_info := '
(
/**
* @brief 自定义类别的member_funtion说明…
* @param[in] int a 参数a的说明
* @param[out] int b 参数b的说明
* @return 传回a+b
*/
)'
; 全局变量注释
gloab_info := '
(
/** Description of global variable */
)'
; 代码注释(Doxygen 对于批注是视为在解释后面的程序代码,因此批注前面的代码需要使用下面格式,即//用于解释后面代码,///<用于解释前面代码)
code_info := '
(
///< Description of the member variable
)'
str_past(str)
{
old := ClipboardAll()
A_Clipboard := str
Send("^v")
Sleep(100)
A_Clipboard := old
}
~$/::{
if (KeyWait("/", "T0.2"))
{
if (KeyWait("/", "D T0.2")) ; Double press
{
; MsgBox("Double press")
Send("{BackSpace 2}")
str_past(code_info)
}
else ; Single press
{
; MsgBox("Single press")
; Send("/")
}
}
else ; Long press
{
; MsgBox("Long press")
Send("{BackSpace}")
str_past(gloab_info)
Sleep(100)
}
}
::#file::
::/file::{
str_past(file_info)
}
::#head::
::/head::{
str_past(head_info)
}
::#func::
::/func::{
str_past(func_info)
}
::#type:: {
str := '
(
typedef struct _xxx_s {
uint8_t a;
}xxx_t;
)'
str_past(str)
}
::#if:: {
str := '
(
if {
} esle {
}
)'
str_past(str)
}
::#sw:: {
str := '
(
switch(xxx){
case 1:
break;
default: break;
}
)'
str_past(str)
}
配合GUI和文件操作就是一键代码生成器,甚至可以直接生成整个工程。不过工程比较大的话,就不适合用str的形式写到代码里了,可以考虑用下载,模板代码保存到github,然后脚本访问并下载下来,修改工程部分文件或内容。