• 使用Visual Studio宏给代码文件自动添加文件头


    一般来说代码文件都要有个文档头,带有版权声明和帮助信息等。我以前都是用CtrlC,CtrlV来做,后来研究了一下网上的资料,发现用宏来完成这个任务比较方便。

    在工具菜单中选择宏->宏IDE,在宏编辑器中添加新项,选择模块,嗯,模块名字就叫做"AddDocumentHeader"吧。

    编辑代码内容如下:

    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports EnvDTE90
    Imports EnvDTE90a
    Imports EnvDTE100
    Imports System.Diagnostics

    Public Module AddDocumentHeader
        
    Sub AddDocumentHeader()
            
    Dim document As Document
            document 
    = DTE.ActiveDocument
            document.Selection.StartOfDocument()
            document.Selection.GotoLine(
    2True)
            
    Dim content As String = document.Selection.Text
            
    '通过比较代码第二行版权信息字符串判断是否需要添加文件头.
            Dim copyrightInformation As String = "Copyright (c) xwingyz(at)gmail.com. All right reserved."
            
    Dim ms = " * " + copyrightInformation
            
    Dim Found = String.Compare(content, ms)
            
    If (Found <> 0Then
                document.Selection.StartOfDocument()
                document.Selection.LineUp()
                document.Selection.Text 
    = "/* -----------------------------------------------------------------------------"
                document.Selection.NewLine()
                document.Selection.Text 
    = copyrightInformation
                document.Selection.NewLine()
                document.Selection.NewLine()
                document.Selection.NewLine()
                document.Selection.Text 
    = "$LastChangedBy$"
                document.Selection.NewLine()
                document.Selection.Text 
    = "$LastChangedDate$"
                document.Selection.NewLine()
                document.Selection.Text 
    = "$LastChangedRevision$"
                document.Selection.NewLine()
                document.Selection.Text 
    = "-----------------------------------------------------------------------------"
                document.Selection.NewLine()
                document.Selection.Text 
    = "/"
                document.Selection.NewLine()
            
    End If
        
    End Sub
    End Module

     保存,退出。

     打开一个代码文件,选择工具菜单中宏->Macro资源管理器,选择刚刚建立的模块AddDocumentHeader右键菜单上选择运行,看看代码中是否自动添加了文件头?再执行一次看看是不是没有重复添加?不出意外的话一切OK。

    下一步是要给这个宏分配一个快捷键,这样就更方便了。

    找到工具菜单中选项->键盘选项, 在过滤框中输入AddDocumentHeader,看到你刚刚建立的宏了吧? 赶紧分配一个你喜欢快捷键吧。

  • 相关阅读:
    java学习之旅(一):BOS项目使用的技术以及开发环境
    spring手动回滚
    tomcat下配置多端口,多项目
    centos7安装Mysql5.6
    windows phone7 下 Silverlight 异步读取网络图片
    Sencha Touch 本地化存储配置
    LCD1602显示接收的串口通讯字串
    QML 怎么在gridview中用Index定位? 怎么在代理中设置背景?
    89C52定时/计数器
    QML JSON 展示
  • 原文地址:https://www.cnblogs.com/xwing/p/2028859.html
Copyright © 2020-2023  润新知