问题 Visual Studio,仅折叠/扩展区域快捷方式


是否有任何快捷方式可以折叠/扩展区域?意思是,如果我有一个包含5个方法的区域,并且我遇到了崩溃,那么该区域将会崩溃,当我进行扩展时,该区域将会扩展,我将看到所有5个方法具有与之前相同的状态(折叠/展开)。

目前我发现的快捷键会折叠ALL,或者展开ALL,或者将“All”字替换为“Current”字样。

我正在寻找一个只会折叠区域的快捷方式,并且不会对区域内的其他区块做任何事情。扩展同样的事情。

如果没有这样的事情,也许有人找到了一些视觉扩展来做到这一点?

干杯 卢卡斯


9097
2018-05-01 10:20


起源



答案:


您可以使用以下宏来展开/折叠区域,同时保留各个方法的展开/折叠状态。

我找到了宏 这里。请注意,我必须从CollapseAllRegions方法注释掉对objSelection.EndOfDocument()的调用才能使其正常工作(使用Visual Studio 2010)

Imports EnvDTE
Imports System.Diagnostics
' Macros for improving keyboard support for "#region ... #endregion"
Public Module RegionTools
    ' Expands all regions in the current document
    Sub ExpandAllRegions()

        Dim objSelection As TextSelection ' Our selection object

        DTE.SuppressUI = True ' Disable UI while we do this
        objSelection = DTE.ActiveDocument.Selection() ' Hook up to the ActiveDocument's selection
        objSelection.StartOfDocument() ' Shoot to the start of the document

        ' Loop through the document finding all instances of #region. This action has the side benefit
        ' of actually zooming us to the text in question when it is found and ALSO expanding it since it
        ' is an outline.
        Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText)
            ' This next command would be what we would normally do *IF* the find operation didn't do it for us.
            'DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
        Loop
        objSelection.StartOfDocument() ' Shoot us back to the start of the document
        DTE.SuppressUI = False ' Reenable the UI

        objSelection = Nothing ' Release our object

    End Sub

    ' Collapses all regions in the current document
    Sub CollapseAllRegions()

        Dim objSelection As TextSelection ' Our selection object

        ExpandAllRegions() ' Force the expansion of all regions

        DTE.SuppressUI = True ' Disable UI while we do this
        objSelection = DTE.ActiveDocument.Selection() ' Hook up to the ActiveDocument's selection
        objSelection.EndOfDocument() ' Shoot to the end of the document

        ' Find the first occurence of #region from the end of the document to the start of the document. Note:
        ' Note: Once a #region is "collapsed" .FindText only sees it's "textual descriptor" unless
        ' vsFindOptions.vsFindOptionsMatchInHiddenText is specified. So when a #region "My Class" is collapsed,
        ' .FindText would subsequently see the text 'My Class' instead of '#region "My Class"' for the subsequent
        ' passes and skip any regions already collapsed.
        Do While (objSelection.FindText("#region", vsFindOptions.vsFindOptionsBackwards))
            DTE.ExecuteCommand("Edit.ToggleOutliningExpansion") ' Collapse this #region
            'objSelection.EndOfDocument() ' Shoot back to the end of the document for
            ' another pass.
        Loop
        objSelection.StartOfDocument() ' All done, head back to the start of the doc
        DTE.SuppressUI = False ' Reenable the UI

        objSelection = Nothing ' Release our object

    End Sub
End Module

5
2017-09-16 13:06



Visual Studio 2013的任何想法;没有宏观支持..? - wasatchwizard


答案:


您可以使用以下宏来展开/折叠区域,同时保留各个方法的展开/折叠状态。

我找到了宏 这里。请注意,我必须从CollapseAllRegions方法注释掉对objSelection.EndOfDocument()的调用才能使其正常工作(使用Visual Studio 2010)

Imports EnvDTE
Imports System.Diagnostics
' Macros for improving keyboard support for "#region ... #endregion"
Public Module RegionTools
    ' Expands all regions in the current document
    Sub ExpandAllRegions()

        Dim objSelection As TextSelection ' Our selection object

        DTE.SuppressUI = True ' Disable UI while we do this
        objSelection = DTE.ActiveDocument.Selection() ' Hook up to the ActiveDocument's selection
        objSelection.StartOfDocument() ' Shoot to the start of the document

        ' Loop through the document finding all instances of #region. This action has the side benefit
        ' of actually zooming us to the text in question when it is found and ALSO expanding it since it
        ' is an outline.
        Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText)
            ' This next command would be what we would normally do *IF* the find operation didn't do it for us.
            'DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
        Loop
        objSelection.StartOfDocument() ' Shoot us back to the start of the document
        DTE.SuppressUI = False ' Reenable the UI

        objSelection = Nothing ' Release our object

    End Sub

    ' Collapses all regions in the current document
    Sub CollapseAllRegions()

        Dim objSelection As TextSelection ' Our selection object

        ExpandAllRegions() ' Force the expansion of all regions

        DTE.SuppressUI = True ' Disable UI while we do this
        objSelection = DTE.ActiveDocument.Selection() ' Hook up to the ActiveDocument's selection
        objSelection.EndOfDocument() ' Shoot to the end of the document

        ' Find the first occurence of #region from the end of the document to the start of the document. Note:
        ' Note: Once a #region is "collapsed" .FindText only sees it's "textual descriptor" unless
        ' vsFindOptions.vsFindOptionsMatchInHiddenText is specified. So when a #region "My Class" is collapsed,
        ' .FindText would subsequently see the text 'My Class' instead of '#region "My Class"' for the subsequent
        ' passes and skip any regions already collapsed.
        Do While (objSelection.FindText("#region", vsFindOptions.vsFindOptionsBackwards))
            DTE.ExecuteCommand("Edit.ToggleOutliningExpansion") ' Collapse this #region
            'objSelection.EndOfDocument() ' Shoot back to the end of the document for
            ' another pass.
        Loop
        objSelection.StartOfDocument() ' All done, head back to the start of the doc
        DTE.SuppressUI = False ' Reenable the UI

        objSelection = Nothing ' Release our object

    End Sub
End Module

5
2017-09-16 13:06



Visual Studio 2013的任何想法;没有宏观支持..? - wasatchwizard


为什么不简单地打

CTRL +  +

而光标在 #region regionname


8
2017-09-16 13:10



这仅适用于一个地区 - Christian Hagelid


我写了一个免费的Visual Studio扩展“Menees VS Tools“它提供了”折叠所有区域“和”展开所有区域“的命令。它适用于2003年至2013年的VS版本。 2013 和VS. 2012 Visual Studio库中提供了各种版本。


3
2018-01-05 00:11