开发者

Need help enhancing a Visual Studio Macro

Here's my current Macro

Public Module CopyrightCode
    Sub AddCopyrightHeader()

        Dim doc As Document
        Dim docName As String
        Dim companyName As String = "Urban Now"
        Dim authorName As String = "Chase Florell"
        Dim authorEmail As String = "chase@infinitas.ws"
        Dim copyrightText As String = "'     All code is Copyright © " & vbCrLf & _
        "'      -   Urban Now (http://mysite.com)" & vbCrLf & _
        "'      -   Infinitas Advantage (http://infinitas.ws)" & vbCrLf & _
        "'     All Rights Reserved"

        ' Get the name of this object from the file name
        doc = DTE.ActiveDocument

        ' Get the name of开发者_运维知识库 the current document
        docName = doc.Name

        ' Set selection to top of document
        DTE.ActiveDocument.Selection.StartOfDocument()
        DTE.ActiveDocument.Selection.NewLine()

        Dim sb As New StringBuilder
        sb.Append("' --------------------------------")
        sb.Append(vbCrLf)
        sb.Append("' <copyright file='" & docName & "' company='" & companyName & "'>")
        sb.Append(vbCrLf)
        sb.Append(copyrightText)
        sb.Append(vbCrLf)
        sb.Append("' </copyright>")
        sb.Append(vbCrLf)
        sb.Append("' <author>" & authorName & "</author>")
        sb.Append(vbCrLf)
        sb.Append("' <email>" & authorEmail & "</email>")
        sb.Append(vbCrLf)
        sb.Append("' <lastedit>" & FormatDateTime(Date.Now, vbLongDate) & "</lastedit>")
        sb.Append(vbCrLf)
        sb.Append("' ---------------------------------")

        ' Write first line
        DTE.ActiveDocument.Selection.LineUp()
        DTE.ActiveDocument.Selection.Text = sb.ToString

    End Sub
End Module

What I need to do is first do a file search for the line ' <lastedit>Monday, July 05, 2010</lastedit> (obviously as a REGEX because the date will always be different)

and if it exists, replace the date with today's date, and if it doesn't run the full insert.

Then what I want to hook up is every time I close a file, the Macro runs to update the edit date.


I'm not sure what you're doing, but if that is XML (as it looks like), you should be using XQuery or whatever to locate/update the lastedit node, since that'll handle the assorted complexities of comments and nesting and so on.

If you're confident of what the input text will be and are certain there's no nasties in there, you can match that specific date format quick and dirty:

<lastedit>\w{6,9}, \w{3,9} \d\d, \d{4}</lastedit>

Or, even quicker and dirtier:

<lastedit>[^<]+<lastedit>


It depends what your needs are, how confident you are of what the file contents will be, and so on.


Oh. So I was curious and went and looked up how Visual Studio actually does it's regex stuff, and well... whoever did the VS regex needs to be whacked round the head.

Translate the above standard regex into VS regex, you get these:

\<lastedit\>:i+, :i+ :d:d, :d:d:d:d\</lastedit\>

and

\<lastedit\>[^<]+</lastedit\>

Maybe. It's hard to read the documentation because Microsoft don't appear to want to write websites that work in modern browsers.

Of course, that assumes macros use this insane regex instead of the normal .NET regex - if it's the latter than the top stuff will be fine and you can ignore this craziness. :)


To implement, try something like this:

Dim reLastEdit As Regex = New Regex("<lastedit>[^<]+<lastedit>")

Dim matches AS MatchCollection = reLastEdit.Matches(Input)

If matches.Count > 0
Then
    ' Change Header
    Dim NewLastEdit As String = "<lastedit>" & FormatDateTime(Date.Now, vbLongDate) & "</lastedit>"
    reLastEdit.Replace(Input,NewLastEdit)
Else
    ' Add Header
EndIf

Or similar. Info here: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex_methods.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