C# 默认情况下是否可以有Visual Studio折叠摘要部分

C# 默认情况下是否可以有Visual Studio折叠摘要部分,c#,visual-studio-2010,C#,Visual Studio 2010,默认情况下,VisualStudio是否可以折叠方法和类的摘要部分?或者是否有一个命令可以折叠所有摘要部分而不折叠方法本身 我在下面提供了一个摘要部分示例 折叠示例 扩展示例 您必须使用宏。下面是一个详细描述该过程的示例。为了方便起见,我在这里粘贴了一段代码片段: ''' <summary> ''' Collapse XML comment for all code members '''</summary> Sub CollapseXmlComments()

默认情况下,VisualStudio是否可以折叠方法和类的摘要部分?或者是否有一个命令可以折叠所有摘要部分而不折叠方法本身

我在下面提供了一个摘要部分示例

折叠示例

扩展示例

您必须使用宏。下面是一个详细描述该过程的示例。为了方便起见,我在这里粘贴了一段代码片段:

''' <summary>
''' Collapse XML comment for all code members
'''</summary>
Sub CollapseXmlComments()
    Try
        DTE.UndoContext.Open("Collapse XML comments")

        Dim ce As CodeElement2
        For Each ce In DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElements
            collapseSubmembers(ce, False)
        Next

        DTE.UndoContext.Close()
    Catch ex As Exception
        DTE.UndoContext.Close()
    End Try
End Sub

''' <summary>
''' Toggles the outline of XML comment for all code members.
'''</summary>
Sub ToggleXmlComments()
    Try
        DTE.UndoContext.Open("Toggle XML comments outline")

        'remember selection
        Dim oldAnchor, oldActive As EnvDTE.TextPoint
        Dim sel As TextSelection = CType(DTE.ActiveDocument.Selection, TextSelection)
        oldAnchor = sel.AnchorPoint.CreateEditPoint
        oldActive = sel.ActivePoint.CreateEditPoint

        Dim ce As CodeElement2
        For Each ce In DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElements
            collapseSubmembers(ce, True)
        Next

        'restore selection
        sel.MoveToAbsoluteOffset(oldAnchor.AbsoluteCharOffset) 'set active point
        sel.SwapAnchor() 'set anchor to active point
        sel.MoveToAbsoluteOffset(oldActive.AbsoluteCharOffset, True)

        DTE.UndoContext.Close()
    Catch ex As Exception
        DTE.UndoContext.Close()
    End Try
End Sub

''' <summary>Collapses the member and its sub members if any.</summary>
''' <param name="ce">The member.</param>
''' <param name="toggle">If True, the comment outline is toggled,
''' otherwise it is collapsed.</param>
Private Sub collapseSubmembers(ByVal ce As CodeElement2, ByVal toggle As Boolean)
    Dim memberStart, commentStart, commentEnd As EditPoint2
    Dim comChars As String

    Select Case DTE.ActiveDocument.ProjectItem.FileCodeModel.Language
        Case "{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}"
            'VB
            comChars = "'''"
        Case Else
            'C#
            comChars = "///"
    End Select

    Try
        memberStart = ce.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint
        commentStart = getCommentStart(memberStart.CreateEditPoint, comChars)
        commentEnd = getCommentEnd(commentStart.CreateEditPoint, comChars)
        If toggle Then
            'toggle
            CType(DTE.ActiveDocument.Selection, TextSelection).MoveToPoint(commentStart)
            DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
        Else
            'collapse
            commentStart.OutlineSection(commentEnd)
        End If
    Catch ex As Exception
    End Try

    'try submembers
    If ce.IsCodeType Then
        Dim ce2 As CodeElement2
        For Each ce2 In CType(ce, CodeType).Members
            collapseSubmembers(ce2, toggle)
        Next
    ElseIf ce.Kind = vsCMElement.vsCMElementNamespace Then
        Dim ce2 As CodeElement2
        For Each ce2 In CType(ce, CodeNamespace).Members
            collapseSubmembers(ce2, toggle)
        Next
    End If
End Sub

''' <summary>Gets starting point of the comment.</summary>
''' <param name="ep">Commented member start point.</param>
''' <param name="commentChars">The comment character.
''' It is ''' for VB or /// for C#.</param>
''' <returns></returns>
Private Function getCommentStart(ByVal ep As EditPoint2, ByVal commentChars As String) As EditPoint2
    Try
        Dim line, lastCommentLine As String
        ep.StartOfLine()
        ep.CharLeft()
        While Not ep.AtStartOfDocument
            line = ep.GetLines(ep.Line, ep.Line + 1).Trim
            If line.Length = 0 Or line.StartsWith(commentChars) Then
                If line.Length> 0 Then
                    lastCommentLine = ep.Line
                End If
                ep.StartOfLine()
                ep.CharLeft()
            Else
                Exit While
            End If
        End While

        ep.MoveToLineAndOffset(lastCommentLine, 1)
        While ep.GetText(commentChars.Length) <> commentChars
            ep.CharRight()
        End While

        Return ep.CreateEditPoint
    Catch ex As Exception
    End Try
End Function

