macro
提起VS2003的宏,可能有的人常用,有的人从来不用。
可是,对于我这种“能让机器干,就自己绝不动手”的人来说,肯定还是会用到的。
常常为了写宏没有写代码而挨批,
自己写的宏一般都比较专用,专门用在项目的某个地方,一旦有什么更改,可以立刻用宏来重新构造那些大量复杂重复的代码。但是,也有一些非常好的宏,比较通用,在这里大家一起讨论一番。例如这个用来将字段转化为属性的宏就很好用,并且,我个人觉得要比vs2005的[重构]要好,要实用,而且这个宏还可以一次转换多行字段,是2005没法达到的。
Imports EnvDTE
Imports System.Diagnostics
Imports System.Text.RegularExpressions
Public Module forCSharp
Sub ReCreate()
Dim line, originalCode As String
If DTE.ActiveDocument Is Nothing Then Return
Dim txt As TextSelection
txt = CType(DTE.ActiveDocument.Selection, TextSelection)
originalCode = txt.Text
Dim variableName As String
Dim publicName As String
Dim privateName As String
Dim dataType As String
Dim propertyProcedures As New System.Text.StringBuilder
Dim lines() As String
lines = Split(originalCode, vbLf)
Dim r As Regex
r = New Regex( _
"/s*(?<typename>/w*)" & _
"/s*(?<varname>/w*)/s*/;", _
RegexOptions.IgnoreCase Or _
RegexOptions.ExplicitCapture)
For Each line In lines
line = line.Trim
If Not line = String.Empty Then
Dim mtch As Match
mtch = r.Match(line)
If mtch.Success Then
variableName = _
mtch.Groups("varname").Value.Trim
dataType = _
mtch.Groups("typename").Value.Trim
variableName = IIf(variableName.StartsWith("_"), variableName.Substring(1), variableName)
variableName = IIf(variableName.StartsWith("m_"), variableName.Substring(2), variableName)
publicName = variableName.Substring(0, 1).ToUpper() & variableName.Substring(1)
privateName = "_" & variableName.Substring(0, 1).ToLower() & variableName.Substring(1)
propertyProcedures.AppendFormat(vbCrLf _
& " private {1} {2};" & vbCrLf _
& " public {1} {0}" & vbCrLf _
& " {{" & vbCrLf _
& " get" & vbCrLf _
& " {{" & vbCrLf _
& " return {2};" & vbCrLf _
& " }}" & vbCrLf _
& "" & vbCrLf _
& " set" & vbCrLf _
& " {{" & vbCrLf _
& " {2} = value;" & vbCrLf _
& " }}" & vbCrLf _
& " }}" & vbCrLf, _
publicName, _
dataType, privateName)
End If
End If
Next
DTE.UndoContext.Open("重构属性")
txt = CType(DTE.ActiveDocument.Selection, TextSelection)
txt.Delete()
txt.Insert(propertyProcedures.ToString, _
vsInsertFlags.vsInsertFlagsContainNewText Or vsInsertFlags.vsInsertFlagsInsertAtEnd)
'txt.Text = propertyProcedures.ToString()
txt.SmartFormat()
DTE.UndoContext.Close()
End Sub
End Module
macro
提起VS2003的宏,可能有的人常用,有的人从来不用。
可是,对于我这种“能让机器干,就自己绝不动手”的人来说,肯定还是会用到的。
常常为了写宏没有写代码而挨批,
自己写的宏一般都比较专用,专门用在项目的某个地方,一旦有什么更改,可以立刻用宏来重新构造那些大量复杂重复的代码。但是,也有一些非常好的宏,比较通用,在这里大家一起讨论一番。例如这个用来将字段转化为属性的宏就很好用,并且,我个人觉得要比vs2005的[重构]要好,要实用,而且这个宏还可以一次转换多行字段,是2005没法达到的。
Imports EnvDTE
Imports System.Diagnostics
Imports System.Text.RegularExpressions
Public Module forCSharp
Sub ReCreate()
Dim line, originalCode As String
If DTE.ActiveDocument Is Nothing Then Return
Dim txt As TextSelection
txt = CType(DTE.ActiveDocument.Selection, TextSelection)
originalCode = txt.Text
Dim variableName As String
Dim publicName As String
Dim privateName As String
Dim dataType As String
Dim propertyProcedures As New System.Text.StringBuilder
Dim lines() As String
lines = Split(originalCode, vbLf)
Dim r As Regex
r = New Regex( _
"/s*(?<typename>/w*)" & _
"/s*(?<varname>/w*)/s*/;", _
RegexOptions.IgnoreCase Or _
RegexOptions.ExplicitCapture)
For Each line In lines
line = line.Trim
If Not line = String.Empty Then
Dim mtch As Match
mtch = r.Match(line)
If mtch.Success Then
variableName = _
mtch.Groups("varname").Value.Trim
dataType = _
mtch.Groups("typename").Value.Trim
variableName = IIf(variableName.StartsWith("_"), variableName.Substring(1), variableName)
variableName = IIf(variableName.StartsWith("m_"), variableName.Substring(2), variableName)
publicName = variableName.Substring(0, 1).ToUpper() & variableName.Substring(1)
privateName = "_" & variableName.Substring(0, 1).ToLower() & variableName.Substring(1)
propertyProcedures.AppendFormat(vbCrLf _
& " private {1} {2};" & vbCrLf _
& " public {1} {0}" & vbCrLf _
& " {{" & vbCrLf _
& " get" & vbCrLf _
& " {{" & vbCrLf _
& " return {2};" & vbCrLf _
& " }}" & vbCrLf _
& "" & vbCrLf _
& " set" & vbCrLf _
& " {{" & vbCrLf _
& " {2} = value;" & vbCrLf _
& " }}" & vbCrLf _
& " }}" & vbCrLf, _
publicName, _
dataType, privateName)
End If
End If
Next
DTE.UndoContext.Open("重构属性")
txt = CType(DTE.ActiveDocument.Selection, TextSelection)
txt.Delete()
txt.Insert(propertyProcedures.ToString, _
vsInsertFlags.vsInsertFlagsContainNewText Or vsInsertFlags.vsInsertFlagsInsertAtEnd)
'txt.Text = propertyProcedures.ToString()
txt.SmartFormat()
DTE.UndoContext.Close()
End Sub
End Module