【问题标题】:Is it possible to have Visual Studio collapse summary sections by default默认情况下是否可以让 Visual Studio 折叠摘要部分
【发布时间】:2014-07-20 04:53:41
【问题描述】:

是否可以让 Visual Studio 默认折叠方法和类的摘要部分?或者是否有一个命令可以折叠所有摘要部分而不折叠方法本身?

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

折叠示例

扩展示例

【问题讨论】:

标签: c# visual-studio-2010


【解决方案1】:

您将不得不使用宏。这是一个link,详细描述了该过程。为方便起见,我在此处粘贴代码 sn-p:

''' <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

在 VS 2010 Professional 中运行良好。

【讨论】:

  • 请注意,在 VS 2013 中宏不再可用。
  • ctrl + m + o 并展开 ctrl + m + p
  • @Maverick 将 everything 折叠成定义​​正是原始发布者想要避免的事情。
  • @ErnodeWeerd 问题是关于 Visual Studio 2010,它有一个标签。
猜你喜欢
  • 2021-06-08
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 2021-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多