把这样的代码:
i_DataSource.SourceType = this.DBSourceType;
i_DataSource.StructureType = this.DBStructureType;
替换成:i_DataSource.StructureType = this.DBStructureType;
this.DBSourceType = i_DataSource.SourceType;
this.DBStructureType = i_DataSource.StructureType;
this.DBStructureType = i_DataSource.StructureType;
如果代码不是很多,手动完成这样的事件应该不难,对于大量的代码怎么办呢?
有两种方法可以完成这样的事情:第一种就是利用VS.Net的查找与替换,当然,直接查找是不行的,必须使用正则表达式来完成。
查找:^{\t+}{<.+}={.+};$
替换为:\1\3 = \2;
这样就可以快速的完成这个替换了。
第二个方法就是利用VS.Net的宏:然后利用Ctrl+Shift+P的快捷键来替换,速度会更快。当然,写宏也是可以用正则表达式的,这里给出一个没用正则表达式的宏:
Option Strict Off
Option Explicit Off
Imports EnvDTE
Imports System.Diagnostics
Public Module RecordingModule
Sub TemporaryMacro()
Dim m_TextSection As TextSelection = DTE.ActiveDocument.Selection
Dim m_indexOfEqual As Integer
Dim m_indexOfSemicolon As Integer
Dim m_PerFix As String
Dim m_Left As String
Dim m_Right As String
Dim m_Equal As String
Dim m_Semicolon As String
Dim m_Char As Char
Dim i_index As Integer
m_Equal = "="
m_Semicolon = ";"
m_TextSection.SelectLine()
m_indexOfEqual = m_TextSection.Text.IndexOf(m_Equal)
m_indexOfSemicolon = m_TextSection.Text.IndexOf(m_Semicolon)
If m_indexOfEqual > 0 And m_indexOfSemicolon > 0 Then
i_index = 0
m_Char = m_TextSection.Text.Chars(i_index)
'MsgBox(m_Char)
While m_Char.IsWhiteSpace(m_Char)
i_index = i_index + 1
m_Char = m_TextSection.Text.Chars(i_index)
'MsgBox(m_Char)
End While
m_PerFix = m_TextSection.Text.Substring(0, i_index)
m_Left = m_TextSection.Text.Substring(i_index, m_indexOfEqual - i_index).Trim()
'MsgBox(m_Left)
m_Right = m_TextSection.Text.Substring(m_indexOfEqual + 1, m_indexOfSemicolon - m_indexOfEqual - 1).Trim()
' MsgBox(m_Right)
m_TextSection.Text = m_PerFix + m_Right + " " + m_Equal + " " + m_Left + m_Semicolon + Chr(13) + Chr(10)
End If
End Sub
End Module
效率不是很好,改成正则表达式的可能要好一些。Option Explicit Off
Imports EnvDTE
Imports System.Diagnostics
Public Module RecordingModule
Sub TemporaryMacro()
Dim m_TextSection As TextSelection = DTE.ActiveDocument.Selection
Dim m_indexOfEqual As Integer
Dim m_indexOfSemicolon As Integer
Dim m_PerFix As String
Dim m_Left As String
Dim m_Right As String
Dim m_Equal As String
Dim m_Semicolon As String
Dim m_Char As Char
Dim i_index As Integer
m_Equal = "="
m_Semicolon = ";"
m_TextSection.SelectLine()
m_indexOfEqual = m_TextSection.Text.IndexOf(m_Equal)
m_indexOfSemicolon = m_TextSection.Text.IndexOf(m_Semicolon)
If m_indexOfEqual > 0 And m_indexOfSemicolon > 0 Then
i_index = 0
m_Char = m_TextSection.Text.Chars(i_index)
'MsgBox(m_Char)
While m_Char.IsWhiteSpace(m_Char)
i_index = i_index + 1
m_Char = m_TextSection.Text.Chars(i_index)
'MsgBox(m_Char)
End While
m_PerFix = m_TextSection.Text.Substring(0, i_index)
m_Left = m_TextSection.Text.Substring(i_index, m_indexOfEqual - i_index).Trim()
'MsgBox(m_Left)
m_Right = m_TextSection.Text.Substring(m_indexOfEqual + 1, m_indexOfSemicolon - m_indexOfEqual - 1).Trim()
' MsgBox(m_Right)
m_TextSection.Text = m_PerFix + m_Right + " " + m_Equal + " " + m_Left + m_Semicolon + Chr(13) + Chr(10)
End If
End Sub
End Module
利用VS.Net的查找和正则表达式,可以把宏简化:
Sub ExchangeEqual2()
'Dim m_result As vsFindResult
DTE.Find.FindReplace(vsFindAction.vsFindActionReplace, "^{\t+}{<.+}={.+};$", vsFindOptions.vsFindOptionsRegularExpression, "\1\3 = \2;", vsFindTarget.vsFindTargetCurrentDocument, , , vsFindResultsLocation.vsFindResultsNone)
'MsgBox(m_result)
End Sub
'Dim m_result As vsFindResult
DTE.Find.FindReplace(vsFindAction.vsFindActionReplace, "^{\t+}{<.+}={.+};$", vsFindOptions.vsFindOptionsRegularExpression, "\1\3 = \2;", vsFindTarget.vsFindTargetCurrentDocument, , , vsFindResultsLocation.vsFindResultsNone)
'MsgBox(m_result)
End Sub
VS.Net 2003中测试可用,2005应该也没问题,但没试过。