Visual studio 用于折叠自定义节点的Visual Studio宏

Visual studio 用于折叠自定义节点的Visual Studio宏,visual-studio,Visual Studio,如果我有这样的东西: namespace SomeNameSpace { #region Usings using System.Collections.Generic; using System.Security; #endregion /// <summary> Implements a dictionary with several keys. </summary&g

如果我有这样的东西:

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()
         {

         }
    }
}

它看起来比实际情况更复杂。在任何文档上运行它,它将显示所有属性、类、枚举等,但由于某些原因,它找不到注释或区域。

第一个函数
IncludeMember
用于确定要排除的成员类型。例如,在本例中,我不会折叠名称空间并使用指令:

' filter some mebers. for example using statemets cannot be collapsed so exclude them. 
    Function IncludeMember(ByVal member As EnvDTE.CodeElement)

        If member.Kind = vsCMElement.vsCMElementIDLImport Then
            Return False
        ElseIf member.Kind = vsCMElement.vsCMElementNamespace Then
            Return False  ' I do not want to colapse enums
        End If

        Return True

    End Function

    Sub CollapseNodes()

        ' activate working window
        DTE.Windows.Item(DTE.ActiveDocument.Name).Activate()

        ' expand everything to start

        Try
            DTE.ExecuteCommand("Edit.StopOutlining")
        Catch
        End Try

        Try
            DTE.ExecuteCommand("Edit.StartAutomaticOutlining")
        Catch
        End Try


        ' get text of document and replace all new lines with \r\n
        Dim objTextDoc As TextDocument
        Dim objEditPt As EnvDTE.EditPoint
        Dim text As String
        ' Get a handle to the new document and create an EditPoint.
        objTextDoc = DTE.ActiveDocument.Object("TextDocument")
        objEditPt = objTextDoc.StartPoint.CreateEditPoint
        ' Get all Text of active document
        text = objEditPt.GetText(objTextDoc.EndPoint)
        text = System.Text.RegularExpressions.Regex.Replace( _
                        text, _
                        "(\r\n?|\n\r?)", ChrW(13) & ChrW(10) _
                    )

        ' add new line to text so that lines of visual studio match with index of array
        Dim lines As String() = System.Text.RegularExpressions.Regex.Split(vbCrLf & text, vbCrLf)

        ' list where whe will place all colapsable items
        Dim targetLines As New System.Collections.Generic.List(Of Integer)

        ' regex that we will use to check if a line contains a #region
        Dim reg As New System.Text.RegularExpressions.Regex(" *#region( |$)")

        Dim i As Integer
        For i = 1 To lines.Length - 1

            If reg.Match(lines(i)).Success Then
                targetLines.Add(i)
            End If

        Next


        Dim fileCM As FileCodeModel
        Dim elts As EnvDTE.CodeElements
        Dim elt As EnvDTE.CodeElement

        Dim projectItem = DTE.ActiveDocument.ProjectItem

        Dim temp = projectItem.Collection.Count

        Dim b = DirectCast(DirectCast(projectItem.Document, EnvDTE.Document).ActiveWindow, EnvDTE.Window).ContextAttributes

        fileCM = projectItem.FileCodeModel
        elts = fileCM.CodeElements
        For i = 1 To elts.Count
            elt = elts.Item(i)
            CollapseE(elt, elts, i, targetLines)
        Next

        ' now that we have the lines that we will plan to collapse sort them. it is important to go in order
        targetLines.Sort()

        ActivateWorkingWindow()

        ' go in reverse order so that we can collapse nested regions
        For i = targetLines.Count - 1 To 0 Step -1


            DTE.ExecuteCommand("Edit.Goto", targetLines(i))

            DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")

        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, ByRef targetLines As System.Collections.Generic.List(Of 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) Or elt.Kind = EnvDTE.vsCMElement.vsCMElementNamespace) Then
            Dim i As Integer
            Dim mems As EnvDTE.CodeElements

            mems = elt.Members
            For i = 1 To mems.Count

                CollapseE(mems.Item(i), mems, i, targetLines)

            Next

        End If


        If (epStart.LessThan(epEnd)) Then
            If IncludeMember(elt) Then
                targetLines.Add(epStart.Line)
            End If
        End If



    End Sub
