Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut to collapse to definitions except regions

In vs2008, how can I (possibly with a macro) assign a shortcut key to collapse to definitons but leave regions expanded (they must expand if collapsed)?

EDIT: I hate regions but my co-workers does not (: So I want this to avoid the regions used by them.

I read jeff's post. Ctrl M + O is what I really want to do, if there were not regions.

like image 291
Serhat Ozgel Avatar asked Oct 22 '08 11:10

Serhat Ozgel


People also ask

What is the shortcut to collapse all functions in Visual Studio?

CTRL + M + O will collapse all.

How do you collapse a shortcut?

Simply press the Alt + Shift + Right Arrow keys instead. If you want to collapse all groups in a worksheet, press the Alt + Shift + A keys on your keyboard. This shortcut will collapse all groups in the active worksheet. You can also use this shortcut to expand all groups in a worksheet.

How do I Collapse All methods in Visual Studio?

(Ctrl+M, Ctrl+P) - Removes all outlining information for the entire document. (Ctrl+M, Ctrl+U) - Removes the outlining information for the currently selected user-defined region. Not available in Visual Basic. (Ctrl+M, Ctrl+O) - Collapses the members of all types.


1 Answers

I believe I have finally got the answer that I've been looking for, and I think it might help you as well, @Serhat. You said:

I read jeff's post. Ctrl M + O is what I really want to do, if there were not regions.

That was exactly what I was thinking to myself. I continued that line of thought and worked on a way to (temporarily) get rid of the #regions.

This isn't a complete solution, but I'm so glad to have something that I'm on the verge of jumping up and down. I will try to make these directions as easy as possible, although I daresay it may simply be easier to post the actual content of the macros I've created. (see link at bottom)

I've created two macros:

  1. Comment out all #region and #endregion directives.
  2. Uncomment all //#region and //#endregion occurrences.

Create the first macro:

  • Start recording a macro with Ctrl+Shift+R, and follow these steps:
  • Ctrl+H, Find what: #region, Replace with: //#region
  • Alt+A for Replace All
  • Ctrl+H, Find what: #endregion, Replace with: //#endregion
  • Alt+A for Replace All
  • End recording the macro with Ctrl+Shift+R
  • Open the Macro Explorer with Alt+F8 or Tools | Macros > Macro Explorer
  • Rename TemporaryMacro to CommentRegionDirectives

Then, create the second macro:

  • Start recording a macro with Ctrl+Shift+R, and follow these steps:
  • Ctrl+H, Find what: //#region, Replace with: #region
  • Alt+A for Replace All
  • Ctrl+H, Find what: //#endregion, Replace with: #endregion
  • Alt+A for Replace All
  • End recording the macro with Ctrl+Shift+R
  • Open the Macro Explorer with Alt+F8 or Tools | Macros > Macro Explorer
  • Rename (this new) TemporaryMacro to UncommentRegionDirectives

Now, save your macros in the Macro Explorer with Ctrl+S.

Finally, assign shortcut keys to the two macros:

  • Open Tools | Options | Environment+Keyboard
  • In "Show commands containing:" type in Directives. This should show you your two macros, named "Macros.MyMacros.RecordingModule.CommentRegionDirectives" and "...UncommentRegionDirectives"
  • Highlight the CommentRegionDirectives entry and in the "Press shortcut keys:" box type Alt+/ then click the Assign button
  • Highlight the UncommentRegionDirectives entry and in the "Press shortcut keys:" box type Alt+Shift+/ then click the Assign button (by default these two shortcut combinations are not assigned to anything)
  • Click OK to save your shortcut assignments.

Now, when you are faced with auto-collapsed #regions, hit Alt+/ to comment out the #region directives, and hit the standard Ctrl+M+O for Collapse to Definitions (if you so choose). Then later, before committing that unit with the commented-out #regions, just hit Alt+Shift+/ to uncomment the #regions and they will be reactivated.

And finally, @Serhat, thank you again for your original comment which put me on this track in the first place.

In practice there is one little hiccup that I am quite willing to live with. //#region followed by #//endregion counts as a contiguous comment and comments are still collapsed, but at least there is no code hidden in there.

Here is the promised macro text I extracted from the Macro Explorer: http://pastebin.ca/1688618, although it shouldn't be required if you manually follow the steps I outlined above.

like image 83
JMD Avatar answered Oct 10 '22 21:10

JMD