Shortcut to collapse to definitions except regions

2020-07-06 07:20发布

问题:

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.

回答1:

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.



回答2:

I find Ctrl + M, Ctrl + O is really useful to collapse everything.

Have you read Jeff's blog post about regions? There's a few more useful shortcuts he lists.

Why do you want to keep these expanded, do you really need the region if you don't want it collapsed?