zoukankan      html  css  js  c++  java
  • 使用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,看到你刚刚建立的宏了吧? 赶紧分配一个你喜欢快捷键吧。

  • 相关阅读:
    C# 排序技术研究与对比
    基于.net的通用内存缓存模型组件
    Scala学习笔记:重要语法特性
    一个初学者的指南,使用D3做数据绑定
    CLR垃圾回收的设计
    CLR线程概览(下)
    CLR线程概览(一)
    使用sos查看.NET对象内存布局
    .NET对象的内存布局
    MYC编译器源码之代码生成
  • 原文地址:https://www.cnblogs.com/xwing/p/2028859.html
Copyright © 2011-2022 走看看