zoukankan      html  css  js  c++  java
  • 利用宏自动在类文件中添加注释信息

    有些时候我们为了版权或是别的原因需要在文件的头部添加一些注释,比如我新加入的这家公司就要求在文件的头部添加文件名、路径名、项目名以及公司版权声明等信息。当遇到这种情况的时候我一般想到的是查找当前手头可用的工具,看看有没有可以直接自动完成的,如果没有就自己编写一个工具来完成。

    针对上面的需求我查找了一下安装的Resharper工具,发现Resharper只能添加固定类型的头注释,无法动态的设置文件名、路径名以及项目名,这显然不符合我的要求,所以我就想到了用宏来完成工作,下面是注释文件的样式和宏代码,希望对您有帮助。

    宏代码
    1 OptionExplicitOff
    2  OptionStrictOff
    3
    4  Imports System
    5  Imports EnvDTE
    6  Imports EnvDTE80
    7  Imports EnvDTE90
    8  Imports System.Diagnostics
    9  Imports VSLangProj
    10  Imports System.Windows.Forms
    11  Imports System.Runtime.InteropServices
    12  Imports System.Collections.Generic
    13  Imports System.Text
    14  Imports System.IO
    15
    16  PublicModule AddFileHeader
    17 PublicSub AddFileHeader()
    18 Try
    19 Dim document As Document = DTE.ActiveDocument
    20 Dim selection As TextSelection = DTE.ActiveDocument.Selection
    21 Dim headerText AsString
    22 Dim filename AsString= document.Name
    23 Dim pathname AsString= document.Path.Substring(document.Path.LastIndexOf("src\") +4) '根据需要自动截取,这里是从项目根目录开始
    24  Dim projectname AsString= document.ProjectItem.ContainingProject.Name
    25
    26 selection.StartOfDocument()
    27 selection.Insert("// ---------------------------------------------------------------------")
    28 selection.NewLine()
    29
    30
    31 selection.Insert("// - File: ")
    32 selection.Insert(filename)
    33 selection.NewLine()
    34
    35 selection.Insert("// - Directory: ")
    36 selection.Insert(pathname)
    37 selection.NewLine()
    38
    39 selection.Insert("// - Project: ")
    40 selection.Insert(projectname)
    41 selection.NewLine()
    42
    43 selection.Insert("// ")
    44 selection.NewLine()
    45 selection.Insert("// - (c) 2002-2010 XXXXXX Inc. All rights reserved.")
    46 selection.NewLine()
    47 selection.Insert("// ---------------------------------------------------------------------")
    48 selection.NewLine()
    49 Finally
    50 Application.DoEvents()
    51 EndTry
    52 End Sub
    53
    54  End Module
    55
    56  
    自动生成的文本示例
    // ---------------------------------------------------------------------
    // - File: Test.cs
    // - Directory: TestProject\App_Class\
    // - Project: TestProject
    //
    // - (c) 2002-2010 XXXXXX. All rights reserved.
    // ---------------------------------------------------------------------
    // ---------------------------------------------------------------------

    下面是修改的版本,重构了一下代码并且加入了删除现在注释的功能 

    加入了删除现有注释的版本
    1 OptionExplicitOff
    2  OptionStrictOff
    3
    4  Imports System
    5  Imports EnvDTE
    6  Imports EnvDTE80
    7  Imports EnvDTE90
    8 Imports System.Diagnostics
    9 Imports VSLangProj
    10 Imports System.Windows.Forms
    11 Imports System.Runtime.InteropServices
    12 Imports System.Collections.Generic
    13 Imports System.Text
    14 Imports System.IO
    15
    16 PublicModule AddFileHeader
    17 Dim document As Document = DTE.ActiveDocument
    18 Dim selection As TextSelection = DTE.ActiveDocument.Selection
    19 Dim headerText AsString
    20 Dim filename AsString= Document.Name
    21 Dim pathname AsString= Document.Path.Substring(Document.Path.LastIndexOf("src\") +4).Trim("\") '根据需要自动截取,这里是从项目根目录开始
    22 Dim projectname AsString= document.ProjectItem.ContainingProject.Name
    23
    24 PublicSub AddFileHeader()
    25 Try
    26 deleteExistComment()
    27 insertComment()
    28 Finally
    29 Application.DoEvents()
    30 EndTry
    31 End Sub
    32
    33 PrivateSub deleteExistComment()
    34 selection.StartOfDocument()
    35 DTE.ExecuteCommand("Edit.Find")
    36 DTE.Windows.Item(filename).Activate()
    37 DTE.Find.FindWhat ="using system"
    38 DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
    39 DTE.Find.MatchCase =False
    40 DTE.Find.MatchWholeWord =False
    41 DTE.Find.Backwards =False
    42 DTE.Find.MatchInHiddenText =True
    43 DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
    44 DTE.Find.Action = vsFindAction.vsFindActionFind
    45 If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
    46 Return
    47 EndIf
    48 DTE.Windows.Item(filename).Activate()
    49 DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
    50 DTE.ActiveDocument.Selection.LineUp(True, 100)
    51 DTE.ActiveDocument.Selection.Delete()
    52 DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close()
    53 End Sub
    54
    55 PrivateSub insertComment()
    56 selection.Insert("// ---------------------------------------------------------------------")
    57 selection.NewLine()
    58
    59
    60 selection.Insert("// - File: ")
    61 selection.Insert(filename)
    62 selection.NewLine()
    63
    64 selection.Insert("// - Directory: ")
    65 selection.Insert(pathname)
    66 selection.NewLine()
    67
    68 selection.Insert("// - Project: ")
    69 selection.Insert(projectname)
    70 selection.NewLine()
    71
    72 selection.Insert("// ")
    73 selection.NewLine()
    74 selection.Insert("// - (c) 2002-2010 RelayHealth Inc. All rights reserved.")
    75 selection.NewLine()
    76 selection.Insert("// ---------------------------------------------------------------------")
    77 selection.NewLine()
    78 End Sub
    79
    80 End Module
    81
    82

    再次更新,修复了有时会报错的情况。

    再次更新
    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



  • 相关阅读:
    MySQL 数据库 查询语句的基本操作,单表查询,多表查询
    MySQL数据库 外键,级联, 修改表的操作
    MySQL数据库 存储引擎,创建表完整的语法,字段类型,约束条件
    MySQL数据库 介绍,安装,基本操作
    python GIL全局解释器锁,多线程多进程效率比较,进程池,协程,TCP服务端实现协程
    子进程回收资源两种方式,僵尸进程与孤儿进程,守护进程,进程间数据隔离,进程互斥锁,队列,IPC机制,线程,守护线程,线程local作用,线程池,回调函数add_done_callback,TCP服务端实现并发
    并发编程 操作系统发展史,多道技术,进程,同步与异步,阻塞与非阻塞,进程的三种状态,创建进程的两种方式
    网络编程 UDP协议 TCP局域网客户端与服务端上传下载电影示例
    网络编程 TCP协议:三次握手,四次挥手,反馈机制 socket套接字通信 粘包问题与解决方法
    k8s 常用命令
  • 原文地址:https://www.cnblogs.com/zhangronghua/p/AddHeaderCommentMacro.html
Copyright © 2011-2022 走看看