Visual Studio, Collapse/Extends Regions ONLY shortcut

后端 未结 3 1927
野的像风
野的像风 2020-12-15 23:05

Is there any shortcut to collapse/expand the regions ONLY? Meaning, if I have a region with 5 methods in it, and I hit collapse, the region will collapse, and when I will hi

相关标签:
3条回答
  • 2020-12-15 23:27

    why not simply hit

    ctrl + m + m

    while cursor in #region regionname

    0 讨论(0)
  • 2020-12-15 23:46

    I've written a free Visual Studio extension "Menees VS Tools" that provides commands for "Collapse All Regions" and "Expand All Regions". It's available for VS versions from 2003 to 2013. The VS 2013 and VS 2012 versions are available in the Visual Studio Gallery.

    0 讨论(0)
  • 2020-12-15 23:51

    You can use the following macros to expand/collapse the regions while leaving the expand/collapse state of individual methods as they were.

    I found the macro here. Note that I had to comment out the call to objSelection.EndOfDocument() from the CollapseAllRegions method for it to work properly (using 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
    
    0 讨论(0)
提交回复
热议问题