Visual Studio 2010 - C# - "Ignore spaces in declaration statements" not working
Asked Answered
S

2

8

I am using Visual Studio 2010 and have statements that look like this:

a = 1;
ab = 12;
abc = 123;
etc...

which I would very much like to turn into this:

a   = 1;
ab  = 12;
abc = 123;
etc...

without Visual Studio then turning them back to to the first example when ending the statement with a semi-colon or Pressing Ctrl-K + F (Format selected lines).

I have checked the box Tools->Options->Text Editor->C#->Formatting->Spacing->"Ignore spaces in declaration statements" but it doesn't help. It only helps for real declaration when I am creating a new variable. That is, it works on the following block:

int a   = 1;
int ab  = 12;
int abc = 123;
etc...

But I really (also) need an option "Ignore spaces in assignment statements". How do a I do that? I have tried to install "Align Assignment" (https://visualstudiogallery.msdn.microsoft.com/0cc34d69-c6f1-41e3-ac6e-5de071b3edc8) and sure enough, it aligns the rows, but then Visual Studio de-aligns them automatically when formatting the text file or editing the rows.

Secularism answered 22/10, 2014 at 11:37 Comment(8)
Personally I found it much more useful to learn new IDE rules, than to pass my old IDE experience over years through. For me first example looks natural and accepted, while second.. I would reformat it, but then your question subject get lost ;)Gulgee
@Gulgee I don't know what you mean with "learn new IDE rules" but I am bound to VS2010 version at the moment in this project. Also, when having many lines of code with long assignments it really does wonders when one can align them like I want to. So, I do not know why one would want to reformat it?Secularism
I was aligning variables like this back in old days (Borland C 3.1). Since VS I stick to auto-formatting offered by its editor. Few years later I got used to it. It might be easier to get used to it than to fight the system, that's what I mean. Btw, you are not alone. But be ready whenever copy/pasting your code into public what it gets reformatted =PGulgee
@Gulgee Ok, I get your point, but putting the formatting preferences aside, in my post I merely wonder why it behaves like it does; since there is that setting in Visual Studio called "Ignore spaces in assignment statement" and it works for my third example, but not for the second. So I was wondering if I had missed some option for the second example or how one should do to get the alignment that I want?Secularism
Have you tried: codealignment.com ? I find it makes my life easier when dealing with all my alignment needs :)Snaky
@Snaky Thanks for the tip! No, I have not tried it yet. But the thing is that I have found a tool that re-aligns the code as I want to, but Visual Studio reverts the alignment as I mention in my post in the lower section.Secularism
so, when does it actually "deformat" it ? straight away? when you save, close brackets, put a ; ?Snaky
@Snaky I cite my original post: "[...] without Visual Studio then turning them back to to the first example when ending the statement with a semi-colon or Pressing Ctrl-K + F (Format selected lines)." That is, as soon as I edit the line or re-format the code by Ctrl+K Ctrl+F or if I simply edit the indented line of code and put a semicolon it immediately breaks the indentation.Secularism
U
8

This was answered in a duplicate question that is linked above. I have verified that it works. The important missing part is the operator option. Since I can't comment and vote, here is the solution copied from viktorcpp:

Tools|Options|C#|Formatting

I. Ignore spaces in declaration statements <<-- check this

AND

II. Set spacing for operators Insert space... Ignore spaces ... <<-- check this

Ugly answered 6/2, 2015 at 0:34 Comment(1)
This mostly works, but for ternary operators (myVar = someVarIsTrue ? 1 : 0) everything after the ? will no longer be aligned on save (but using these options, everything before it is aligned)Purdah
S
0

The settings that I'm aware of would be to automatically format on ;, } and paste.
You can disable them to avoid formatting on those.

This is from VS 2013, but I'm sure 2010 will have something similar:

VS settings

I usually don't have issues with reformatting, since I'll first write the code / method, and then simply use the code alignment to align on a specific = or . or ,.

You'll have to avoid the formatting (CTRL-K,F), but I can't remember really needing it on a daily basis (unless I get some really bad code from somewhere ...)

Other than that, I usually use Sublime for some other specific uses since I love the multi cursors, and once you get use to it, it's sometimes easier to paste into sublime, edit, and paste back.

Hope this helped.

Snaky answered 26/11, 2014 at 10:9 Comment(4)
Yes, disabling would probably fix the issue, but I don't want to diasble since I want it to do the re-formatting on semi-colon etc - just not ruin my aligned assignments. Also, this is very much my case: going through massive amounts of code where I really would like to do (Ctrl+K,F) often without it ruining the alignments. In the options I have enabled the Tools->Options->Text Editor->C#->Formatting->Spacing->"Ignore spaces in declaration statements" but I can not figure out why it is only "declaration statements". I want exactly the same functionality but "Assignment statements" as well :/Secularism
sounds like you have a niche for writing an extension for VS then :) No idea how easy it'll be though... hopefully somebody else knows more and can shed light on your problemSnaky
Thanks for your help though, you are right, I have been thinking of writing an extension, but right now there's no real time available for it. There must be progress made continuousely to the code.Secularism
been there, done that, i have the equivalent of the dead aeroplanes junk yards you see in movies, only it's a folder with projects i wanted to do :)Snaky

© 2022 - 2024 — McMap. All rights reserved.