//*Public Module Utilities
//Declare these two variables under module
Dim repPattern As String
Dim repPatternReadOnly As String
*//Sub ConvertVariables()' Determine current language by looking at the extension of the current document.Dim doc As Document = DTE.ActiveDocumentIf doc Is Nothing Then Exit SubDim docName As String = doc.Name.ToLower()' Read all the text lines touched by the selection.Dim sel As TextSelection = CType(DTE.ActiveDocument.Selection, TextSelection)Dim ed1 As EditPoint = sel.AnchorPoint.CreateEditPoint()ed1.EndOfLine() : ed1.StartOfLine() : ed1.StartOfLine()Dim ed2 As EditPoint = sel.BottomPoint.CreateEditPoint()ed2.EndOfLine()Dim text As String = ed1.GetText(ed2)' The find and replacement pattern depend on the current language.Dim findPattern As StringIf docName.EndsWith(".vb") ThenfindPattern = "(?
[\t ]+)Public\s+(?Shared\s+)?(?ReadOnly\s+)?" _& "(?\w+)\s+As\s+(?\S+)(?.*?)\n"' {0}=property name, {1}=property type, {2}=static keyword, {3} initvalue, ' {4}=CR-LF, {5}=Tab, {6}=indentrepPattern = "{6}Private {2}m_{0} As {1}{3}{4}" _& "{6}Public {2}Property {0}() As {1}{4}" _& "{6}{5}Get{4}" _& "{6}{5}{5}Return m_{0}{4}" _& "{6}{5}End Get{4}" _& "{6}{5}Set(ByVal Value As {1}){4}" _& "{6}{5}{5}m_{0} = Value{4}" _& "{6}{5}End Set{4}" _& "{6}End Property{4}{4}"repPatternReadOnly = "{6}Private {2}ReadOnly m_{0} As {1}{3}{4}" _& "{6}Public ReadOnly {2}Property {0}() As {1}{4}" _& "{6}{5}Get{4}" _& "{6}{5}{5}Return m_{0}{4}" _& "{6}{5}End Get{4}" _& "{6}End Property{4}{4}"ElseIf docName.EndsWith(".cs") Then' Notice the (?.*;) element is needed to ensure that public fields are matched, ' but public properties aren'tfindPattern = "(?[\t ]+)public\s+(?static\s+)?(?readonly\s+)?" _& "(?\S+)\s+(?\w+)(?=.*;)(?.*?)\n"' {0}=property name, {1}=property type, {2}=static keyword, {3} initvalue, ' {4}=CR-LF, {5}=Tab, {6}=indentrepPattern = "{6}private {2}{1} _{0}{3}{4}" _& "{6}public {2}{1} {0}{4}" _& "{6}{{{4}" _& "{6}{5}get {{ return _{0}; }}{4}" _& "{6}{5}set {{ _{0} = value; }}{4}" _& "{6}}}{4}{4}"repPatternReadOnly = "{6}private {2}readonly {1} _{0}{3}{4}" _& "{6}public {2}{1} {0}{4}" _& "{6}{{{4}" _& "{6}{5}get {{ return _{0}; }}{4}" _& "{6}}}{4}{4}"End If' Replace the text. Add a trailing CR-LF but remove it later.Dim replaceText As String = Regex.Replace(text + ControlChars.CrLf, findPattern, _AddressOf ReplaceWithProperty)ed1.ReplaceText(ed2, replaceText.Substring(0, replaceText.Length - 2), 0)End Sub' Private callback function for the Replace methodPrivate Function ReplaceWithProperty(ByVal m As Match) As StringDim pattern As String = repPatternIf m.Groups("readonly").Length > 0 Then pattern = repPatternReadOnlyReturn String.Format(pattern, m.Groups("name").Value, m.Groups("type").Value, _m.Groups("static").Value, m.Groups("init").Value, ControlChars.CrLf, _ControlChars.Tab, m.Groups("indent").Value)
End Function
No comments:
Post a Comment