vim+Doxygen实现注释自动生成

前端之家收集整理的这篇文章主要介绍了vim+Doxygen实现注释自动生成前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

借鉴文章:https://www.cnblogs.com/zzqcn/p/4660615.html


为自己的代码写好注释是一个良好的习惯,而编写Doxygen风格的注释更是可以通过doxygen工具为代码自己生成文档,非常好用。DoxygenToolkit(https://github.com/vim-scripts/DoxygenToolkit.vim)就是这样的一个插件。Doxygen插件用Vundle插件管理器来自动安装,首先安装Vundle:

1、git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
如果目录.vim/bundle不存在请先创建

2、在~/.vimrc中添加如下内容

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vundle
set nocompatible              " be iMproved,required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively,pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle,required
Plugin 'VundleVim/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
"Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
"Plugin 'L9'
" Git plugin not hosted on GitHub
"Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
"Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
"Plugin 'rstacruz/sparkup',{'rtp': 'vim/'}
" Avoid a name conflict with L9
"Plugin 'user/L9',{'name': 'newL9'}

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes,instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

从其中的注释可以知道,Vundle支持多种形式的插件源,并给出了示例。这些插件包括:github上的插件、http://vim-scripts.org/vim/scripts.html上的插件、非github上的git插件、本地硬盘上的插件等。

3、打开vim,运行 :PluginInstall 命令来自动安装插件,过程中有可能需要输入github用户名和密码。等待Vundle安装完成即可。


安装Doxygen:

在~/.vimrc中的Vundle插件列表区域中添加DoxygenToolkit的源位置

Plugin 'vim-scripts/DoxygenToolkit.vim'

.vimrc中Vundle区域如下:


保存后退出,再打开vim,运行:PluginInstall命令安装。

安装好Doxygen后,打开代码文件,即可通过:DoxLic,:DoxAuthor,:Dox添加license说明、作者版本说明和函数说明,我把license说明和作者版本说明整合了一下,并加入了公司名称的变量,并修改了作者版本说明字段对齐,这些修改在~/.vim/bundle/DoxygenToolkit.vim/plugin/DoxygenToolkit.vim中完成。代码如下:

修改变量,licenseTag用COMPANY做公司名占位符,好用.vimrc中定义的公司名替换:

let s:licenseTag = "Unpublished copyright. All rights reserved. This material contains\<enter>"
let s:licenseTag = s:licenseTag . "proprietary information that should be used or copied only within\<enter>"
let s:licenseTag = s:licenseTag . "COMPANY,except with written permission of COMPANY.\<enter>"
if !exists("g:DoxygenToolkit_briefTag_lic_pre")
  let g:DoxygenToolkit_briefTag_lic_pre = "@brief:   "
endif
if !exists("g:DoxygenToolkit_briefTag_pre")
  let g:DoxygenToolkit_briefTag_pre = "@brief: "
endif
if !exists("g:DoxygenToolkit_fileTag")
  let g:DoxygenToolkit_fileTag = "@file:    "
endif
if !exists("g:DoxygenToolkit_authorTag")
  let g:DoxygenToolkit_authorTag = "@author:  "
endif
if !exists("g:DoxygenToolkit_dateTag")
  let g:DoxygenToolkit_dateTag = "@date:    "
endif
if !exists("g:DoxygenToolkit_versionTag")
  let g:DoxygenToolkit_versionTag = "@version: "
endif


修改DoxygenLicenseFunc函数,整合作者版本信息,这里默认版本号为1.0,单独添加作者版本信息时要输入版本号

""""""""""""""""""""""""""
" Doxygen license comment
""""""""""""""""""""""""""
function! <SID>DoxygenLicenseFunc()
  call s:InitializeParameters()

  " Test authorName variable
  if !exists("g:DoxygenToolkit_companyName")
    let g:DoxygenToolkit_companyName = input("Enter name of your company: ")
  endif
  if !exists("g:DoxygenToolkit_authorName")
    let g:DoxygenToolkit_authorName = input("Enter name of the author (generally yours...) : ")
  endif
  mark d

  " Get file name
  let l:fileName = expand('%:t')
  let l:year = strftime("%Y")
  let l:copyright = "Copyright (c) "
  let l:copyright = l:copyright.l:year." ".g:DoxygenToolkit_companyName."."
  let l:license = substitute( g:DoxygenToolkit_licenseTag,"\<enter>","\<enter>".s:interCommentBlock,"g" )
  let l:license = substitute( l:license,"COMPANY",g:DoxygenToolkit_companyName,"g" )
  exec "normal O".s:startCommentBlock
  exec "normal o".s:interCommentTag.l:copyright."\<enter>".s:interCommentTag
  exec "normal o".s:interCommentTag.l:license
  exec "normal o".s:interCommentTag.g:DoxygenToolkit_fileTag.l:fileName
  exec "normal o".s:interCommentTag.g:DoxygenToolkit_briefTag_lic_pre
  mark d
  exec "normal o".s:interCommentTag.g:DoxygenToolkit_authorTag.g:DoxygenToolkit_authorName
  exec "normal o".s:interCommentTag.g:DoxygenToolkit_versionTag."1.0"
  let l:date = strftime("%Y-%m-%d")
  exec "normal o".s:interCommentTag.g:DoxygenToolkit_dateTag.l:date
  if( s:endCommentBlock != "" )
    exec "normal o".s:endCommentBlock
  endif
  exec "normal `d"

  call s:RestoreParameters()
  startinsert!
endfunction


修改DoxygenAuthorFunc(),把DoxygenToolkit_briefTag_pre替换为DoxygenToolkit_briefTag_lic_pre为了对齐。

然后在.vimrc增加如下代码块:



vi打开代码文件,即可通过快捷键进行代码注释了,公司名为xxxx,效果如下:

猜你在找的Bash相关文章