如果我有类似的东西:
namespace SomeNameSpace
{
#region Usings
using System.Collections.Generic;
using System.Security;
#endregion
/// <summary> Implements a dictionary with several keys. </summary>
/// <typeparam name="Value"> What type of elements we will be storing in the dictionary</typeparam>
public class MultiKeyDic<Value>
{
/// <summary> a very long summary that can be
/// collapsed and expanded. </summary>
public int SomeInt {get;set;}
public void someMethod()
{
}
}
}
如何创建一个可以找到所有可扩展位置(节点)的宏。如果我想折叠所有节点,我将不得不按someMethod()
, summary of SomeInt
, class MultiKeyDic
,summary of class MultiKeyDic
和#region Usings
finally的顺序折叠节点namespace
。
我知道命令ctrl++M会O折叠所有内容,但我不想折叠所有内容。例如,我可能不想折叠评论。如果我折叠所有内容,然后展开扩展父节点的评论。
到目前为止,我已经创建了这个可以找到大多数节点的宏:
Sub VisitAllNodes()
Dim i As Integer
Dim fileCM As FileCodeModel
Dim elts As EnvDTE.CodeElements
Dim elt As EnvDTE.CodeElement
fileCM = DTE.ActiveDocument.ProjectItem.FileCodeModel
elts = fileCM.CodeElements
For i = 1 To elts.Count
elt = elts.Item(i)
CollapseE(elt, elts, i)
Next
End Sub
'' Helper to OutlineCode. Recursively outlines members of elt.
''
Sub CollapseE(ByVal elt As EnvDTE.CodeElement, ByVal elts As EnvDTE.CodeElements, ByVal loc As Integer)
Dim epStart As EnvDTE.EditPoint
Dim epEnd As EnvDTE.EditPoint
epStart = elt.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint()
epEnd = elt.GetEndPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint() ' Copy it because we move it later.
epStart.EndOfLine()
If ((elt.IsCodeType()) And (elt.Kind <> EnvDTE.vsCMElement.vsCMElementDelegate)) Then
Dim i As Integer
Dim mems As EnvDTE.CodeElements
mems = elt.Members
For i = 1 To mems.Count
Dim temp As EnvDTE.CodeElement = mems.Item(i)
Dim t As String = [Enum].GetName(GetType(EnvDTE.vsCMElement), temp.Kind)
MsgBox("Found member (" & t & ") at line# " & temp.StartPoint.Line)
CollapseE(mems.Item(i), mems, i)
Next
ElseIf (elt.Kind = EnvDTE.vsCMElement.vsCMElementNamespace) Then
Dim i As Integer
Dim mems As EnvDTE.CodeElements
mems = elt.Members
For i = 1 To mems.Count
Dim temp As EnvDTE.CodeElement = mems.Item(i)
Dim t As String = [Enum].GetName(GetType(EnvDTE.vsCMElement), temp.Kind)
MsgBox("Found member (" & t & ") at line# " & temp.StartPoint.Line)
CollapseE(mems.Item(i), mems, i)
Next
End If
'Return
' collapse the element
If (epStart.LessThan(epEnd)) Then
loc = loc + 1
If (loc <= elts.Count) Then
epEnd.MoveToPoint(elts.Item(loc).GetStartPoint(vsCMPart.vsCMPartHeader))
epEnd.LineUp()
epEnd.EndOfLine()
End If
epStart.OutlineSection(epEnd)
End If
End Sub
它看起来比实际情况更复杂。在任何文档上运行它,它将显示所有属性、类、枚举等,但由于某种原因,它找不到注释或区域。