简体   繁体   中英

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.

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;

到目前为止,代码折叠都可以正常工作

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

错误放置的起始花括号

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.

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:

静态弦

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:

会员名单

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.

ps: 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

I'm not sure exactly what the issue is here, as this certainly doesn't seem like normal behaviour (I've never experienced it).

There are a few options though (without knowing more about the problem) that might help you.

Firstly, someone has given a very good answer on another thread about customising the visual studio intellisense: Custom Intellisense Extension

If that isn't an option, perhaps have a look at the extensions that are provided for Visual Studio 2010.

Productivity Power Tools might help you out with some of your refactoring. You can get this through Visual Studio by going to Tools - Extension Mananger. And then select the option and Install it. Power Tools is quite limited so this might not be enough. I'm sure people can offer other alternatives but I would certainly recommend ReSharper: http://www.jetbrains.com/resharper/ . You can get a free trial but after that period you have to pay for it. I use it and the extensions it provides are fantastic. You can completely change the way Visual Studio behaves for your personal preferences.

Sorry I can't offer any other help, but if you can provide some more information then maybe we can find a solution (eg What do you mean by other methods are expanding etc).

Well, this problem that you encounter is a universal one and it can be sometimes irritating. In my experience, visual studio goes haywire especially when you are writing a lot of block statements in a large code file, whenever such happens you can save your work and restart VS. But to answer your question, make sure that you first try to open any collapsed code you want to edit and as for copying/cutting try to highlight the collapsed code first before carrying out of the editing options this informs the editor that you want to edit the highlighted segment of code. Hope it helps

vs选项

Maybe this can be of some help ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM