Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop VisualStudio from expanding everything when deleting { } brackets

I asked this question few days ago and didn't get an answer, so I'll try again precising it a little.

I like to keep my code rolled up by collapsing methods, comments and xml summaries that I don't use at the moment. However, every time I edit some braces in a method, loop, switch statement or any part of the code (anything containing { or } brace), everything below expands after 1 second. Everything unfolds all the way down till the end of current file (or region, if edited code lies within it).

I can't take it anymore I'm tired of using Ctrl+M+O all the time and then re-finding edited field again. Is there any option or extension for VS2010, that would solve my problem?

Update I'm starting to realize there's no way to solve the problem. However I could also accept an answer to a modified question: Is there a way or tool that would allow me to automatically delete { and } brace pairs containing only 1 instruction? It'd be an acceptable workaround for my problem.

like image 718
Tarec Avatar asked Aug 22 '13 14:08

Tarec


People also ask

How do I remove an expanded region in Visual Studio?

or use a shortcut. Ctrl+M+O will collapse all. Ctrl+M+L will expand all. Ctrl+M+P will expand all and disable the outline.

How do I collapse all codes in Visual Studio?

(Ctrl+M, Ctrl+H) - Collapses a selected block of code that would not normally be available for outlining, for example an if block. To remove the custom region, use Stop Hiding Current (or Ctrl+M, Ctrl+U). Not available in Visual Basic.

How do I minimize all regions in Visual Studio?

Collapse All Regions command for #region blocks in C#, VB, C/C++, XAML (and possibly F#) source code files. The default key binding for this command is [Ctrl+M, Ctrl+R] Note: It may also work with F# files, when a #region block handler extension is installed.

How do I expand all in Visual Studio?

CTRL + M + P will expand all and disable outlining. CTRL + M + M will collapse/expand the current section.


2 Answers

If I understand what you need then you are asking how to stop the code below the line that I am editing from automatically expanding.I would like to tell there is no solution either from official sources or anything.Code collapsing a feature of ideal code editors like Visual Studio.

Let me explain you why this happens and how to prevent it.First let me explain essential conditions for code collapsing to work.Ideal code editors make use of Lexers and Parsers for the language the support,parsers make use of regular expressions to match language specific syntax entered by Coder or someone,in this sequence code editor stores all the locations where specific symbols like ;,{ and },".

In order for code collapsing to work effectively there must be equal number of theses symbols specified in exact order in the code being edited,whenever there is something in the source code which does not match the language specific syntax the parser flags reports the editing and formatting engine of this error.

Coming back to your problem,lets talk about what you face,to better understand it lets consider a simple code block;

Code Collapsing works perfectly upto this point

and consider there are more functions below AddNumbers that are also collapsed.Now If I understood you,then you said if I editMultiplyNumbersand remove its starting curly brace{`,the all the code below this point automatically expands.

Misplaced starting curly brace

If this is the case then the problem is because parser tries to match the language syntax and searches for any terminating curly braces below this point which is found in AddNumbers's terminating curly brace.

Update :

In one line,there is no solution to this problem,this is because Visual Studio performs Real time parsing,that's how it shows you errors at real time.Actually this is not a problem that's why this has never been reported due to which there is nothing available from official sources.However you can prevent this by changing you coding habits a bit,like when you are creating a function suppose SomeFunction(int a,int b),try to first complete the outer side of function like below;

private void SomeFunction(int a,int b)
{
//Write code later.
}

First create the function outline as above and then write some code in it like below;

private void SomeFunction(int a,int b)
{
int z=a+b;
Console.WriteLine(z);

int x=a*b;
Console.WriteLine(x);

int p=a/b;
Console.WriteLine(p);

int q=a-b;
Console.WriteLine(q);
}

Next consider you are writing an if statement,first complete the outer side like this;

if(//We'll define condition later)
{
//Write function body later.
}

If you've used code snippets in Visual Studio,then you might have noticed Visual Studio generates the snipett's outer side first,then it places the caret to first argument.

So,these are some guidelines,something that can prevent the issue.

Now head towards solution,to prevent this situation when you create a function try to first place its { and } braces before writing any code in it.

Hope this prevents the issue you are facing.If there's anything more you are facing please let me know.

like image 160
devavx Avatar answered Oct 02 '22 01:10

devavx


I am using folding myself for a static class containing localization text, and it is pretty nice to be able to hide/show things, similar to how TreeView does with nodes:

Static strings

And I have never ever faced the described problem or been annoyed with something that VS does.

However, when you are editing code, such behavior is a bit too much for Intellisense I think (as already described in another answer). Perhaps you should change your "way of thinking" ?

Take a look:

Members list

You can quickly navigate between members or have a brief overview of them by using this special window, while having all the code unfolded.

I have seen some people like #region much. But you can achieve the same (if not better in all measures) by using partial and splitting single class into pieces.

p.s.: and with last your question, why don't you just select first what you need to delete and then press Delete ? Or what would that tool do for you? Automatically go through sources, detect that and delete? If you are a programmer, then making software that will do that shouldn't take more than writing you question here =D

like image 39
Sinatr Avatar answered Oct 02 '22 00:10

Sinatr