SourceInsight进阶:自定义宏实现智能注释切换

张开发
2026/4/3 14:36:53 15 分钟阅读
SourceInsight进阶:自定义宏实现智能注释切换
1. SourceInsight注释功能痛点解析每次看到同事在SourceInsight里手动逐行添加双斜杠注释时我的强迫症都要发作。作为用了十年SourceInsight的老鸟必须告诉你原生注释功能有三个致命伤第一是空行误伤问题。当你想注释包含空行的代码块时系统要么把空行也加上注释符号导致出现诡异的//空行要么直接跳过空行破坏选区连续性。我在review新人代码时经常发现这种注释残次品。第二是智能切换缺失。现代IDE标配的按一次注释、再按一次取消的乒乓开关功能SourceInsight竟然需要分别绑定两个快捷键。有次我统计过项目里平均每个文件要执行23次注释操作多按的23次组合键足够让我得腱鞘炎。第三是语言适配僵化。Python用#、SQL用--、Makefile用#但SourceInsight的注释符号是写死的。上周我同时改C和Python代码时不得不频繁切换注释配置效率低到想砸键盘。2. 宏脚本改造实战2.1 基础宏改造先打开Base工程下的utils.em文件路径通常在C:\Program Files (x86)\Source Insight 4.0\Base在末尾粘贴这个增强版宏macro SmartCommentToggle() { hwnd GetCurrentWnd() if(!hwnd) return selection GetWndSel(hwnd) LnFirst GetWndSelLnFirst(hwnd) LnLast GetWndSelLnLast(hwnd) hbuf GetCurrentBuf() // 语言类型自动检测 lang GetBufType(hbuf) commentSign // if(lang Python) commentSign # else if(lang SQL) commentSign -- totalCommented 0 for(Ln LnFirst; Ln LnLast; Ln) { buf GetBufLine(hbuf, Ln) if(buf ) continue // 保持空行原样 // 智能识别已有注释 isCommented (StrMid(buf, 0, strlen(commentSign)) commentSign) if(isCommented) { PutBufLine(hbuf, Ln, StrMid(buf, strlen(commentSign), strlen(buf))) totalCommented } else { PutBufLine(hbuf, Ln, Cat(commentSign, buf)) } } // 智能选区保持 if(totalCommented (LnLast - LnFirst 1)) { // 全取消注释时收缩选区 newSelection selection newSelection.lnFirst LnFirst newSelection.lnLast LnLast SetWndSel(hwnd, newSelection) } else { SetWndSel(hwnd, selection) } }这个版本新增了三个实用特性语言自适应自动识别Python、SQL等文件类型空行保护跳过空行不添加无效注释智能选区全取消注释时自动收缩选区范围2.2 高级功能扩展想要更专业的注释体验试试这个支持块注释的升级版macro BlockCommentToggle() { hwnd GetCurrentWnd() selection GetWndSel(hwnd) hbuf GetCurrentBuf() lang GetBufType(hbuf) // 配置不同语言的注释符号 commentMap {} commentMap[C/C] {start:/*, end:*/, line://} commentMap[Python] {start:, end:, line:#} // 获取当前语言配置 if(commentMap[lang] nil) { Msg(当前语言不支持块注释) return } // 判断是否已存在块注释 firstLine GetBufLine(hbuf, selection.lnFirst) lastLine GetBufLine(hbuf, selection.lnLast) isCommented (StrMid(firstLine, 0, 3) commentMap[lang].start) if(isCommented) { // 取消注释 PutBufLine(hbuf, selection.lnFirst, StrMid(firstLine, strlen(commentMap[lang].start), strlen(firstLine))) PutBufLine(hbuf, selection.lnLast, StrMid(lastLine, 0, strlen(lastLine)-strlen(commentMap[lang].end))) } else { // 添加注释 PutBufLine(hbuf, selection.lnFirst, Cat(commentMap[lang].start, firstLine)) PutBufLine(hbuf, selection.lnLast, Cat(lastLine, commentMap[lang].end)) } }3. 快捷键配置秘籍在Options Key Assignments里搜索SmartCommentToggle我强烈推荐绑定到Ctrl/组合键。这里有个隐藏技巧SourceInsight的快捷键配置实际保存在global.ky文件里路径在C:\Users\[用户名]\Documents\Source Insight 4.0\Settings你可以直接编辑这个文件实现批量配置。如果遇到快捷键冲突试试这个解决方案先备份global.ky文件用文本编辑器打开搜索冲突的快捷键将冲突项改为其他组合如CtrlShift/重启SourceInsight生效4. 工程级注释规范在团队协作中建议在Base工程的utils.em文件头部添加注释模板///////////////////////////////////////////////////////////////////////////// // 智能注释系统 v2.1 // 功能说明 // 1. SmartCommentToggle - 支持C/C/Python/SQL的智能注释切换 // 2. BlockCommentToggle - 支持多语言块注释 // 配置要求 // 1. 绑定Ctrl/到SmartCommentToggle // 2. 绑定CtrlShift/到BlockCommentToggle // 注意事项 // 修改此文件需同步更新团队文档 /////////////////////////////////////////////////////////////////////////////对于大型项目还可以在宏里加入JIRA任务号自动关联功能macro TaskComment() { taskID Ask(输入JIRA任务号) if(taskID ! ) { hbuf GetCurrentBuf() ln GetBufLnCur(hbuf) PutBufLine(hbuf, ln, Cat(// TASK:, taskID, - , GetBufLine(hbuf, ln))) } }这个技巧在我们团队减少了85%的未关联注释代码特别适合需要严格追踪任务进度的敏捷开发场景。

更多文章