views:

4869

answers:

10

When editing really long code blocks (which should definitely be refactored anyway, but that's beyond the scope of this question), I often long for the ability to collapse statement blocks like one can collapse function blocks. That is to say, it would be great if the minus icon appeared on the code outline for everything enclosed in braces. It seems to appear for functions, classes, regions, namespaces, usings, but not for conditional or iterative blocks. It would be fantastic if I could collapse things like ifs, switches, foreaches, that kind of thing!

Googling into that a bit, I discovered that apparently C++ outlining in VS allows this but C# outlining in VS does not. I don't really get why. Even notepad++ will so these collapses if I select the C# formatting, so I don't get why Visual Studio doesn't.

Does anyone know of a VS2008 add-in that will enable this behavior? Or some sort of hidden setting for it?

Edited to add: inserting regions is of course an option and it did already occur to me, but quite frankly, I shouldn't have to wrap things in a region that are already wrapped in braces... if I was going to edit the existing code, I would just refactor it to have better separation of concern anyway. ("wrapping" with new methods instead of regions ;)

+6  A: 

I'm not aware of add-ins, but you mentioned regions and I see nothing wrong with doing something like this...

foreach (Item i in Items)
{
  #region something big happening here
  ...
  #endregion

  #region something big happening here too
  ...
  #endregion

  #region something big happening here also
  ...
  #endregion
}

EDIT: In response to the question's EDIT: You're right, sticking a bunch of regions everywhere isn't ideal and refactoring is probably the way to go. But it seems that you're looking for something magical that will "organize" the code for you, and I don't think that exists.

Kon
IIRC, you can't define regions within a function.
Joel Coehoorn
his code is correct; you are not limited to where you can define regions like this (as long as you aren't ending outside of scope of the beginning)
John
@john is correct
kenny
Please see the edit of the original question; manual action required on the part of the developer (even using a one-or-two-click region-wrapping shortcut) doesn't really answer the question/desire for this to be done automatically
Grank
@Joel I believe you're thinking of VB.NET
Bryan Anderson
+2  A: 

Visual Studio 2008 supports regions inside of functions as long as you keep them in the same code hierarchical level

#region Won't work
for(int i = 0; i<Count; i++)
{
//do something
#endregion
}

for(int i=0; i<Count; i++)
{
#region Works fine
//do lots of stuff
#endregion
}
Chris Marisic
This isn't new in 2008. 2005 supports it as well.
Kon
+7  A: 

You can collapse specific blocks of text within visual studio, but you have to turn off automatic outlining.

Right click in your code window and select (Outlining | Stop Outlining)

Then, select some text, right click and select (Outlining | Hide Selection)

When you turn on automatic outlining again, your custom "Regions" will no longer collapse.

Matt Brunell
Fair enough. I'm hoping to find through this question a way to add these blocks to the automatic outlining though, as it doesn't really make sense for them not to be.
Grank
+1  A: 

I upvoted the question because it's one of my pet peeves that #regions absolutely stink. They might do something very obscure in the background (in wich case I might adjust my opinion) but I doubt that. What I do believe is:

  • #regions don't increase readability
  • #regions make refactoring code harder because it's easy to accidentally cut out one end but forget or miss the other, leaving you with a mess that won't compile for no good reason.

Most of my c# career was spent maintaining existing code, in a team environment where I didn't get to decide the coding standards so the grass might be greener elsewhere, but seriously, who thought of a dumb way to make a comment look like a compiler directive and made it able to break a build?...

I often work on a piece of c# code on my Mac with textmate, just to get better code folding.

Kris
Yes that means i want the requested functionality as well
Kris
Amen to that. I've actually seen cases where the programmer began a region in one method and closed it in the next. (On purpose!) Fortunately, VS 2008 doesn't allow that like 2003 did.
Jeromy Irvine
I think using regions is good practice if you put them in appropriate locations and name them well. It makes navigating code much quicker after you collapse to definition and expand out only a few things you need and regions hide the rest.
Chris Marisic
Chris Marisic: did you ever meet any code that had endregions named? there just is no easy way to see what region and endregion belongs to. You're also not stating anything that would make regions a better idea than normal code folding.
Kris
We always name the #endregion directive, in fact if you don't you will get a remark from the code reviewer.Using regions is fine, but you should enforce some regulations in your code guidelines.
Oxymoron
A: 

I will add here that in VS 2010 Microsoft has added WPF adorner capabilities using Managed Extensibility Framework (MEF), this will allow us to extend the source code editor to organize them in a much better way to make it more readable and accessible.

For instance the Summary Comments visualizer that Scott Gu demoed at PDC 2008.

So look forward to a better tomorrow for developers :)

Vin
+3  A: 

BTW: I found that these two shortcuts made my life very easy:

Toggle outline: Ctrl + M, M Collapse All: Ctrl + M, O

Gaurav
Ctrl + M, L toggles a recursive collapse/expand. Of course, this can leave you with a single line. Inside that line, though (Ctrl + M, M) leaves you with a nicely summarized file.
patridge
I use these all the time...
sliderhouserules
A: 

# region ,#endregion is the smart option.

StyleCop rule SA1123: DoNotPlaceRegionsWithinElements: A violation of this rule occurs whenever a region is placed within the body of a code element. In many editors, including Visual Studio, the region will appear collapsed by default, hiding the code within the region. It is generally a bad practice to hide code within the body of an element, as this can lead to bad decisions as the code is maintained over time.
280Z28
+1  A: 

This feature has been added to Visual Studio 2010's C# editor. I can't find the source verifying it was actually put in, but I remember seeing it on one of the Dev 10 team member blogs talking about changes since Beta 1 or something. As a consolation, here's one Microsoft comment suggesting they wanted to add it.

280Z28
+1  A: 

I'm not sure either why code blocks, developed AEONS ago, are not a folding atom in VS. There must be SOME logic that says enabling this was a BAD THING somehow. It doesn't seem like an IDE to me if I can't fold blocks.

I mean, you want to make an IDE. You start with the editor. Folding blocks, as defined by curly brackets, which have an old, established, deep meaning in so many languages, is like the SECOND OR THIRD feature you implement.

So the answer is, someone with more political power than coding skill decided this was Bad, or everyone collectively somehow missed IDE feature number 3.

Anderlan
A: 

Coderush will outline all code blocks for you. Not sure if it allows you to expand/collapse the blocks, but outlining is the next best thing. I use resharper instead of coderush which as far as I know doesn't provide block collapsing either :(

goku_da_master