- "一些常用的doxygen风格的注释
- "作者:谭金文
- "QQ: 26906391
- "Email: tanjinwen.8@qq.com
- "Copyright (c) 2008 任何人都可以自由获取、修改、发布该程序。
- "参考:本脚本参考了大量的网上资料以及VIM中文手册、doxygen user manual
- "不足之处:
- " 1. 函数注释并不能提取函数的参数列表和返回值。虽然www.vim.org有大量的doxygen风格函数注释脚本,
- "但是经过测试我还没有发现有完全工作正确的。提取函数参数列表的难点在于有字符串作为默认参数以及有函数指针
- "的参数。虽然已经想到了些方法,但是鉴于时间关系没有去实现。如果有你实现了该功能或者有好的想法,欢迎与
- "我进行交流。
- " 2. 通过键映射的方式添加注释后为插入模式,但是通过输入自定义命令后是普通模式
- "添加只包含一次文件的宏#ifndef ..#define #endif
- "使用方法:在普通模式下击键zho
- function InsertIncludeOnceTag()
- let CurTime = strftime("%Y_%m_%d_%H_%M") "将当前时间格式化后存入到CurTime当中去
- let CurFilePath = toupper(expand("%")) "expand(/"%/")获取当前编辑的文件路径
- let LastSlash = strridx(CurFilePath, "/") "找出路径中最后一个/
- let HeadTag1 = "#ifndef "
- let HeadTag2 = "#define "
- let HeadTag3 = "#endif //"
- "将非数字、字母替换为_
- let Suffix = substitute(strpart(CurFilePath, LastSlash + 1), "[^a-zA-Z0-9]", "_", "g")."_".CurTime
- let HeadTag1 = HeadTag1.Suffix
- let HeadTag2 = HeadTag2.Suffix
- let HeadTag3 = HeadTag3.Suffix
- "跳转到第一行并输入#ifndef ..
- exe "normal ggO".HeadTag1
- exe "normal o".HeadTag2
- exe "normal Go".HeadTag3
- "添加一个空白行
- exe "normal o"
- "添加两行空白行并将光标停在第四行
- normal 3GO
- normal 4GO
- endfunction
- "添加doxygen风格的文件注释
- "使用方法:在普通模式下击键zfc
- function InsertFileCommentTag()
- let l:File_Path = expand("%")
- let l:File_Name = strpart(l:File_Path, strridx(l:File_Path, "/") + 1)
- exe "normal ggO/**"
- exe "normal o* @file ".l:File_Name
- exe "normal o* @brief "
- let l:wRow = line(".")
- let l:wCol = col(".")
- exe "normal o* @author jinwentan.8@qq.com"
- exe "normal o* @version VERSION_PLACEHOLDER"
- exe "normal o* @date ".strftime("%Y/%m/%d")
- exe "normal o* @note "
- exe "normal o* @remarks Copyright 1998 - 2008 XXX Inc. All Rights Reserved."
- exe "normal o*/"
- "重新缩进文件
- exe "silent normal gg=G"
- "设置光标在@brief后面
- :call cursor(l:wRow, l:wCol)
- endfunction
- "添加doxygen风格的类注释
- "使用方法:在普通模式下,将光标停留在类名上面击键zcc
- function InsertClassCommentTag()
- let Class_Name = expand("<cword>")
- exe "normal O/**"
- exe "normal o* @class ".Class_Name
- exe "normal o* @brief "
- let l:wRow = line(".")
- let l:wCol = col(".")
- exe "normal o* @author jinwentan.8@qq.com"
- exe "normal o* @version VERSION_PLACEHOLDER"
- exe "normal o* @date ".strftime("%Y/%m/%d")
- exe "normal o* @note "
- exe "normal o*/"
- exe "silent normal gg=G"
- :call cursor(l:wRow, l:wCol)
- endfunction
- "添加doxygen风格的函数注释
- "使用方法:在普通模式下面,将光标停留在函数名称行击键zmc
- function InsertFunctionCommentTag()
- exe "normal O/**"
- exe "normal o* @brief "
- let l:wRow = line(".")
- let l:wCol = col(".")
- exe "normal o* @param "
- exe "normal o* @return "
- exe "normal o* @note "
- exe "normal o*/"
- exe "silent normal gg=G"
- :call cursor(l:wRow, l:wCol)
- endfunction
- "添加doxygen风格的结构体注释
- "使用方法:在普通模式下,将光标停留在结构体名称上面击键zsc
- function InsertStructCommentTag()
- let l:Struct_Name = expand("<cword>")
- exe "normal O/**"
- exe "normal o* @struct ".l:Struct_Name
- exe "normal o* @brief "
- let l:wRow = line(".")
- let l:wCol = col(".")
- exe "normal o* @note "
- exe "normal o*/"
- exe "silent normal gg=G"
- :call cursor(l:wRow, l:wCol)
- endfunction
- "添加doxygen风格的枚举体注释
- "使用方法:在普通模式下,将光标停留在枚举体名称上面击键zec
- function InsertEnumCommentTag()
- let l:Enum_Name = expand("<cword>")
- exe "normal O/**"
- exe "normal o* @enum ".l:Enum_Name
- exe "normal o* @brief "
- let l:wRow = line(".")
- let l:wCol = col(".")
- exe "normal o* @note "
- exe "normal o*/"
- exe "silent normal gg=G"
- :call cursor(l:wRow, l:wCol)
- endfunction
- "添加doxygen风格的模块定义注释
- "使用方法:在visual模式下选定要加入到模块的行,击键zgc
- function InsertGroupCommentTag()
- "获取选定的行
- let l:wBegin = line("'<")
- let l:wEnd = line("'>")
- exe "normal ".l:wBegin."G"
- exe "normal O/**"
- exe "normal o* @defgroup "
- let l:wRow = line(".")
- let l:wCol = col(".")
- exe "normal o* @brief "
- exe "normal o* @detail "
- exe "normal o* @{"
- exe "normal o */"
- exe "normal o"
- let l:wEnd = l:wEnd + 7
- exe "normal ".l:wEnd."G"
- exe "normal o/** @} */ //end define of group"
- exe "silent normal gg=G"
- :call cursor(l:wRow, l:wCol)
- endfunction
- "将代码块添加到特定模块的Doxygen注释
- "使用方法:在visual模式下选定要加入到模块的行,击键zag
- function InsertAddToGroupCommentTag()
- "获取选定的行
- let l:wBegin = line("'<")
- let l:wEnd = line("'>")
- exe "normal ".l:wBegin."G"
- exe "normal O/*! @addtogroup "
- let l:wRow = line(".")
- let l:wCol = col(".")
- exe "normal o* @{"
- exe "normal o */"
- exe "normal o"
- let l:wEnd = l:wEnd + 4
- exe "normal ".l:wEnd."G"
- exe "normal o/** @} */ //end addtogroup"
- exe "silent normal gg=G"
- :call cursor(l:wRow, l:wCol)
- endfunction
- "在符号(宏、成员变量、全局变量)后增加doxygen风格的注释
- "使用方法:普通模式下,在符号定义行击键zpc
- function InsertItemPostCommentTag()
- exe "normal A/t"
- let l:wRow = line(".")
- let l:wCol = col(".")
- while l:wCol < 50
- exe "normal A "
- let l:wCol = col(".")
- endwhile
- exe "normal A/*!< */"
- exe "normal 2h"
- endfunction
- "在符号(宏、成员变量、全局变量)上方增加doxygen风格的注释或者是函数的简单说明
- "使用方法:在普通模式下,在符号定义行击键zuc
- function InsertItemUpCommentTag()
- exe "normal O"
- exe "normal o/// "
- endfunction
- nmap zho <Esc>:call InsertIncludeOnceTag()<CR>i
- nmap zfc <Esc>:call InsertFileCommentTag()<CR>i
- nmap zcc <Esc>:call InsertClassCommentTag()<CR>i
- nmap zmc <Esc>:call InsertFunctionCommentTag()<CR>i
- nmap zsc <Esc>:call InsertStructCommentTag()<CR>i
- nmap zec <Esc>:call InsertEnumCommentTag()<CR>i
- nmap zpc <Esc>:call InsertItemPostCommentTag()<CR>i
- nmap zuc <Esc>:call InsertItemUpCommentTag()<CR>i
- vmap zgc <Esc>:call InsertGroupCommentTag()<CR>i
- vmap zag <Esc>:call InsertAddToGroupCommentTag()<CR>i
- command! -nargs=0 DoxHeader :call InsertIncludeOnceTag()
- command! -nargs=0 DoxFile :call InsertFileCommentTag()
- command! -nargs=0 DoxClass :call InsertClassCommentTag()
- command! -nargs=0 DoxFunction :call InsertFunctionCommentTag()
- command! -nargs=0 DoxStruct :call InsertStructCommentTag()
- command! -nargs=0 DoxEnum :call InsertEnumCommentTag()
- command! -nargs=0 DoxItemPost :call InsertItemPostCommentTag()
- command! -nargs=0 DoxItemUp :call InsertItemUpCommentTag()
- command! -nargs=0 DoxDefGroup :call InsertGroupCommentTag()
- command! -nargs=0 DoxAddToGroup :call InsertAddToGroupCommentTag()
- "在.h/.cpp/.c间跳转
- function SwitchImpDeclare()
- let l:File_Name = expand("%")
- let l:LastDot = strridx(l:File_Name, ".")
- let l:Suffix = strpart(l:File_Name, l:LastDot + 1)
- let l:Base = strpart(l:File_Name, 0, l:LastDot)
- if l:Suffix == "cpp" || l:Suffix == "c"
- if bufexists(l:Base.".h")
- exe ":b ".l:Base.".h"
- elseif filereadable(l:Base.".h")
- exe ":e ".l:Base.".h"
- else
- echo "Can't edit file: ".l:Base.".h"
- endif
- elseif l:Suffix == "h"
- if bufexists(l:Base.".cpp")
- exe ":b ".l:Base.".cpp"
- elseif bufexists(l:Base.".c")
- exe ":b ".l:Base.".c"
- elseif filereadable(l:Base.".cpp")
- exe ":e ".l:Base.".cpp"
- elseif filereadable(l:Base.".c")
- exe ":e ".l:Base.".c"
- else
- echo "Can't edit file: ".l:Base.".cpp or "l:Base.".c"
- endif
- endif
- endfunction
- function IncludeDeclare()
- let l:File_Name = expand("%")
- let l:LastDot = strridx(l:File_Name, ".")
- let l:Suffix = strpart(l:File_Name, l:LastDot + 1)
- let l:Base = strpart(l:File_Name, 0, l:LastDot)
- if l:Suffix == "cpp" || l:Suffix == "c"
- exe "normal o#include /"".strpart(l:Base, strridx(l:Base, "/") + 1).".h/""
- elseif l:Suffix == "h"
- echo "Current file is not a c/c++ implement file"
- endif
- endfunction
- command! -nargs=0 BS :call SwitchImpDeclare()
- command! -nargs=0 IH :call IncludeDeclare()
修正“苦练1天半,终于写出了一些常用doxygen风格的vim注释脚本”部分BUG
最新推荐文章于 2023-06-19 15:06:58 发布