''' <summary>Gets ending point of the comment.</summary>
''' <param name="ep">Comment start point.</param>
''' <param name="commentChars">The comment character.
''' It is ''' for VB or /// for C#.</param>
''' <returns></returns>
Private Function getCommentEnd(ByVal ep As EditPoint2, ByVal commentChars As String) As EditPoint2
    Try
        Dim line As String
        Dim lastCommentPoint As EditPoint
        lastCommentPoint = ep.CreateEditPoint
        ep.EndOfLine()
        ep.CharRight()
        While Not ep.AtEndOfDocument
            line = ep.GetLines(ep.Line, ep.Line + 1).Trim
            If line.StartsWith(commentChars) Then
                lastCommentPoint = ep.CreateEditPoint
                ep.EndOfLine()
                ep.CharRight()
            Else
                Exit While
            End If
        End While

        lastCommentPoint.EndOfLine()
        Return lastCommentPoint
    Catch ex As Exception
    End Try
End Function
“”
''折叠所有代码成员的XML注释
'''
子CollapseXmlComments()
尝试
Open(“折叠XML注释”)
将ce设置为CodeElement2
对于DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElements中的每个ce
collapseSubmembers(ce,False)
下一个
DTE.UndoContext.Close()
特例
DTE.UndoContext.Close()
结束尝试
端接头
''' 
''切换所有代码成员的XML注释大纲。
'''
子切换XMLComments()
尝试
Open(“切换XML注释大纲”)
“记住选择
标注oldAnchor,oldActive作为EnvDTE.TextPoint
文本选择的Dim sel=CType(DTE.ActiveDocument.Selection,文本选择)
oldAnchor=sel.AnchorPoint.CreateEditPoint
oldActive=sel.ActivePoint.CreateEditPoint
将ce设置为CodeElement2
对于DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElements中的每个ce
collapseSubmembers(ce,True)
下一个
'恢复选择
选择移动到绝对偏移(oldAnchor.AbsoluteCharOffset)“设置激活点
sel.SwapAnchor()'将锚点设置为活动点
选择移动到绝对偏移(oldActive.AbsoluteCharOffset,真)
DTE.UndoContext.Close()
特例
DTE.UndoContext.Close()
结束尝试
端接头
''折叠成员及其子成员(如有)。
''成员。
''如果为真,则切换注释大纲,
''否则它就倒塌了。
私有子collapseSubmembers(ByVal ce作为CodeElement2,ByVal切换作为布尔值)
Dim memberStart、commentStart、commentEnd作为EditPoint2
像弦一样暗淡的comChars
选择Case DTE.ActiveDocument.ProjectItem.FileCodeModel.Language
案例“{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}”
"VB
comChars=“””
其他情况
C#
comChars=“//”
结束选择
尝试
memberStart=ce.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes)。CreateEditPoint
commentStart=getCommentStart(memberStart.CreateEditPoint,comChars)
commentEnd=getCommentEnd(commentStart.CreateEditPoint,comChars)
如果切换,则
“切换
CType(DTE.ActiveDocument.Selection,TextSelection).MoveToPoint(commentStart)
DTE.ExecuteCommand(“编辑.切换大纲扩展”)
其他的
"崩溃"
commentStart.OutlineSection(commentEnd)
如果结束
特例
结束尝试
'试试子成员
如果ce.IsCodeType,则
将ce2调暗为CodeElement2
对于CType(ce,CodeType)中的每个ce2。成员
折叠子成员(ce2,切换)
下一个
ElseIf ce.Kind=vsCMElement.vsCMElementNamespace然后
将ce2调暗为CodeElement2
对于CType(ce,CodeNamespace)中的每个ce2。成员
折叠子成员(ce2,切换)
下一个
如果结束
端接头
''获取注释的起始点。
''注释了成员起始点。
“注释”字符。
对于VB来说是“”,对于C#来说是//。
''' 
私有函数getCommentStart(ByVal ep作为EditPoint2,ByVal commentChars作为String)作为EditPoint2
尝试
尺寸线,最后一行作为字符串
ep.StartOfLine()
ep.CharLeft()
而不是ep.ATSTARTOF文件
行=ep.GetLines(ep.line,ep.line+1)。修剪
如果line.Length=0或line.StartsWith(commentChars),则
如果line.Length>0,则
lastCommentLine=ep.Line
如果结束
ep.StartOfLine()
ep.CharLeft()
其他的
退出时
如果结束
结束时
ep.移动到直线和偏移(最后一行,1)
而ep.GetText(commentChars.Length)则是commentChars
ep.CharRight()
结束时
返回ep.CreateEditPoint
特例
结束尝试
端函数
''获取注释的结束点。
''注释开始点。
“注释”字符。
对于VB来说是“”,对于C#来说是//。
''' 
私有函数getCommentEnd(ByVal ep作为EditPoint2,ByVal commentChars作为String)作为EditPoint2
尝试
将线变暗为字符串
将lastCommentPoint设置为编辑点
lastCommentPoint=ep.CreateEditPoint
ep.EndOfLine()
ep.CharRight()
而不是ep.AtEndOfDocument
行=ep.GetLines(ep.line,ep.line+1)。修剪
如果line.StartsWith(commentChars)则
lastCommentPoint=ep.CreateEditPoint
ep.EndOfLine()
ep.CharRight()
其他的
退出时
如果结束
结束时
lastCommentPoint.EndOfLine()
返回最后一个点
特例
结束尝试
端函数

在VS 2010 Professional中效果很好。

好问题,我也发现了这个令人烦恼的问题,它可能会帮助你,但不会解决你的问题。请注意,在VS 2013中,宏不再可用。ctrl+m+o和展开ctrl+m+p@Maverick将所有内容折叠为定义正是原始海报想要避免的事情。@ernodeweard问题是关于VisualStudio2010的,它有一个标签。