”筛选一些meber。例如,无法折叠使用statemets,因此请排除它们。
函数IncludeMember(ByVal成员作为EnvDTE.codelement)
如果member.Kind=vsCMElement.vsCMElementIDLImport,则
返回错误
ElseIf member.Kind=vsCMElement.vsCMElementNamespace然后
Return False“我不想关闭枚举”
如果结束
返回真值
端函数
亚胶原蛋白
'激活工作窗口
DTE.Windows.Item(DTE.ActiveDocument.Name).Activate()
"一切从开始,
尝试
DTE.ExecuteCommand(“编辑.停止大纲显示”)
抓住
结束尝试
尝试
DTE.ExecuteCommand(“Edit.StartAutomaticOutlineing”)
抓住
结束尝试
'获取文档文本并将所有新行替换为\r\n
Dim objTextDoc作为文本文档
作为EnvDTE.EditPoint的Dim objEditPt
将文本变暗为字符串
'获取新文档的句柄并创建编辑点。
objTextDoc=DTE.ActiveDocument.Object(“TextDocument”)
objEditPt=objTextDoc.StartPoint.CreateEditPoint
'获取活动文档的所有文本
text=objEditPt.GetText(objTextDoc.EndPoint)
text=System.text.RegularExpressions.Regex.Replace(_
文本_
(\r\n?| \n\r?)、ChrW(13)和ChrW(10)_
)
'向文本添加新行,以便visual studio的行与数组的索引匹配
按字符串()调整行的大小=System.Text.RegularExpressions.Regex.Split(vbCrLf&Text,vbCrLf)
'列出将放置所有可折叠项目的位置
将目标线调整为新System.Collections.Generic.List(整型)
'用于检查行是否包含#区域的正则表达式
Dim reg作为新的System.Text.RegularExpressions.Regex(“*#region(|$)”)
作为整数的Dim i
对于i=1到线。长度-1
如果注册匹配(第(i)行),则成功
目标线。添加(i)
如果结束
下一个
将fileCM设置为FileCodeModel
将ELT变暗为EnvDTE.codelements
将elt设置为EnvDTE.codelement
Dim projectItem=DTE.ActiveDocument.projectItem
Dim temp=projectItem.Collection.Count
Dim b=DirectCast(DirectCast(projectItem.Document,EnvDTE.Document).ActiveWindow,EnvDTE.Window).ContextAttributes
fileCM=projectItem.FileCodeModel
elts=fileCM.codelements
对于i=1至elts。计数
elt=elts.项目(i)
折叠视图(elt、elts、i、targetLines)
下一个
现在我们已经有了我们计划崩溃的线路,我们将对它们进行排序。按顺序走很重要
targetLines.Sort()
ActivateWorkingWindow()
'按相反顺序进行,以便折叠嵌套区域
对于i=targetLines。计数-1到0步骤-1
DTE.ExecuteCommand(“Edit.Goto”,targetLines(i))
DTE.ExecuteCommand(“编辑.切换大纲扩展”)
下一个
端接头
“”帮助程序到大纲视图代码。递归地勾勒出英语教学的成员。
''
子折叠视图(ByVal elt作为EnvDTE.codelement,ByVal elts作为EnvDTE.codelements,ByVal loc作为Integer,ByRef targetLines作为System.Collections.Generic.List(整数))
将epStart设置为EnvDTE.EditPoint
将epEnd设置为EnvDTE.EditPoint
epStart=elt.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint()
epEnd=elt.GetEndPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint()'复制它,因为我们稍后会移动它。
epStart.EndOfLine()
如果((elt.IsCodeType())和(elt.Kind EnvDTE.vsCMElement.vsCMElementDelegate)或elt.Kind=EnvDTE.vsCMElement.vsCMElementNamespace),则
作为整数的Dim i
Dim mems作为环境编码元件
mems=英语教学成员
对于i=1到mems.Count
折叠视图(mems.项目(i)、mems、i、目标线)
下一个
如果结束
如果(epStart.LessThan(epEnd))那么
如果包含成员(elt),则
targetLines.Add(eStart.Line)
如果结束
如果结束
端接头
' filter some mebers. for example using statemets cannot be collapsed so exclude them. 
    Function IncludeMember(ByVal member As EnvDTE.CodeElement)

        If member.Kind = vsCMElement.vsCMElementIDLImport Then
            Return False
        ElseIf member.Kind = vsCMElement.vsCMElementNamespace Then
            Return False  ' I do not want to colapse enums
        End If

        Return True

    End Function

    Sub CollapseNodes()

        ' activate working window
        DTE.Windows.Item(DTE.ActiveDocument.Name).Activate()

        ' expand everything to start

        Try
            DTE.ExecuteCommand("Edit.StopOutlining")
        Catch
        End Try

        Try
            DTE.ExecuteCommand("Edit.StartAutomaticOutlining")
        Catch
        End Try


        ' get text of document and replace all new lines with \r\n
        Dim objTextDoc As TextDocument
        Dim objEditPt As EnvDTE.EditPoint
        Dim text As String
        ' Get a handle to the new document and create an EditPoint.
        objTextDoc = DTE.ActiveDocument.Object("TextDocument")
        objEditPt = objTextDoc.StartPoint.CreateEditPoint
        ' Get all Text of active document
        text = objEditPt.GetText(objTextDoc.EndPoint)
        text = System.Text.RegularExpressions.Regex.Replace( _
                        text, _
                        "(\r\n?|\n\r?)", ChrW(13) & ChrW(10) _
                    )

        ' add new line to text so that lines of visual studio match with index of array
        Dim lines As String() = System.Text.RegularExpressions.Regex.Split(vbCrLf & text, vbCrLf)

        ' list where whe will place all colapsable items
        Dim targetLines As New System.Collections.Generic.List(Of Integer)

        ' regex that we will use to check if a line contains a #region
        Dim reg As New System.Text.RegularExpressions.Regex(" *#region( |$)")

        Dim i As Integer
        For i = 1 To lines.Length - 1

            If reg.Match(lines(i)).Success Then
                targetLines.Add(i)
            End If

        Next


        Dim fileCM As FileCodeModel
        Dim elts As EnvDTE.CodeElements
        Dim elt As EnvDTE.CodeElement

        Dim projectItem = DTE.ActiveDocument.ProjectItem

        Dim temp = projectItem.Collection.Count

        Dim b = DirectCast(DirectCast(projectItem.Document, EnvDTE.Document).ActiveWindow, EnvDTE.Window).ContextAttributes

        fileCM = projectItem.FileCodeModel
        elts = fileCM.CodeElements
        For i = 1 To elts.Count
            elt = elts.Item(i)
            CollapseE(elt, elts, i, targetLines)
        Next

        ' now that we have the lines that we will plan to collapse sort them. it is important to go in order
        targetLines.Sort()

        ActivateWorkingWindow()

        ' go in reverse order so that we can collapse nested regions
        For i = targetLines.Count - 1 To 0 Step -1


            DTE.ExecuteCommand("Edit.Goto", targetLines(i))

            DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")

        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, ByRef targetLines As System.Collections.Generic.List(Of 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) Or elt.Kind = EnvDTE.vsCMElement.vsCMElementNamespace) Then
            Dim i As Integer
            Dim mems As EnvDTE.CodeElements

            mems = elt.Members
            For i = 1 To mems.Count

                CollapseE(mems.Item(i), mems, i, targetLines)

            Next

        End If


        If (epStart.LessThan(epEnd)) Then
            If IncludeMember(elt) Then
                targetLines.Add(epStart.Line)
            End If
        End If



    End Sub