Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' Macro for localizing the columns of a grid.
- ' Input Selection : The "<COLUMNS>....</COLUMNS>" part of the grid.
- ' Output - There are 2 sections in the output. The first section should be
- ' used to replace the part selected in the input. The second section
- ' is an XML to be copied to the local resource file.
- Sub GenerateGridColumnHeadingResource()
- GenerateGridColumnHeadingResource_Helper(String.Empty)
- End Sub
- Sub GenerateGridColumnHeadingResourceWithPrefix()
- Dim resourcePrefix As String = InputBox("Enter Resource Prefix")
- GenerateGridColumnHeadingResource_Helper(resourcePrefix)
- End Sub
- Sub GenerateGridColumnHeadingResource_Helper(ByVal resourcePrefix As String)
- DTE.UndoContext.Open("MakeGridColumnHeadingResource")
- Dim selectedText As TextSelection
- selectedText = DTE.ActiveDocument.Selection
- Dim line, originalCode As String
- originalCode = selectedText.Text
- Dim lines() As String
- lines = Split(originalCode, vbLf)
- Dim heading As String
- Dim regResourceName As New System.Text.RegularExpressions.Regex("(\s|(<BR>)|('s)|/|&)*", Text.RegularExpressions.RegexOptions.Compiled)
- Dim regHeading As New System.Text.RegularExpressions.Regex("(?<pre>headertext\s*=\s*"")(?<heading>[^""]*)""", Text.RegularExpressions.RegexOptions.Compiled)
- Dim regValueAmp As New System.Text.RegularExpressions.Regex("&", Text.RegularExpressions.RegexOptions.Compiled)
- Dim regValueLT As New System.Text.RegularExpressions.Regex("<", Text.RegularExpressions.RegexOptions.Compiled)
- Dim regValueGT As New System.Text.RegularExpressions.Regex(">", Text.RegularExpressions.RegexOptions.Compiled)
- Dim xmlOutFormat As String = "<data name=""{0}"" xml:space=""preserve"">" _
- & vbCrLf & vbTab & "<value>{1}</value>" _
- & vbCrLf & "</data>"
- Dim resAssignFormat As String = "<%$ Resources:{0}{1}Columns_{2} %>"
- Dim resNameFormat As String = "{0}{1}Columns_{2}"
- Dim markupOutput As String = ""
- Dim resourceOutput As String = ""
- For Each line In lines
- If line.Length > 0 Then
- Dim mtch As System.Text.RegularExpressions.Match
- mtch = regHeading.Match(line)
- If mtch.Success Then
- heading = mtch.Groups("heading").Value.Trim
- Dim resName As String = regResourceName.Replace(heading, "")
- Dim fullResName As String = String.Format(resNameFormat, resourcePrefix, IIf(resourcePrefix.Length > 0, "_", ""), resName)
- Dim assignResource As String = String.Format(resAssignFormat, resourcePrefix, IIf(resourcePrefix.Length > 0, "_", ""), resName)
- Dim resValue As String = regValueAmp.Replace(heading, "&")
- resValue = regValueLT.Replace(resValue, "<")
- resValue = regValueGT.Replace(resValue, ">")
- line = regHeading.Replace(line, "${pre}" & assignResource & """")
- resourceOutput = resourceOutput & vbCrLf & String.Format(xmlOutFormat, fullResName, resValue)
- End If
- markupOutput = markupOutput & line
- End If
- Next
- ReplaceSelectedtextWith(selectedText, markupOutput)
- AddToResourceFile(resourceOutput)
- DTE.UndoContext.Close()
- End Sub
- Private Sub ReplaceSelectedtextWith(ByVal selectedText As TextSelection, ByVal replacementText As String)
- Dim startPoint As EnvDTE.EditPoint
- startPoint = selectedText.TopPoint.CreateEditPoint()
- selectedText.Delete()
- startPoint.Insert(replacementText)
- End Sub
- Private Sub AddToResourceFile(ByVal resourceData As String)
- Dim targetResxDocument As EnvDTE.Document = GetTargetResourceDocument()
- Dim textEditor As TextDocument = targetResxDocument.Object("TextDocument")
- Dim editPoint As EnvDTE.EditPoint = textEditor.CreateEditPoint(textEditor.EndPoint) 'should skip to the end of the document at the closing '</root>' element
- editPoint.LineUp(1) 'jump up one line from the </root>
- editPoint.EndOfLine() 'go the end of the line
- editPoint.Insert(Environment.NewLine)
- editPoint.Insert(resourceData)
- targetResxDocument.Save()
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement