Introduction
Ever designed a .aspx or .ascx file and ended up with lots of extra @Register tags, tags like this:
<%@ Register TagPrefix="uc1" TagName="MyControl" ... %>
<%@ Register TagPrefix="uc2" Namespace="MySite.Controls" ... %>
This macro generates a list of unused tags in the output window.
Sub CheckRegisterTags()
Dim str As String = ""
Dim r As New Regex("\<%@\ Register\ TagPrefix\=\""(?<tag>\w*)\""\" & _
"(TagName\=\""(?<name>\w*)\"")?")
Dim selection As TextSelection = DTE.ActiveDocument.Selection()
selection.SelectAll()
Dim theText As String = selection.Text
Dim matches As MatchCollection = r.Matches(theText)
Dim m As Match
For Each m In matches
Dim g1 As Group = m.Groups("tag")
Dim g2 As Group = m.Groups("name")
Dim prefix As String = g1.Value
Dim suffix As String = ""
If Not (g2 Is Nothing) Then
suffix = g2.Value
End If
Dim r2 As New Regex("\<" & prefix & ":" & suffix, _
RegexOptions.IgnoreCase)
If Not r2.IsMatch(theText) Then
str += m.Value
str += vbCrLf
End If
Next
selection.GotoLine(1)
If str = "" Then
str = "No excess registertags found"
End If
Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
Dim OWp As OutputWindowPane = win.Object.OutputWindowPanes.Item(1)
OWp.Clear()
OWp.Activate()
OWp.OutputString(str)
OWp.TextDocument.Selection.GotoLine(OWp.TextDocument.EndPoint().Line())
DTE.ExecuteCommand("View.Output")
End Sub
The New Regex("\<%@\ Register\ TagPrefix\=\""(?<tag>\w*)\""\ (TagName\=\""(?<name>\w*)\"")?") regular expression has two named groups: <tag> and <name>. The tag is always filled with the TagPrefix value, and when available the name is filled with the TagName value.
For every Match in the Matches collection of this Regex I do a new regular expression search for "\<" & prefix & ":" & suffix (where suffix sometimes is an empty string). If I get no match I concatenate the Match.Value to the str string.
When the str string is empty there are no excess register tags.
Finally, we get the output window and write the str string to this window.
Hope this helps,
Rooc