利用宏自动在类文件中添加注释信息
2010-03-26 16:54
471 查看
有些时候我们为了版权或是别的原因需要在文件的头部添加一些注释,比如我新加入的这家公司就要求在文件的头部添加文件名、路径名、项目名以及公司版权声明等信息。当遇到这种情况的时候我一般想到的是查找当前手头可用的工具,看看有没有可以直接自动完成的,如果没有就自己编写一个工具来完成。
针对上面的需求我查找了一下安装的Resharper工具,发现Resharper只能添加固定类型的头注释,无法动态的设置文件名、路径名以及项目名,这显然不符合我的要求,所以我就想到了用宏来完成工作,下面是注释文件的样式和宏代码,希望对您有帮助。
再次更新
针对上面的需求我查找了一下安装的Resharper工具,发现Resharper只能添加固定类型的头注释,无法动态的设置文件名、路径名以及项目名,这显然不符合我的要求,所以我就想到了用宏来完成工作,下面是注释文件的样式和宏代码,希望对您有帮助。
再次更新
Option Explicit Off Option Strict Off Imports System Imports EnvDTE Imports EnvDTE80 Imports EnvDTE90 Imports System.Diagnostics Imports VSLangProj Imports System.Windows.Forms Imports System.Runtime.InteropServices Imports System.Collections.Generic Imports System.Text Imports System.IO Public Module AddFileHeader Dim document As Document = DTE.ActiveDocument Dim selection As TextSelection = DTE.ActiveDocument.Selection Dim headerText As String Dim filename As String = Document.Name Dim pathname As String = Document.Path.Substring(Document.Path.LastIndexOf("src\") + 4).Trim("\") '根据需要自动截取,这里是从项目根目录开始 Dim projectname As String = document.ProjectItem.ContainingProject.Name Public Sub AddFileHeader() Try document = DTE.ActiveDocument selection = DTE.ActiveDocument.Selection headerText = "" filename = document.Name pathname = document.Path.Substring(document.Path.LastIndexOf("src\") + 4).Trim("\") projectname = document.ProjectItem.ContainingProject.Name deleteExistComment() insertComment() Finally Application.DoEvents() End Try End Sub Private Sub deleteExistComment() selection.StartOfDocument() DTE.ExecuteCommand("Edit.Find") DTE.Windows.Item(filename).Activate() DTE.Find.FindWhat = "using System" DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument DTE.Find.MatchCase = False DTE.Find.MatchWholeWord = False DTE.Find.Backwards = False DTE.Find.MatchInHiddenText = True DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral DTE.Find.Action = vsFindAction.vsFindActionFind If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then Return End If DTE.Windows.Item(filename).Activate() DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn) DTE.ActiveDocument.Selection.LineUp(True, 100) DTE.ActiveDocument.Selection.Delete() DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close() End Sub Private Sub insertComment() selection.Insert("/***************************************************************** ") selection.NewLine() selection.Insert("* Copyright (C) 在这里填写你的信息") selection.NewLine() selection.Insert("* 文件名称: ") selection.Insert(filename) selection.NewLine() selection.Insert("* 功能描述: ") selection.NewLine() selection.Insert("* 作 者: ZhangRongHua ") selection.NewLine() selection.Insert("* 创建时间: ") selection.Insert(Date.Today.ToString("yyyy-MM-dd")) selection.NewLine() selection.Insert("*") selection.NewLine() selection.Insert("* 修改记录:") selection.NewLine() selection.Insert("* 修改人:") selection.NewLine() selection.Insert("*********************************************************************/") selection.NewLine() End Sub End Module
相关文章推荐
- idea创建新的类(文件)时,自动添加作者创建时间(文件注释)等信息的设置方法
- VS中为程序文件自动添加版权信息注释的方法
- myeclipse 自动添加头时间,作者,文件注释等信息
- 自动添加VS 2008 代码文件版权信息 注释,用小程序实现更改
- Eclipse新建类的时候如何自动添加注释(作者,时间的信息等)
- Eclipse新建类的时候如何自动添加注释(作者,时间的信息
- VS中为类,函数代码自动添加版权注释信息
- 一个很有用的vimrc文件(添加抬头,产生cpp初始程序,添加注释,注释一行,自动K&R格式化,taglist)
- 让你的Visual Studio 2010自动添加头部注释信息
- vs2012及以上版本中写c++时自动给代码添加创建信息注释的问题
- VC助手自动添加文件版本信息
- VS中为类,函数代码自动添加版权注释信息
- Pycharm在创建py文件时,如何自动添加文件头注释?
- Eclipse新建类的时候如何自动添加注释(作者,时间,版本等信息)
- C#实现为类和函数代码自动添加版权注释信息的方法
- Eclipse和Android Studio新建文件时自动添加注释
- 利用VBA 宏实现vc6.0的自动添加注释和自动取消注释
- myeclipse 自动添加头时间,作者,文件注释
- Eclipse新建类的时候如何自动添加注释(作者,时间的信息)
- 第一个 想创建文件时 文件的头部注释会自动生成我自己想要的信息 怎么配置呢