一个很棒的宏,可以自动生成 C# 代码中的区域






4.86/5 (23投票s)
在我工作的 ComponentScience,我们非常喜欢使用区域来将我们的代码在逻辑上分成有意义的块。 在手动操作几年后,我决定编写一个小宏来让我的生活更轻松。
目录
引言
在 ComponentScience,我们非常喜欢使用区域来在逻辑上将我们的代码分成有意义的块。 在手动操作几年后,我决定编写一个小宏来让我的生活更轻松。 经过一些研究,我找到了一些关于如何操作 Visual Studio 代码编辑器的示例。 结果就是我用来区域化我的代码的以下宏。
我还包括了如何将其安装到 Visual Studio 并将其分配给热键的说明。 我使用 Alt+R 是因为它未在 Visual Studio 文本编辑器中使用,而且我觉得这样有意义。
欢迎您随意使用它,把它给您的朋友,甚至声称是您自己编写的,如果您愿意的话。
注意:我只在 C# 文件中使用此宏。 如果您想在 VB 中使用它,则需要取消注释 VB 部分并注释掉 C# 部分。 如果有人有关于如何修改脚本以检测正在使用的语言的提示,请告诉我,我会相应地修改脚本。 我相信有一个简单的方法,我只是没有研究它,因为我不在乎。 VB 部分在 VB 代码中确实有效。 我测试了那么多。
更新:在 CodeProject 上发布这篇文章后,我收到了一些读者的反馈,内容涉及如何测试正在使用的语言、改进填充以及处理制表符和空格。 我已经相应地更新了宏。
更多更新!:CodeProject 上的用户在调整宏方面做得非常出色! 我们现在有一个非常好的区域宏。 我对从 CodeProject 社区收到的反馈非常满意。 特别感谢(排名不分先后)RussKie、simonech、Hrusikesh、ERobishaw、isler-j 和 SvenRieke。 这些人体现了合作开发的精神。
宏代码
Sub MakeRegion()
Regions.MakeRegion()
End Sub
Public Class Regions
' MakeRegion inserts #region and #endregion tags
' around selected text in the VS editor.
Shared Sub MakeRegion()
Dim rName As String = ""
Dim pad As String = ""
Dim junk As String
Dim count, i As Integer
Dim startpoint, endpoint, tmppoint As EditPoint
With DTE.ActiveDocument.Selection
startpoint = .TopPoint.CreateEditPoint()
endpoint = .BottomPoint.CreateEditPoint
End With
If startpoint.EqualTo(endpoint) Then
Exit Sub
End If
'ELR: ADDED THIS, to move the startpoint to the start of the line
'so that the Pad function works correctly
If Not startpoint.AtStartOfLine Then
startpoint.StartOfLine()
End If
'IV 2004-12-13: rName = InputBox("Region Name:")
rName = InputBox("Region Name:", "Pick a name", _
GetDesc(DTE.ActiveDocument.Selection.TopPoint.CreateEditPoint()))
DTE.UndoContext.Open("Insert A Region")
Try
junk = startpoint.GetText(startpoint.LineLength)
pad = String.Empty
For count = 0 To junk.Length - 1
If junk.Substring(count, 1).Equals(" ") Or _
junk.Substring(count, 1).Equals(vbTab) Then
pad += junk.Substring(count, 1)
Else
Exit For
End If
Next
'ELR: ADDED Test for Languages
If DTE.ActiveDocument.Language = "CSharp" Then
' C Sharp Code
startpoint.Insert(String.Format("{0}#region {1}{2}", _
pad, rName, vbCrLf))
If endpoint.LineLength = 0 Then
endpoint.Insert(String.Format("{0}#endregion // {1}{2}", _
pad, rName, vbCrLf))
Else
endpoint.Insert(String.Format("{0}#endregion // {1}{2}", _
vbCrLf & pad, rName, vbCrLf))
End If
Else
' VB Code
startpoint.Insert(String.Format("{0}#Region ""{1}""{2}", _
pad, rName, vbCrLf))
If endpoint.LineLength = 0 Then
endpoint.Insert(String.Format("{0}#End Region '{1}{2}", _
pad, rName, vbCrLf))
Else
endpoint.Insert(String.Format("{0}#End Region ' {1}{2}", _
vbCrLf & pad, rName, vbCrLf))
End If
End If
Finally
DTE.UndoContext.Close()
End Try
End Sub
' IV: Get the description from the 1st line of code in the region
' i.e. ignore c# comment tags (///) or take 1st line of the comments (//)
' Requires adjustments for VB and other langs
Private Shared Function GetDesc(ByVal startpoint As EditPoint) As String
Dim line As String = ""
Dim tmppoint As EditPoint
line = startpoint.GetText(startpoint.LineLength)
If (line.Length > 0) Then
line = line.TrimStart(" ", vbTab)
If DTE.ActiveDocument.Language = "CSharp" Then
If (line.StartsWith("///")) Then
tmppoint = startpoint
tmppoint.LineDown()
line = GetDesc(tmppoint)
ElseIf (line.StartsWith("//")) Then
line = line.TrimStart("//", " ")
End If
line = line.Replace("{", String.Empty)
End If
line = line.TrimEnd(" ", vbTab)
End If
Return line
End Function
End Class
如何使用
安装
要安装宏,请打开 Visual Studio 并按照以下说明进行操作...
- 按 Alt+F8 打开宏资源管理器。
- 右键单击您的 MyMacros 图标,然后选择“新建模块”。
- 给新模块起一个有意义的名称并保存它。
- 右键单击新模块,然后选择“新建宏”。
- 用上面的代码替换“
Sub Macro1()
”到“End Sub
”。 - 保存宏文件。
热键分配
将新的 MakeRegion 宏分配给 Alt+R
- 在 Visual Studio 中,从菜单中单击“工具 | 选项”。
- 打开“环境”页面并选择“键盘”。
- 单击 [另存为...] 并使用有意义的名称保存默认键盘映射。(您无法修改默认键盘映射。)
- 在“显示包含命令:”字段中,键入“MakeRegion”。
- 从列表中选择新的 MakeRegion 宏。
- 在“使用新快捷键:”字段中,选择“文本编辑器”。
- 在“按快捷键(s):”字段中,按“Alt+R”。
- 单击 [分配] 按钮。
- 单击 [确定] 按钮。
您完成了! 现在,打开文本编辑器,选择一些文本,然后按 [Alt+R]。 第一次运行时需要几秒钟才能显示出来。 当对话框弹出时,键入区域的名称并按 Enter。
快乐的区域化!